Class: BatchApi::Operation::Rack

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

Direct Known Subclasses

Rails

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



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

def initialize(op, base_env, app)
  @op = op

  @method = op["method"]
  @url = op["url"]
  @params = op["params"] || {}
  @headers = op["headers"] || {}

  raise 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

#appObject

Returns the value of attribute app.



10
11
12
# File 'lib/batch_api/operation/rack.rb', line 10

def app
  @app
end

#envObject

Returns the value of attribute env.



10
11
12
# File 'lib/batch_api/operation/rack.rb', line 10

def env
  @env
end

#headersObject

Returns the value of attribute headers.



9
10
11
# File 'lib/batch_api/operation/rack.rb', line 9

def headers
  @headers
end

#methodObject

Returns the value of attribute method.



9
10
11
# File 'lib/batch_api/operation/rack.rb', line 9

def method
  @method
end

#paramsObject

Returns the value of attribute params.



9
10
11
# File 'lib/batch_api/operation/rack.rb', line 9

def params
  @params
end

#resultObject

Returns the value of attribute result.



10
11
12
# File 'lib/batch_api/operation/rack.rb', line 10

def result
  @result
end

#urlObject

Returns the value of attribute url.



9
10
11
# File 'lib/batch_api/operation/rack.rb', line 9

def url
  @url
end

Instance Method Details

#executeObject

Execute a batch request, returning a BatchResponse object. If an error occurs, it returns the same results as Rails would.



34
35
36
37
38
39
40
41
42
# File 'lib/batch_api/operation/rack.rb', line 34

def execute
  process_env
  begin
    response = @app.call(@env)
  rescue => err
    response = BatchApi::Errors::Operation.new(err).render
  end
  BatchApi::Response.new(response)
end

#process_envObject

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.



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
# File 'lib/batch_api/operation/rack.rb', line 47

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
  @env["REQUEST_URI"] = @env["REQUEST_URI"].gsub(/\/batch.*/, @url)
  @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