Class: Rack::Cors::Resources

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResources

Returns a new instance of Resources.



10
11
12
13
14
# File 'lib/rack/cors/resources.rb', line 10

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

Instance Attribute Details

#resourcesObject (readonly)

Returns the value of attribute resources.



8
9
10
# File 'lib/rack/cors/resources.rb', line 8

def resources
  @resources
end

Instance Method Details

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

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rack/cors/resources.rb', line 39

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

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

#match_resource(path, env) ⇒ Object



53
54
55
# File 'lib/rack/cors/resources.rb', line 53

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

#origins(*args, &blk) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rack/cors/resources.rb', line 16

def origins(*args, &blk)
  @origins = args.flatten.reject { |s| s == '' }.map do |n|
    case n
    when Proc, Regexp, %r{^[a-z][a-z0-9.+-]*://}
      n
    when '*'
      @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)


35
36
37
# File 'lib/rack/cors/resources.rb', line 35

def public_resources?
  @public_resources
end

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



31
32
33
# File 'lib/rack/cors/resources.rb', line 31

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

#resource_for_path(path) ⇒ Object



57
58
59
# File 'lib/rack/cors/resources.rb', line 57

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