require 'gds_api/helpers'

class ApplicationController < ActionController::Base
  include GdsApi::Helpers
  include Slimmer::Headers

  protect_from_forgery with: :exception

  private

  def error_404; error 404; end
  def error_410; error 410; end
  def error_503(e); error(503, e); end

  def error(status_code, exception = nil)
    render status: status_code, text: "#{status_code} error"
  end

  def cacheable_404
    set_expiry(10.minutes)
    error 404
  end

  def set_expiry(duration = 30.minutes)
    unless Rails.env.development?
      expires_in(duration, :public => true)
    end
  end

  def validate_slug_param(param_name = :slug)
    param_to_use = params[param_name]
    if param_to_use.parameterize != param_to_use
      cacheable_404
    end
  rescue StandardError # Triggered by trying to parameterize malformed UTF-8
    cacheable_404
  end
end

require 'gds_api/content_api'

class ApplicationController < ActionController::Base
  include Slimmer::Headers

  protect_from_forgery with: :exception

  private

  def error_404; error 404; end
  def error_410; error 410; end
  def error_503(e); error(503, e); end

  def error(status_code, exception = nil)
    render status: status_code, text: "#{status_code} error"
  end

  def cacheable_404
    set_expiry(10.minutes)
    error 404
  end

  def set_expiry(duration = 30.minutes)
    unless Rails.env.development?
      expires_in(duration, :public => true)
    end
  end

  def validate_slug_param(param_name = :slug)
    param_to_use = params[param_name]
    if param_to_use.parameterize != param_to_use
      cacheable_404
    end
  rescue StandardError # Triggered by trying to parameterize malformed UTF-8
    cacheable_404
  end

  def content_api
    @content_api ||= GdsApi::ContentApi.new(
      Plek.current.find('contentapi'),
      { web_urls_relative_to: Plek.current.website_root }
    )
  end
end

Set Content API web URLs relative to the website root
Set Content API web URLs relative to the website root

Define a custom instance of the Content API adapter, configured to use
the value returned from 'Plek.current.website_root' as the hostname
for the tag URLs. This fixes tag URLs in development pointing to the
'www' host.
