Skip to content

Commit

Permalink
Add Marketplace.id and ids shorthands
Browse files Browse the repository at this point in the history
  • Loading branch information
hakanensari committed Oct 5, 2024
1 parent 0db58f3 commit 71e4c54
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 34 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Added

- Marketplace.id and Marketplace.ids shorthands

## [3.0.0] - 2024-10-04

### Added
Expand Down
20 changes: 19 additions & 1 deletion lib/peddler/marketplace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,35 @@ module Peddler
class << self
# @param [String] country_code
def find(country_code)
values = MARKETPLACE_IDS.fetch(country_code) do
values = MARKETPLACE_IDS.fetch(country_code == "GB" ? "UK" : country_code) do
raise ArgumentError, "#{country_code} not found"
end

new(**values.merge(country_code: country_code))
end

# @param [String] country_code
# @return [String]
def id(country_code)
find(country_code).id
end

# @param [Array<String>] country_codes
# @return [Array<String>]
def ids(*country_codes)
country_codes.map { |country_code| id(country_code) }
end
end

# @return [Peddler::Endpoint]
def endpoint
Endpoint.find_by_selling_region(selling_region)
end

# @note So HTTP can encode
# @return [String]
def to_str
id
end
end
end
2 changes: 1 addition & 1 deletion test/peddler/api/catalog_items_2022_04_01_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CatalogItems20220401Test < Minitest::Test

def test_search_catalog_items
res = api.search_catalog_items(
["A1F83G8C2ARO7P"],
Marketplace.ids("UK"),
identifiers: "188864544X",
identifiers_type: "ASIN",
)
Expand Down
10 changes: 5 additions & 5 deletions test/peddler/api/feeds_2021_06_30_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ def test_create_feed
"attributes": {
"merchant_suggested_asin": [{
"value": "188864544X",
"marketplace_id": "A1F83G8C2ARO7P",
"marketplace_id": Marketplace.id("UK"),
}],
"condition_type": [{
"value": "new_new",
"marketplace_id": "A1F83G8C2ARO7P",
"marketplace_id": Marketplace.id("UK"),
}],
"merchant_shipping_group": [
{
"value": "legacy-template-id",
"marketplace_id": "A1F83G8C2ARO7P",
"marketplace_id": Marketplace.id("UK"),
},
],
"fulfillment_availability": [{
Expand All @@ -71,7 +71,7 @@ def test_create_feed
"value_with_tax": 450,
}],
}],
"marketplace_id": "A1F83G8C2ARO7P",
"marketplace_id": Marketplace.id("UK"),
}],
},
},
Expand All @@ -83,7 +83,7 @@ def test_create_feed

payload = {
"feedType" => "JSON_LISTINGS_FEED",
"marketplaceIds" => ["A1F83G8C2ARO7P"],
"marketplaceIds" => Marketplace.ids("UK"),
"inputFeedDocumentId" => feed_document_id,
}
res = api.create_feed(payload)
Expand Down
16 changes: 8 additions & 8 deletions test/peddler/api/listings_items_2021_08_01_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ def test_put_listings_item
attributes: {
merchant_suggested_asin: [{
value: "188864544X",
marketplace_id: "A1F83G8C2ARO7P",
marketplace_id: Marketplace.id("UK"),
}],
condition_type: [{
value: "new_new",
marketplace_id: "A1F83G8C2ARO7P",
marketplace_id: Marketplace.id("UK"),
}],
merchant_shipping_group: [
{
value: "legacy-template-id",
marketplace_id: "A1F83G8C2ARO7P",
marketplace_id: Marketplace.id("UK"),
},
],
fulfillment_availability: [{
Expand All @@ -50,12 +50,12 @@ def test_put_listings_item
value_with_tax: 450,
}],
}],
marketplace_id: "A1F83G8C2ARO7P",
marketplace_id: Marketplace.id("UK"),
}],
},
}

res = api.put_listings_item("A34PPN1ZLYCOGT", "SKU123", "A1F83G8C2ARO7P", body)
res = api.put_listings_item("A34PPN1ZLYCOGT", "SKU123", Marketplace.id("UK"), body)

assert_predicate(res.status, :ok?)
end
Expand All @@ -79,7 +79,7 @@ def test_patch_listings_item
],
}

res = api.patch_listings_item("A34PPN1ZLYCOGT", "SKU123", "A1F83G8C2ARO7P", body)
res = api.patch_listings_item("A34PPN1ZLYCOGT", "SKU123", Marketplace.id("UK"), body)

assert_predicate(res.status, :ok?)
end
Expand All @@ -88,15 +88,15 @@ def test_get_listings_item
res = api.get_listings_item(
"A34PPN1ZLYCOGT",
"SKU123",
"A1F83G8C2ARO7P",
Marketplace.id("UK"),
included_data: "attributes,issues",
)

assert_predicate(res.status, :ok?)
end

def test_delete_listings_item
res = api.delete_listings_item("A34PPN1ZLYCOGT", "SKU123", "A1F83G8C2ARO7P")
res = api.delete_listings_item("A34PPN1ZLYCOGT", "SKU123", Marketplace.id("UK"))

assert_predicate(res.status, :ok?)
end
Expand Down
2 changes: 1 addition & 1 deletion test/peddler/api/listings_restrictions_2021_08_01_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_get_listings_restrictions
res = api.get_listings_restrictions(
"188864544X",
"A34PPN1ZLYCOGT",
"A1F83G8C2ARO7P",
Marketplace.id("UK"),
condition_type: "new_new",
)

Expand Down
3 changes: 2 additions & 1 deletion test/peddler/api/orders_v0_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
require "helper"

require "peddler/api/orders_v0"
require "peddler/marketplace"

module Peddler
class API
class OrdersV0Test < Minitest::Test
include FeatureHelpers

def test_get_orders
res = api.sandbox.get_orders(["ATVPDKIKX0DER"], created_after: "TEST_CASE_200")
res = api.sandbox.get_orders(Marketplace.ids("US"), created_after: "TEST_CASE_200")

assert_predicate(res.status, :ok?)
end
Expand Down
16 changes: 8 additions & 8 deletions test/peddler/api/product_pricing_2022_05_01_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ def test_get_featured_offer_expected_price_batch
{
"uri" => "/products/pricing/2022-05-01/offer/featuredOfferExpectedPrice",
"method" => "GET",
"marketplaceId" => "A1F83G8C2ARO7P",
"marketplaceId" => Marketplace.id("UK"),
"sku" => "TSY-IBT-IL2",
},
{
"uri" => "/products/pricing/2022-05-01/offer/featuredOfferExpectedPrice",
"method" => "GET",
"marketplaceId" => "A1F83G8C2ARO7P",
"marketplaceId" => Marketplace.id("UK"),
"sku" => "SM-2DHM-HZJF",
},
{
"uri" => "/products/pricing/2022-05-01/offer/featuredOfferExpectedPrice",
"method" => "GET",
"marketplaceId" => "A1F83G8C2ARO7P",
"marketplaceId" => Marketplace.id("UK"),
"sku" => "Q0-I4ZH-B6JS",
},
{
"uri" => "/products/pricing/2022-05-01/offer/featuredOfferExpectedPrice",
"method" => "GET",
"marketplaceId" => "A1F83G8C2ARO7P",
"marketplaceId" => Marketplace.id("UK"),
"sku" => "G1-MPH9-Y7RX",
},
],
Expand All @@ -50,28 +50,28 @@ def test_get_competitive_summary
uri: "/products/pricing/2022-05-01/items/competitiveSummary",
method: "GET",
asin: "188864544X",
marketplaceId: "A1F83G8C2ARO7P",
marketplaceId: Marketplace.id("UK"),
includedData: ["featuredBuyingOptions", "referencePrices", "lowestPricedOffers"],
},
{
uri: "/products/pricing/2022-05-01/items/competitiveSummary",
method: "GET",
asin: "8086217868",
marketplaceId: "A1F83G8C2ARO7P",
marketplaceId: Marketplace.id("UK"),
includedData: ["featuredBuyingOptions", "referencePrices", "lowestPricedOffers"],
},
{
uri: "/products/pricing/2022-05-01/items/competitiveSummary",
method: "GET",
asin: "3822843865",
marketplaceId: "A1F83G8C2ARO7P",
marketplaceId: Marketplace.id("UK"),
includedData: ["featuredBuyingOptions", "referencePrices", "lowestPricedOffers"],
},
{
uri: "/products/pricing/2022-05-01/items/competitiveSummary",
method: "GET",
asin: "B003EHOCGE",
marketplaceId: "A1F83G8C2ARO7P",
marketplaceId: Marketplace.id("UK"),
includedData: ["featuredBuyingOptions", "referencePrices", "lowestPricedOffers"],
},
],
Expand Down
10 changes: 5 additions & 5 deletions test/peddler/api/product_pricing_v0_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ class ProductPricingV0Test < Minitest::Test
include FeatureHelpers

def test_get_pricing
res = api.get_pricing("A1F83G8C2ARO7P", "Asin", asins: ["188864544X"])
res = api.get_pricing(Marketplace.id("UK"), "Asin", asins: ["188864544X"])

assert_predicate(res.status, :ok?)
end

def test_get_competitive_pricing
res = api.get_competitive_pricing("A1F83G8C2ARO7P", "Asin", asins: ["188864544X"])
res = api.get_competitive_pricing(Marketplace.id("UK"), "Asin", asins: ["188864544X"])

assert_predicate(res.status, :ok?)
end

def test_get_item_offers
res = api.get_item_offers("A1F83G8C2ARO7P", "New", "B0CHXFCYCR")
res = api.get_item_offers(Marketplace.id("UK"), "New", "B0CHXFCYCR")

assert_predicate(res.status, :ok?)
end
Expand All @@ -33,13 +33,13 @@ def test_get_item_offers_batch
{
"uri" => "/products/pricing/v0/items/B0CHXFCYCR/offers",
"method" => "GET",
"MarketplaceId" => "A1F83G8C2ARO7P",
"MarketplaceId" => Marketplace.id("UK"),
"ItemCondition" => "New",
},
{
"uri" => "/products/pricing/v0/items/B0CHXFCYCR/offers",
"method" => "GET",
"MarketplaceId" => "A1F83G8C2ARO7P",
"MarketplaceId" => Marketplace.id("UK"),
"ItemCondition" => "Used",
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ProductTypeDefinitions20200901Test < Minitest::Test
def test_get_definitions_product_type
res = api.get_definitions_product_type(
"PRODUCT",
"A1F83G8C2ARO7P",
Marketplace.id("UK"),
requirements: "LISTING_OFFER_ONLY",
)

Expand Down
2 changes: 1 addition & 1 deletion test/peddler/api/reports_2021_06_30_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_create_report
res = api.create_report(
{
"reportType" => "GET_MERCHANTS_LISTINGS_FYP_REPORT",
"marketplaceIds" => ["A1F83G8C2ARO7P"],
"marketplaceIds" => Marketplace.ids("UK"),
},
)

Expand Down
21 changes: 19 additions & 2 deletions test/peddler/marketplace_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
module Peddler
class MarketplaceTest < Minitest::Test
def setup
country_code = MARKETPLACE_IDS.keys.sample
@marketplace = Marketplace.find(country_code)
end

Expand All @@ -32,8 +31,26 @@ def test_endpoint

def test_invalid_country_code
assert_raises(ArgumentError) do
Marketplace.new("FOO").id
Marketplace.find("FOO")
end
end

def test_id_class_method
assert(Marketplace.id(country_code))
end

def test_id_gb
assert_equal("A1F83G8C2ARO7P", Marketplace.id("GB"))
end

def test_ids
assert(Marketplace.ids(*MARKETPLACE_IDS.keys))
end

private

def country_code
MARKETPLACE_IDS.keys.sample
end
end
end

0 comments on commit 71e4c54

Please sign in to comment.