From 2f7b1471d54503f65afa5ef1316bbcd22c213513 Mon Sep 17 00:00:00 2001 From: Anton Pershakov Date: Tue, 21 May 2024 12:50:54 +0100 Subject: [PATCH 1/2] Add support of Product API V2.1 --- lib/vend/request.rb | 2 +- lib/vend/resources/V21/product.rb | 10 ++ spec/vend/unit/resources/V21/product_spec.rb | 106 +++++++++++++++++++ 3 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 lib/vend/resources/V21/product.rb create mode 100644 spec/vend/unit/resources/V21/product_spec.rb diff --git a/lib/vend/request.rb b/lib/vend/request.rb index 39d4e80..35a06be 100644 --- a/lib/vend/request.rb +++ b/lib/vend/request.rb @@ -26,7 +26,7 @@ def initialize(version, uri) @uri = case version when '0.9' uri - when '1.0', '2.0' + when '1.0', '2.0', '2.1' "#{version}/#{uri}" else "2.0/#{uri}" diff --git a/lib/vend/resources/V21/product.rb b/lib/vend/resources/V21/product.rb new file mode 100644 index 0000000..3ad0f81 --- /dev/null +++ b/lib/vend/resources/V21/product.rb @@ -0,0 +1,10 @@ +# Products V2.1 +# https://x-series-api.lightspeedhq.com/reference/updateproduct + +module Vend + module V21 + class Product < Resource + include Vend::ResourceActions.new api_version: '2.1', uri: 'products' + end + end +end diff --git a/spec/vend/unit/resources/V21/product_spec.rb b/spec/vend/unit/resources/V21/product_spec.rb new file mode 100644 index 0000000..0de3df1 --- /dev/null +++ b/spec/vend/unit/resources/V21/product_spec.rb @@ -0,0 +1,106 @@ +# frozen_string_literal: true + +require 'securerandom' + +RSpec.describe Vend::V21::Product do + it { is_expected.to be_a(Vend::Resource) } + + describe '.update' do + let(:store) { 'teststore' } + let(:product_id) { SecureRandom.uuid } + let(:params) do + { + details: { + inventory: [ + { + outlet_id: SecureRandom.uuid, + current_amount: 10 + } + ] + } + } + end + + let(:connection) do + Vend::Connection.build(Vend::Config.new(domain_prefix: store, access_token: SecureRandom.hex)) + end + + subject { described_class.update(product_id, params.merge(connection: connection)) } + + before dos + stub_request( + :put, + "https://#{store}.vendhq.com/api/2.1/products/#{product_id}", + ) + .with(body: params.to_json) + .to_return( + body: response.to_json, + headers: {}, + status: 200 + ) + end + + let(:response) do + { + 'data' => { + 'product_id' => product_id, + 'common' => { + 'name' => 'Test product', + 'description' => 'Description', + 'track_inventory' => true, + 'brand_id' => '4dc0a66c-201b-c111-4397-b63bdddcf2e4', + 'product_category_id' => '2fa7fc75-451d-be27-b1a5-6de7c1c0d3ff', + 'product_suppliers' => [ + { + 'id' => '8f26486c-5e15-40bb-b375-45bb541c4649', + 'supplier_id' => nil + } + ], + 'variant_attributes' => [ + { + 'id' => 1, + 'attribute_id' => '39676602-6198-4574-bc7f-72dfb8b7915c' + } + ] + }, + 'details' => { + 'is_active' => true, + 'variant_name' => 'Test product / S / Green', + 'product_codes' => [{'type' => 'CUSTOM','code' => 'MU-HM-S'}], + 'price_excluding_tax' => 25, + 'outlet_taxes' => [], + 'inventory' => [ + { + 'outlet_id' => '10346b77-b098-4d8d-b83a-c0bcc01326cf', + 'current_amount' => 21, + 'reorder_amount' => 0, + 'reorder_point' => 0, + 'initial_average_cost' => nil + } + ], + 'product_suppliers' => [ + { + 'supplier_id' => '', + 'price' => 20 + } + ], + 'variant_attribute_values' => [ + { + 'attribute_id' => '39676602-6198-4574-bc7f-72dfb8b7915c', + 'attribute_value' => 'S' + } + ] + } + } + } + end + + it 'performs a request to the corresponding API path' do + expect(subject).to eq(Oj.load(response.to_json, symbol_keys: true)) + + expect( + a_request(:put, "https://#{store}.vendhq.com/api/2.1/products/#{product_id}").with(body: params.to_json) + ).to have_been_made + end + end +end From 4ad83af8bbc7eaebf9ec2ec731a225184e11d00d Mon Sep 17 00:00:00 2001 From: Anton Pershakov Date: Wed, 19 Mar 2025 16:05:10 +0000 Subject: [PATCH 2/2] Fix a typo in products 2.1 test --- spec/vend/unit/resources/V21/product_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/vend/unit/resources/V21/product_spec.rb b/spec/vend/unit/resources/V21/product_spec.rb index 0de3df1..a46d092 100644 --- a/spec/vend/unit/resources/V21/product_spec.rb +++ b/spec/vend/unit/resources/V21/product_spec.rb @@ -27,7 +27,7 @@ subject { described_class.update(product_id, params.merge(connection: connection)) } - before dos + before do stub_request( :put, "https://#{store}.vendhq.com/api/2.1/products/#{product_id}",