Class: Rack::Cors::Resources

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

Instance Method Summary collapse

Constructor Details

#initializeResources

Returns a new instance of Resources.



219
220
221
222
223
# File 'lib/rack/cors.rb', line 219

def initialize
  @origins = []
  @resources = []
  @public_resources = false
end

Instance Method Details

#allow_origin?(source, env = {}) ⇒ Boolean

Returns:

  • (Boolean)


246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/rack/cors.rb', line 246

def allow_origin?(source,env = {})
  return true if public_resources?

  effective_source = (source == 'null' ? 'file://' : source)

  return !! @origins.detect do |origin|
    if origin.is_a?(Proc)
      origin.call(source,env)
    else
      origin === effective_source
    end
  end
end

#find_resource(path) ⇒ Object



260
261
262
# File 'lib/rack/cors.rb', line 260

def find_resource(path)
  @resources.detect{|r| r.match?(path)}
end

#origins(*args, &blk) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/rack/cors.rb', line 225

def origins(*args, &blk)
  @origins = args.flatten.collect do |n|
    case n
    when Regexp,
         /^https?:\/\//,
         'file://'        then n
    when '*'              then @public_resources = true; n
    else                  Regexp.compile("^[a-z][a-z0-9.+-]*:\\\/\\\/#{Regexp.quote(n)}")
    end
  end.flatten
  @origins.push(blk) if blk
end

#public_resources?Boolean

Returns:

  • (Boolean)


242
243
244
# File 'lib/rack/cors.rb', line 242

def public_resources?
  @public_resources
end

#resource(path, opts = {}) ⇒ Object



238
239
240
# File 'lib/rack/cors.rb', line 238

def resource(path, opts={})
  @resources << Resource.new(public_resources?, path, opts)
end