class CubaApi::CORS

Constants

DEFAULT_METHODS

Public Class Methods

new( options, &block ) click to toggle source
# 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

Public Instance Methods

allow_origin( req, res ) click to toggle source
# 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 ) click to toggle source
# 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
config() click to toggle source
# File lib/cuba_api/cors.rb, line 16
def config
  @config
end
expose=( expose ) click to toggle source
# File lib/cuba_api/cors.rb, line 37
def expose=( expose )
  self._expose = expose
end
headers( headers ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# File lib/cuba_api/cors.rb, line 33
def max_age=( max )
  @options[ :cors_max_age ] = max
end
method_missing( method, *args ) click to toggle source
Calls superclass method
# 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
methods( method, methods = nil ) click to toggle source
# 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 ) click to toggle source
# File lib/cuba_api/cors.rb, line 55
def methods=( *methods )
  self._methods = [ methods ].flatten.collect{ |h| h.to_s.upcase } 
end
origins( domain ) click to toggle source
# 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 ) click to toggle source
# File lib/cuba_api/cors.rb, line 41
def origins=( *origins )
  self._origins = [ origins ].flatten
end
process( req, res, methods ) click to toggle source
# 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