class Safrano::Batch::MyOApp

Mayonaise

Attributes

db[R]
full_req[R]
response[R]

Public Class Methods

new(full_req) click to toggle source
# File lib/odata/batch.rb, line 30
def initialize(full_req)
  @full_req = full_req
  @db = full_req.service.collections.first.db
end

Public Instance Methods

batch_call(part_req) click to toggle source
# File lib/odata/batch.rb, line 42
def batch_call(part_req)
  env = batch_env(part_req)
  env['HTTP_HOST'] = @full_req.env['HTTP_HOST']
  began_at = Rack::Utils.clock_time
  @request = Safrano::Request.new(env)
  @response = Safrano::Response.new

  if part_req.level == 2
    @request.in_changeset = true
    @request.content_id = part_req.content_id
    @request.content_id_references = part_req.content_id_references
  end

  before
  dispatch

  status, header, body = @response.finish
  # Logging of sub-requests with ODataCommonLogger.
  # A bit hacky but working
  # TODO: test ?
  if (logga = @full_req.env['safrano.logger_mw'])
    logga.batch_log(env, status, header, began_at)
    # TODO: check why/if we need Rack::Utils::HeaderHash.new(header)
    # and Rack::BodyProxy.new(body) ?
  end
  [status, header, body]
end
batch_env(mime_req) click to toggle source
# File lib/odata/batch.rb, line 83
def batch_env(mime_req)
  @env = ::Rack::MockRequest.env_for(mime_req.uri,
                                     method: mime_req.http_method,
                                     input: mime_req.content)
  # Logging of sub-requests
  @env[Rack::RACK_ERRORS] = @full_req.env[Rack::RACK_ERRORS]
  @env.merge! headers_for_env(mime_req.hd)

  @env
end
before() click to toggle source

redefined for $batch

# File lib/odata/batch.rb, line 36
def before
  headers 'Cache-Control' => 'no-cache'
  @request.service = @full_req.service
  headers 'DataServiceVersion' => @request.service.data_service_version
end
headers_for_env(headers) click to toggle source

shamelessely copied from Rack::TEST:Session

# File lib/odata/batch.rb, line 71
def headers_for_env(headers)
  converted_headers = {}

  headers.each do |name, value|
    env_key = name.upcase.tr('-', '_')
    env_key = "HTTP_#{env_key}" unless env_key == 'CONTENT_TYPE'
    converted_headers[env_key] = value
  end

  converted_headers
end