Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/bls/core/resources/inventory_level.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@ def resources_path
"#{api.base_url}/products/inventory-levels"
end

def to_json
{
sku: sku,
date: date,
distribution_center: distribution_center,
quantity: quantity
}
end

def self.retrieve_all
resource = new
api_client = resource.client
response = api_client.get(path: resource.resources_path)
if response.success?
response.data[:products].map { |product| build(product) }
response.data[:inventory].map { |inventory| build(inventory) }
else
raise ResourceRetrievalError.build(response)
end
Expand Down
29 changes: 17 additions & 12 deletions lib/bls/core/testing/fake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,25 @@ module Testing
class Fake
def self.configure(*); end

class DistributionCenterInventory
def self.create(name: "WEST_COAST", quantity: 100)
OpenStruct.new(distribution_center: name, quantity: quantity)
end
end

class InventoryLevel
def self.create(sku: "SKU1", date: Date.today.to_s,
dc_inventory: [DistributionCenterInventory.create])
inventory_level = OpenStruct.new(
date: date,
inventory_by_distribution_center: dc_inventory
)
OpenStruct.new(sku: sku, inventory_levels: [inventory_level])
distribution_center_name: "WEST_COAST",
quantity: 100)
OpenStruct.new(sku: sku, date: date,
distribution_center: distribution_center_name,
quantity: quantity)
end

def self.serialize(sku: "SKU1", date: Date.today.to_s,
distribution_center_name: "WEST_COAST",
quantity: 100)
create(sku: sku, date: date,
distribution_center_name: distribution_center_name,
quantity: quantity).to_h
end

def self.retrieve_all
[Fake::InventoryLevel.create]
end
end

Expand Down
21 changes: 20 additions & 1 deletion spec/core/resources/inventory_level_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,26 @@

result = Bls::Core::InventoryLevel.retrieve_all

expect(result.map(&:product_code)).to match_array(product_id)
expect(result.map(&:sku)).to match_array(product_id)
end
end

describe "#to_json" do
it "returns a serialized inventory level" do
level = Bls::Core::InventoryLevel.new(sku: "BLS123",
date: "2026-01-01",
distribution_center: "WEST_COAST",
quantity: 100)
serialized_level = {
sku: "BLS123",
date: "2026-01-01",
distribution_center: "WEST_COAST",
quantity: 100,
}

result = level.to_json

expect(result).to eq(serialized_level)
end
end
end
18 changes: 5 additions & 13 deletions spec/support/fixtures/inventory_levels_response.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
{
"products": [
"inventory": [
{
"product_code": "MP0527",
"inventory_levels": [
{
"date": "2020-04-19",
"inventory_by_distribution_center": [
{
"distribution_center": "WEST_COAST",
"quantity": "2.0"
}
]
}
]
"sku": "MP0527",
"date": "2020-04-19",
"distribution_center": "WEST_COAST",
"quantity": 2
}
],
"missing_product_codes": [
Expand Down