Class: CubaApi::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.



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

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



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

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



93
94
95
96
97
# File 'lib/cuba_api/cors.rb', line 93

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

#allowed?(methods) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
# File 'lib/cuba_api/cors.rb', line 66

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



16
17
18
# File 'lib/cuba_api/cors.rb', line 16

def config
  @config
end

#expose=(expose) ⇒ Object



37
38
39
# File 'lib/cuba_api/cors.rb', line 37

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

#headers(headers) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cuba_api/cors.rb', line 79

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



75
76
77
# File 'lib/cuba_api/cors.rb', line 75

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

#max_age=(max) ⇒ Object



33
34
35
# File 'lib/cuba_api/cors.rb', line 33

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

#methods(method, methods = nil) ⇒ Object



59
60
61
62
63
64
# File 'lib/cuba_api/cors.rb', line 59

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



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

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

#origins(domain) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/cuba_api/cors.rb', line 45

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

#origins=(*origins) ⇒ Object



41
42
43
# File 'lib/cuba_api/cors.rb', line 41

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

#process(req, res, methods) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cuba_api/cors.rb', line 99

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