Class: Rack::Auditor

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

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Auditor

Returns a new instance of Auditor.



5
6
7
8
9
10
11
# File 'lib/rack/auditor.rb', line 5

def initialize(app, options = {})
  @app        = app
  @root_uri   = options[:root_uri] || 'http://snowflake.dev/'
  @dev_mode   = options[:dev_mode] || false
  @api_prefix = options[:api_prefix] || ''
  @access_method = options[:access_method] || :key #key or token
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rack/auditor.rb', line 13

def call(env)
  unless @dev_mode || inappropriate_request(env)
    case @access_method
    when :key
      key    = env['HTTP_X_API_KEY']
      secret = env['HTTP_X_API_SECRET']

      return forbidden unless key && secret
      response = HTTParty.get "#{@root_uri}?api_key=#{key}&api_secret=#{secret}"
    when :token
      token  = env['HTTP_X_ACCESS_TOKEN']

      return forbidden unless token
      response = HTTParty.get "#{@root_uri}?acess_token=#{token}"
    end

    case response.code
    when 403
      forbidden
    when 404
      error_code(404, 'Not Found')
    when 500
      error_code(500, 'Server Error')
    when 503
      error_code(503, 'Maintenance')
    when 504
      error_code(504, 'System Down')
    end
  end

  @app.call(env)
end