Class: Rack::Cors::Resources

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResources

Returns a new instance of Resources.



270
271
272
273
274
# File 'lib/rack/cors.rb', line 270

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

Instance Attribute Details

#resourcesObject (readonly)

Returns the value of attribute resources.



268
269
270
# File 'lib/rack/cors.rb', line 268

def resources
  @resources
end

Instance Method Details

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

Returns:

  • (Boolean)


298
299
300
301
302
303
304
305
306
307
308
# File 'lib/rack/cors.rb', line 298

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

#match_resource(path, env) ⇒ Object



310
311
312
# File 'lib/rack/cors.rb', line 310

def match_resource(path, env)
  @resources.detect { |r| r.match?(path, env) }
end

#origins(*args, &blk) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/rack/cors.rb', line 276

def origins(*args, &blk)
  @origins = args.flatten.reject{ |s| s == '' }.map do |n|
    case n
    when Proc,
         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)


294
295
296
# File 'lib/rack/cors.rb', line 294

def public_resources?
  @public_resources
end

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



290
291
292
# File 'lib/rack/cors.rb', line 290

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

#resource_for_path(path) ⇒ Object



314
315
316
# File 'lib/rack/cors.rb', line 314

def resource_for_path(path)
  @resources.detect { |r| r.matches_path?(path) }
end