Class: Rack::Cors::Resources

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

Instance Method Summary collapse

Constructor Details

#initializeResources

Returns a new instance of Resources.



62
63
64
65
66
# File 'lib/rhoconnect/middleware/cors.rb', line 62

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

Instance Method Details

#allow_origin?(source) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
# File 'lib/rhoconnect/middleware/cors.rb', line 93

def allow_origin?(source)
  result = public_resources? || @origins.include?(source) ||
      (not (@origins.select {|n| n.class == Regexp && n.match(source)}).empty?)
  #TODO: to fix  "n.match(source)". Unsure, but it may lead to security risks.
  result
end

#find_resource(path) ⇒ Object



100
101
102
# File 'lib/rhoconnect/middleware/cors.rb', line 100

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

#origins(*args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rhoconnect/middleware/cors.rb', line 68

def origins(*args)
  @origins = args.flatten.collect do |n|
    if n.class == Regexp
      n
    else
      case n
      when /^[a-z]+:\/\// then n
      when '*'
        @public_resources = true
        n
      else
        "http://#{n}"
      end
    end
  end
end

#public_resources?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/rhoconnect/middleware/cors.rb', line 89

def public_resources?
  @public_resources
end

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



85
86
87
# File 'lib/rhoconnect/middleware/cors.rb', line 85

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