Module: Flapjack::Gateways::JSONAPI::Helpers::Headers

Defined in:
lib/flapjack/gateways/jsonapi/helpers/headers.rb

Instance Method Summary collapse

Instance Method Details

#cors_headersObject



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/flapjack/gateways/jsonapi/helpers/headers.rb', line 9

def cors_headers
  allow_headers  = %w(* Content-Type Accept Authorization Cache-Control)
  allow_methods  = %w(GET POST PATCH DELETE OPTIONS)
  expose_headers = %w(Cache-Control Content-Language Content-Type Expires Last-Modified Pragma)
  ch   = {
    'Access-Control-Allow-Origin'   => '*',
    'Access-Control-Allow-Methods'  => allow_methods.join(', '),
    'Access-Control-Allow-Headers'  => allow_headers.join(', '),
    'Access-Control-Expose-Headers' => expose_headers.join(', '),
    'Access-Control-Max-Age'        => '1728000'
  }
  ch.each_pair {|k, v| response[k] = v}
end

#err(status_code, *msg) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/flapjack/gateways/jsonapi/helpers/headers.rb', line 23

def err(status_code, *msg)
  Flapjack.logger.info "Error: #{msg.inspect}"

  if 'DELETE'.eql?(request.request_method)
    # not set by default for delete, but the error structure is JSON
    response['Content-Type'] = media_type_produced(:with_charset => true)
  end

  # TODO include more relevant data
  error_data = {
    :errors => msg.collect {|m|
      {
        :status => status_code.to_s,
        :detail => m
      }
    }
  }

  # Rack::CommonLogger doesn't log requests which result in exceptions.
  # If you want something done properly, do it yourself...
  result = Flapjack.dump_json(error_data)
  access_log = self.class.instance_variable_get('@middleware').detect {|mw|
    mw.first.is_a?(::Rack::CommonLogger)
  }
  unless access_log.nil?
    access_log.first.send(:log, status_code,
      ::Rack::Utils::HeaderHash.new(headers), result,
      env['request_timestamp'])
  end
  [status_code, headers, result]
end

#is_jsonapi_request?Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/flapjack/gateways/jsonapi/helpers/headers.rb', line 55

def is_jsonapi_request?
  return false if request.content_type.nil?
  Flapjack::Gateways::JSONAPI::JSONAPI_MEDIA_TYPE.eql?(request.content_type.split(/\s*[;,]\s*/, 2).first)
end