Class: Rack::Cors::Resource

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(public_resource, path, opts = {}) ⇒ Resource

Returns a new instance of Resource.



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/rack/cors.rb', line 305

def initialize(public_resource, path, opts={})
  self.path         = path
  self.credentials  = opts[:credentials].nil? ? true : opts[:credentials]
  self.max_age      = opts[:max_age] || 1728000
  self.pattern      = compile(path)
  self.if_proc      = opts[:if]
  self.vary_headers = opts[:vary] && [opts[:vary]].flatten
  @public_resource  = public_resource

  self.headers = case opts[:headers]
  when :any then :any
  when nil then nil
  else
    [opts[:headers]].flatten.collect{|h| h.downcase}
  end

  self.methods = case opts[:methods]
  when :any then [:get, :head, :post, :put, :patch, :delete, :options]
  else
    ensure_enum(opts[:methods]) || [:get]
  end.map{|e| e.to_s }
  
  self.expose = opts[:expose] ? [opts[:expose]].flatten : nil
end

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



303
304
305
# File 'lib/rack/cors.rb', line 303

def credentials
  @credentials
end

#exposeObject

Returns the value of attribute expose.



303
304
305
# File 'lib/rack/cors.rb', line 303

def expose
  @expose
end

#headersObject

Returns the value of attribute headers.



303
304
305
# File 'lib/rack/cors.rb', line 303

def headers
  @headers
end

#if_procObject

Returns the value of attribute if_proc.



303
304
305
# File 'lib/rack/cors.rb', line 303

def if_proc
  @if_proc
end

#max_ageObject

Returns the value of attribute max_age.



303
304
305
# File 'lib/rack/cors.rb', line 303

def max_age
  @max_age
end

#methodsObject

Returns the value of attribute methods.



303
304
305
# File 'lib/rack/cors.rb', line 303

def methods
  @methods
end

#pathObject

Returns the value of attribute path.



303
304
305
# File 'lib/rack/cors.rb', line 303

def path
  @path
end

#patternObject

Returns the value of attribute pattern.



303
304
305
# File 'lib/rack/cors.rb', line 303

def pattern
  @pattern
end

#vary_headersObject

Returns the value of attribute vary_headers.



303
304
305
# File 'lib/rack/cors.rb', line 303

def vary_headers
  @vary_headers
end

Instance Method Details

#match?(path, env) ⇒ Boolean

Returns:

  • (Boolean)


334
335
336
# File 'lib/rack/cors.rb', line 334

def match?(path, env)
  matches_path?(path) && (if_proc.nil? || if_proc.call(env))
end

#matches_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


330
331
332
# File 'lib/rack/cors.rb', line 330

def matches_path?(path)
  pattern =~ path
end

#process_preflight(env) ⇒ Object



338
339
340
341
# File 'lib/rack/cors.rb', line 338

def process_preflight(env)
  return nil if invalid_method_request?(env) || invalid_headers_request?(env)
  {'Content-Type' => 'text/plain'}.merge(to_preflight_headers(env))
end

#to_headers(env) ⇒ Object



343
344
345
346
347
348
349
350
351
# File 'lib/rack/cors.rb', line 343

def to_headers(env)
  h = {
    'Access-Control-Allow-Origin'     => origin_for_response_header(env[ORIGIN_HEADER_KEY]),
    'Access-Control-Allow-Methods'    => methods.collect{|m| m.to_s.upcase}.join(', '),
    'Access-Control-Expose-Headers'   => expose.nil? ? '' : expose.join(', '),
    'Access-Control-Max-Age'          => max_age.to_s }
  h['Access-Control-Allow-Credentials'] = 'true' if credentials
  h
end