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.



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

#appObject

Returns the value of attribute app.



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

def app
  @app
end

#envObject

Returns the value of attribute env.



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

def env
  @env
end

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/batch_api/operation/rack.rb', line 7

def headers
  @headers
end

#methodObject

Returns the value of attribute method.



7
8
9
# File 'lib/batch_api/operation/rack.rb', line 7

def method
  @method
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#paramsObject

Returns the value of attribute params.



7
8
9
# File 'lib/batch_api/operation/rack.rb', line 7

def params
  @params
end

#resultObject

Returns the value of attribute result.



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

def result
  @result
end

#urlObject

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

#executeObject

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_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.



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