class AGCOD::Request

Constants

MOCK_REQUEST_IDS
TIME_FORMAT

Attributes

response[R]

Public Class Methods

new(httpable, action, params) click to toggle source
# File lib/aws_agcod/request.rb, line 13
def initialize(httpable, action, params) # httpable is anything that can have post called upon it; this allows passing in a proxy
  @action = action
  @params = sanitized_params(params)

  @response = Response.new(httpable.post(uri, body: body, headers: signed_headers).body)
end

Private Instance Methods

body() click to toggle source
# File lib/aws_agcod/request.rb, line 41
def body
  @body ||= @params.merge(
    'partnerId' => AGCOD.config.partner_id
  ).to_json
end
sanitized_params(params) click to toggle source
# File lib/aws_agcod/request.rb, line 47
def sanitized_params(params)
  # Prefix partner_id when it's not given as part of request_id for creationRequestId and it's not a mock request_id
  if params['creationRequestId'] && !(params['creationRequestId'] =~ /#{AGCOD.config.partner_id}/) && !(MOCK_REQUEST_IDS.member?(params["creationRequestId"]))
    params['creationRequestId'] = "#{AGCOD.config.partner_id}#{params['creationRequestId']}"
  end

  # Remove partner_id when it's prefixed in requestId
  if params['requestId'] && !!(params['requestId'] =~ /^#{AGCOD.config.partner_id}/)
    params['requestId'].sub!(/^#{AGCOD.config.partner_id}/, '')
  end

  params
end
signed_headers() click to toggle source
# File lib/aws_agcod/request.rb, line 22
def signed_headers
  time = Time.now.utc

  headers = {
    'content-type' => 'application/json',
    'x-amz-date' => time.strftime(TIME_FORMAT),
    'accept' => 'application/json',
    'host' => uri.host,
    "x-amz-target" => "com.amazonaws.agcod.AGCODService.#{@action}",
    'date' => time.to_s
  }

  Signature.new(AGCOD.config).sign(uri, headers, body)
end
uri() click to toggle source
# File lib/aws_agcod/request.rb, line 37
def uri
  @uri ||= URI("#{AGCOD.config.uri}/#{@action}")
end