Class: CORS

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

Constant Summary collapse

DEFAULT_METHODS =
[ 'GET',  'HEAD', 'POST', 'PUT', 'DELETE' ]

Instance Method Summary collapse

Constructor Details

#initialize(options, &block) ⇒ CORS

Returns a new instance of CORS.



5
6
7
8
9
10
11
12
# File 'lib/cuba_api/cors.rb', line 5

def initialize( options, &block )
  @options = options
  # only for inspect
  @config = options.config
  # set default max_ago
  self.max_age = 60 * 60 * 24 # one day
  block.call self if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cuba_api/cors.rb', line 18

def method_missing( method, *args )
  m = method.to_s
  if m.match /^_/
    if m =~ /=$/
      @options[ "cors_#{m[1..-2]}".to_sym ] = args.flatten
    else
      @options[ "cors_#{m[1..-1]}".to_sym ]
    end
  else
    super
  end
end

Instance Method Details

#allow_origin(req, res) ⇒ Object



88
89
90
91
92
# File 'lib/cuba_api/cors.rb', line 88

def allow_origin( req, res )
  if orig = origins( req[ 'HTTP_ORIGIN' ] )
    res[ 'Access-Control-Allow-Origin' ] = orig
  end
end

#allowed?(methods) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
# File 'lib/cuba_api/cors.rb', line 61

def allowed?( methods )
  if methods
    methods = methods.collect { |m| m.to_s.upcase }
    ( ( self._methods || DEFAULT_METHODS ) & methods ).size > 0
  else
    true
  end
end

#configObject



14
15
16
# File 'lib/cuba_api/cors.rb', line 14

def config
  @config
end

#expose=(expose) ⇒ Object



35
36
37
# File 'lib/cuba_api/cors.rb', line 35

def expose=( expose )
  self._expose = expose
end

#headers(headers) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cuba_api/cors.rb', line 74

def headers( headers )
  # return headers as they come in when not configured
  headers = headers.split( /,\s+/ ) if headers
  if self._headers && headers
    given = headers.collect{ |h| h.to_s.upcase }
    # give back the allowed subset of the given headers
    result = given & self._headers
    result = nil if result.empty?
    result
  else
    headers
  end
end

#headers=(*headers) ⇒ Object



70
71
72
# File 'lib/cuba_api/cors.rb', line 70

def headers=( *headers )
  self._headers = [ headers ].flatten.collect{ |h| h.to_s.upcase }
end

#max_age=(max) ⇒ Object



31
32
33
# File 'lib/cuba_api/cors.rb', line 31

def max_age=( max )
  @options[ :cors_max_age ] = max
end

#methods(method, methods = nil) ⇒ Object



54
55
56
57
58
59
# File 'lib/cuba_api/cors.rb', line 54

def methods( method, methods = nil )
  methods = methods.collect { |m| m.to_s.upcase } if methods
  if (methods || self._methods || DEFAULT_METHODS).member?( method.to_s.upcase )
    methods || self._methods || DEFAULT_METHODS
  end
end

#methods=(*methods) ⇒ Object



50
51
52
# File 'lib/cuba_api/cors.rb', line 50

def methods=( *methods )
  self._methods = [ methods ].flatten.collect{ |h| h.to_s.upcase } 
end

#origins(domain) ⇒ Object



43
44
45
46
47
48
# File 'lib/cuba_api/cors.rb', line 43

def origins( domain )
  origins = self._origins
  if origins == [ '*' ] || origins.nil? || origins.member?( domain )
    domain
  end
end

#origins=(*origins) ⇒ Object



39
40
41
# File 'lib/cuba_api/cors.rb', line 39

def origins=( *origins )
  self._origins = [ origins ].flatten
end

#process(req, res, methods) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/cuba_api/cors.rb', line 94

def process( req, res, methods )
  allow_origin( req, res )
  res[ 'Access-Control-Max-Age' ] = _max_age.to_s if _max_age
  if m = methods( req[ 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' ], methods )
    res[ 'Access-Control-Allow-Methods' ] = m.join( ', ' )
  end
  if h = headers( req[ 'HTTP_ACCESS_CONTROL_REQUEST_HEADERS' ] )
    res[ 'Access-Control-Allow-Headers' ] = h.join( ', ' )
  end
  unless _expose.nil? || _expose.empty?
    res[ 'Access-Control-Expose-Headers' ] = _expose.join( ', ' )
  end
  res.status = 200
end