Class: Paapi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/paapi/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_key: Paapi.access_key, secret_key: Paapi.secret_key, partner_tag: Paapi.partner_tag, market: Paapi.market || DEFAULT_MARKET, condition: Paapi.condition || DEFAULT_CONDITION, resources: Paapi.resources || DEFAULT_RESOURCES, partner_type: DEFAULT_PARTNER_TYPE) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/paapi/client.rb', line 9

def initialize(
  access_key: Paapi.access_key,
  secret_key: Paapi.secret_key,
  partner_tag: Paapi.partner_tag,
  market: Paapi.market || DEFAULT_MARKET,
  condition: Paapi.condition || DEFAULT_CONDITION,
  resources: Paapi.resources || DEFAULT_RESOURCES,
  partner_type: DEFAULT_PARTNER_TYPE
)
  raise ArgumentError unless MARKETPLACES.key?(market.to_sym)

  @access_key = access_key
  @secret_key = secret_key
  @partner_type = partner_type
  @resources = resources unless resources.nil?
  @condition = condition
  self.market = market
  @partner_tag = partner_tag if !partner_tag.nil?

  @http = Net::HTTP::Persistent.new(name: "paapi").tap do |c|
    c.open_timeout = 2
    c.read_timeout = 5
    c.write_timeout = 5
  end
end

Instance Attribute Details

#access_keyObject (readonly)

Returns the value of attribute access_key.



7
8
9
# File 'lib/paapi/client.rb', line 7

def access_key
  @access_key
end

#conditionObject

Returns the value of attribute condition.



6
7
8
# File 'lib/paapi/client.rb', line 6

def condition
  @condition
end

#httpObject (readonly)

Returns the value of attribute http.



7
8
9
# File 'lib/paapi/client.rb', line 7

def http
  @http
end

#marketObject

Returns the value of attribute market.



7
8
9
# File 'lib/paapi/client.rb', line 7

def market
  @market
end

#marketplaceObject

Returns the value of attribute marketplace.



6
7
8
# File 'lib/paapi/client.rb', line 6

def marketplace
  @marketplace
end

#partner_tagObject

Returns the value of attribute partner_tag.



6
7
8
# File 'lib/paapi/client.rb', line 6

def partner_tag
  @partner_tag
end

#partner_typeObject (readonly)

Returns the value of attribute partner_type.



7
8
9
# File 'lib/paapi/client.rb', line 7

def partner_type
  @partner_type
end

#resourcesObject

Returns the value of attribute resources.



6
7
8
# File 'lib/paapi/client.rb', line 6

def resources
  @resources
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



7
8
9
# File 'lib/paapi/client.rb', line 7

def secret_key
  @secret_key
end

Instance Method Details

#get_browse_nodes(browse_node_ids:, **options) ⇒ Object



65
66
67
68
# File 'lib/paapi/client.rb', line 65

def get_browse_nodes(browse_node_ids:, **options)
  payload = {BrowseNodeIds: Array(browse_node_ids), Resources: @resources}.merge(options)
  request(op: :get_browse_nodes, payload: payload)
end

#get_items(item_ids:, **options) ⇒ Object



43
44
45
46
# File 'lib/paapi/client.rb', line 43

def get_items(item_ids:, **options)
  payload = {"PartnerTag" => partner_tag, "PartnerType" => "Associates", ItemIds: Array(item_ids), Resources: @resources}.merge(options)
  request(op: :get_items, payload: payload)
end

#get_variations(asin:, **options) ⇒ Object



48
49
50
51
# File 'lib/paapi/client.rb', line 48

def get_variations(asin:, **options)
  payload = {ASIN: asin, Resources: @resources}.merge(options)
  request(op: :get_variations, payload: payload)
end

#search_items(**options) ⇒ Object

TODO: Currently we assume Keywords, but we need one of the following: [Keywords Actor Artist Author Brand Title ]

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
62
63
# File 'lib/paapi/client.rb', line 54

def search_items(**options)
  options.transform_keys!(&:to_s)
  raise ArgumentError.new("Missing keywords") unless (options.keys & SEARCH_PARAMS).length.positive?

  search_index = options.dig(:SearchIndex) || "All"

  payload = {"PartnerTag" => partner_tag, "PartnerType" => "Associates", "Resources" => @resources, "ItemCount" => 10, "ItemPage" => 1, "SearchIndex" => search_index}.merge(options)

  request(op: :search_items, payload: payload)
end