Class: StackMob::Middleware::Proxy
- Inherits:
-
Object
- Object
- StackMob::Middleware::Proxy
- Defined in:
- lib/stackmob/middleware/proxy.rb
Constant Summary collapse
- HEADER_NAME =
'X-StackMob-Proxy'- RACK_ENV_NAME =
'HTTP_X_STACKMOB_PROXY'- VALID_HEADER_VALUES =
['stackmob-api']
- EXCLUDED_HEADERS =
["VERSION", "DATE", "HOST", "ACCEPT"].map { |s| "HTTP_#{s}" }
Instance Method Summary collapse
- #call(env) ⇒ Object
- #http_headers(env) ⇒ Object
- #http_method(env) ⇒ Object
-
#initialize(app) ⇒ Proxy
constructor
A new instance of Proxy.
Constructor Details
#initialize(app) ⇒ Proxy
Returns a new instance of Proxy.
27 28 29 |
# File 'lib/stackmob/middleware/proxy.rb', line 27 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/stackmob/middleware/proxy.rb', line 31 def call(env) if VALID_HEADER_VALUES.include?(env[RACK_ENV_NAME]) req = ::Rack::Request.new(env) method = http_method(env) headers = http_headers(env) params = [:put,:post].include?(method) ? req.body : req.query_string response = client.request(method, :api, env['PATH_INFO'], params, true, headers) [response.code.to_i, response.to_hash, response.body] else @app.call(env) end end |
#http_headers(env) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/stackmob/middleware/proxy.rb', line 55 def http_headers(env) headers = {} for headerArr in env.select { |k, v| k.start_with? 'HTTP_' } if !EXCLUDED_HEADERS.include?(headerArr[0]) headers[headerArr[0].sub('HTTP_', '')] = headerArr[1] end end headers["Accept"] = "application/json" headers end |
#http_method(env) ⇒ Object
51 52 53 |
# File 'lib/stackmob/middleware/proxy.rb', line 51 def http_method(env) env['REQUEST_METHOD'].downcase.to_sym end |