Sha256: 129427cb9ef3a7afa5e5b6eaac069b66872b15e36cbfe99c017dd0068ce57e36

Contents?: true

Size: 1.72 KB

Versions: 7

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

require 'aws-sigv4'
require 'http'
require 'json'

module Vacuum
  # An Amazon Product Advertising API operation
  class Operation
    # @!visibility private
    attr_reader :locale, :name, :params

    # Creates a new operation
    #
    # @param [String] name
    # @param [Hash] params
    # @param [Locale] locale
    def initialize(name, params:, locale:)
      @name = name
      @params = params
      @locale = locale
    end

    # @return [Hash]
    def headers
      signature.headers.merge(
        'x-amz-target' =>
          "com.amazon.paapi5.v1.ProductAdvertisingAPIv1.#{name}",
        'content-encoding' => 'amz-1.0',
        'content-type' => 'application/json; charset=utf-8'
      )
    end

    # @return [String]
    def body
      @body ||= build_body
    end

    # @return [String]
    def url
      @url ||= build_url
    end

    private

    def build_body
      hsh = { 'PartnerTag' => locale.partner_tag,
              'PartnerType' => locale.partner_type }

      params.each do |key, val|
        key = key.to_s.split('_')
                 .map { |word| word == 'asin' ? 'ASIN' : word.capitalize }.join
        hsh[key] = val
      end

      JSON.generate(hsh)
    end

    def build_url
      "https://#{locale.host}/paapi5/#{name.downcase}"
    end

    def signature
      signer.sign_request(http_method: 'POST', url: url, body: body)
    end

    def signer
      Aws::Sigv4::Signer.new(service: 'ProductAdvertisingAPI',
                             region: locale.region,
                             access_key_id: locale.access_key,
                             secret_access_key: locale.secret_key,
                             http_method: 'POST', endpoint: locale.host)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
vacuum-4.1.0 lib/vacuum/operation.rb
vacuum-4.0.0 lib/vacuum/operation.rb
vacuum-3.4.1 lib/vacuum/operation.rb
vacuum-3.4.0 lib/vacuum/operation.rb
vacuum-3.3.0 lib/vacuum/operation.rb
vacuum-3.2.0 lib/vacuum/operation.rb
vacuum-3.1.0 lib/vacuum/operation.rb