Class: Rack::Cors::Resources
- Inherits:
-
Object
- Object
- Rack::Cors::Resources
- Defined in:
- lib/rack/cors.rb
Instance Method Summary collapse
- #allow_origin?(source, env = {}) ⇒ Boolean
- #find_resource(path) ⇒ Object
-
#initialize ⇒ Resources
constructor
A new instance of Resources.
- #origins(*args, &blk) ⇒ Object
- #public_resources? ⇒ Boolean
- #resource(path, opts = {}) ⇒ Object
Constructor Details
#initialize ⇒ Resources
Returns a new instance of Resources.
101 102 103 104 105 |
# File 'lib/rack/cors.rb', line 101 def initialize @origins = [] @resources = [] @public_resources = false end |
Instance Method Details
#allow_origin?(source, env = {}) ⇒ Boolean
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/rack/cors.rb', line 127 def allow_origin?(source,env = {}) return true if public_resources? return !! @origins.detect do |origin| if origin.is_a?(Proc) origin.call(source,env) else origin === source end end end |
#find_resource(path) ⇒ Object
138 139 140 |
# File 'lib/rack/cors.rb', line 138 def find_resource(path) @resources.detect{|r| r.match?(path)} end |
#origins(*args, &blk) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/rack/cors.rb', line 107 def origins(*args,&blk) @origins = args.flatten.collect do |n| case n when Regexp, /^https?:\/\// then n when 'file://' then n when '*' then @public_resources = true; n else ["http://#{n}", "https://#{n}"] end end.flatten @origins.push(blk) if blk end |
#public_resources? ⇒ Boolean
123 124 125 |
# File 'lib/rack/cors.rb', line 123 def public_resources? @public_resources end |