Class: Apia::CORS

Inherits:
Object
  • Object
show all
Defined in:
lib/apia/cors.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCORS

Returns a new instance of CORS.



10
11
12
13
14
# File 'lib/apia/cors.rb', line 10

def initialize
  @origin = '*'
  @methods = '*'
  @headers = []
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/apia/cors.rb', line 7

def headers
  @headers
end

#methodsObject

Returns the value of attribute methods.



6
7
8
# File 'lib/apia/cors.rb', line 6

def methods
  @methods
end

#originObject

Returns the value of attribute origin.



8
9
10
# File 'lib/apia/cors.rb', line 8

def origin
  @origin
end

Instance Method Details

#to_headersObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/apia/cors.rb', line 16

def to_headers
  return {} if @origin.nil?

  headers = {}
  headers['Access-Control-Allow-Origin'] = @origin

  if @methods.is_a?(String)
    headers['Access-Control-Allow-Methods'] = @methods
  elsif @methods.is_a?(Array) && @methods.any?
    headers['Access-Control-Allow-Methods'] = @methods.map(&:upcase).join(', ')
  end

  if @headers.is_a?(String)
    headers['Access-Control-Allow-Headers'] = @headers
  elsif @headers.is_a?(Array) && @headers.any?
    headers['Access-Control-Allow-Headers'] = @headers.join(', ')
  end

  headers
end