Class: BatchApi::Operation::Rack
- Inherits:
-
Object
- Object
- BatchApi::Operation::Rack
- Defined in:
- lib/batch_api/operation/rack.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
-
#env ⇒ Object
Returns the value of attribute env.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#method ⇒ Object
Returns the value of attribute method.
-
#options ⇒ Object
Returns the value of attribute options.
-
#params ⇒ Object
Returns the value of attribute params.
-
#result ⇒ Object
Returns the value of attribute result.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#execute ⇒ Object
Execute a batch request, returning a BatchResponse object.
-
#initialize(op, base_env, app) ⇒ Rack
constructor
Public: create a new Batch Operation given the specifications for a batch operation (as defined above) and the request environment for the main batch request.
-
#process_env ⇒ Object
Internal: customize the request environment.
Constructor Details
#initialize(op, base_env, app) ⇒ Rack
Public: create a new Batch Operation given the specifications for a batch operation (as defined above) and the request environment for the main batch request.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/batch_api/operation/rack.rb', line 13 def initialize(op, base_env, app) @op = op @method = op["method"] || "get" @url = op["url"] @params = op["params"] || {} @headers = op["headers"] || {} @options = op raise Errors::MalformedOperationError, "BatchAPI operation must include method (received #{@method.inspect}) " + "and url (received #{@url.inspect})" unless @method && @url @app = app # deep_dup to avoid unwanted changes across requests @env = BatchApi::Utils.deep_dup(base_env) end |
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
8 9 10 |
# File 'lib/batch_api/operation/rack.rb', line 8 def app @app end |
#env ⇒ Object
Returns the value of attribute env.
8 9 10 |
# File 'lib/batch_api/operation/rack.rb', line 8 def env @env end |
#headers ⇒ Object
Returns the value of attribute headers.
7 8 9 |
# File 'lib/batch_api/operation/rack.rb', line 7 def headers @headers end |
#method ⇒ Object
Returns the value of attribute method.
7 8 9 |
# File 'lib/batch_api/operation/rack.rb', line 7 def method @method end |
#options ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/batch_api/operation/rack.rb', line 8 def @options end |
#params ⇒ Object
Returns the value of attribute params.
7 8 9 |
# File 'lib/batch_api/operation/rack.rb', line 7 def params @params end |
#result ⇒ Object
Returns the value of attribute result.
8 9 10 |
# File 'lib/batch_api/operation/rack.rb', line 8 def result @result end |
#url ⇒ Object
Returns the value of attribute url.
7 8 9 |
# File 'lib/batch_api/operation/rack.rb', line 7 def url @url end |
Instance Method Details
#execute ⇒ Object
Execute a batch request, returning a BatchResponse object. If an error occurs, it returns the same results as Rails would.
33 34 35 36 37 38 39 40 41 |
# File 'lib/batch_api/operation/rack.rb', line 33 def execute process_env begin response = @app.call(@env) rescue => err response = BatchApi::ErrorWrapper.new(err).render end BatchApi::Response.new(response) end |
#process_env ⇒ Object
Internal: customize the request environment. This is currently done manually and feels clunky and brittle, but is mostly likely fine, though there are one or two environment parameters not yet adjusted.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/batch_api/operation/rack.rb', line 46 def process_env path, qs = @url.split("?") # Headers headrs = (@headers || {}).inject({}) do |heads, (k, v)| heads.tap {|h| h["HTTP_" + k.gsub(/\-/, "_").upcase] = v} end # preserve original headers unless explicitly overridden @env.merge!(headrs) # method @env["REQUEST_METHOD"] = @method.upcase # path and query string if @env["REQUEST_URI"] # not all servers provide REQUEST_URI -- Pow, for instance, doesn't @env["REQUEST_URI"] = @env["REQUEST_URI"].gsub(/#{BatchApi.config.endpoint}.*/, @url) end @env["REQUEST_PATH"] = path @env["ORIGINAL_FULLPATH"] = @env["PATH_INFO"] = @url @env["rack.request.query_string"] = qs @env["QUERY_STRING"] = qs # parameters @env["rack.request.form_hash"] = @params @env["rack.request.query_hash"] = @method == "get" ? @params : nil end |