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.



251
252
253
254
255
# File 'lib/rack/cors.rb', line 251

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

Instance Method Details

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

Returns:

  • (Boolean)


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

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

#match_resource(path, env) ⇒ Object



292
293
294
# File 'lib/rack/cors.rb', line 292

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

#origins(*args, &blk) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/rack/cors.rb', line 257

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)


274
275
276
# File 'lib/rack/cors.rb', line 274

def public_resources?
  @public_resources
end

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



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

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

#resource_for_path(path) ⇒ Object



296
297
298
# File 'lib/rack/cors.rb', line 296

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