Module: Sinatra::CrossOrigin::Helpers

Defined in:
lib/cross_origin.rb

Instance Method Summary collapse

Instance Method Details

#cross_origin(hash = nil) ⇒ Object

Apply cross origin headers either from global config or custom config passed as a hash



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cross_origin.rb', line 10

def cross_origin(hash=nil)
  return unless request.env['HTTP_ORIGIN']
  settings.set hash if hash

  origin = settings.allow_origin == :any ? request.env['HTTP_ORIGIN'] : settings.allow_origin
  methods = settings.allow_methods.map{ |m| m.to_s.upcase! }.join(', ')

  headers_list = {
    'Access-Control-Allow-Origin' => origin,
    'Access-Control-Allow-Methods' => methods,
    'Access-Control-Allow-Headers' => settings.allow_headers.map(&:to_s).join(', '),
    'Access-Control-Allow-Credentials' => settings.allow_credentials.to_s,
    'Access-Control-Max-Age' => settings.max_age.to_s
  }

  headers headers_list
end