From 0355a3d25778a47d5062cfbeeef0fde85ea84895 Mon Sep 17 00:00:00 2001 From: Jeff Emminger Date: Tue, 14 Mar 2017 19:20:28 -0400 Subject: [PATCH 1/8] add Customer --- lib/ecwid_api.rb | 1 + lib/ecwid_api/customer.rb | 10 ++++++++++ 2 files changed, 11 insertions(+) create mode 100644 lib/ecwid_api/customer.rb diff --git a/lib/ecwid_api.rb b/lib/ecwid_api.rb index 888bb75..1eb4cf7 100644 --- a/lib/ecwid_api.rb +++ b/lib/ecwid_api.rb @@ -16,6 +16,7 @@ module EcwidApi require_relative "ecwid_api/entity" require_relative "ecwid_api/category" + require_relative "ecwid_api/customer" require_relative "ecwid_api/order" require_relative "ecwid_api/order_item" require_relative "ecwid_api/person" diff --git a/lib/ecwid_api/customer.rb b/lib/ecwid_api/customer.rb new file mode 100644 index 0000000..022464c --- /dev/null +++ b/lib/ecwid_api/customer.rb @@ -0,0 +1,10 @@ +module EcwidApi + class Customer < Entity + self.url_root = "customers" + + ecwid_reader :name, :email, :totalOrderCount, :customerGroupId, :customerGroupName + + ecwid_writer :name, :email + + end +end From 5aeb1a438c807c1d22db44c7696461a87e4d3ba9 Mon Sep 17 00:00:00 2001 From: Jeff Emminger Date: Tue, 14 Mar 2017 19:38:57 -0400 Subject: [PATCH 2/8] add Customer, wip --- lib/ecwid_api/api/customers.rb | 53 ++++++++++++++++++++++++++++++++++ lib/ecwid_api/client.rb | 5 ++-- 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 lib/ecwid_api/api/customers.rb diff --git a/lib/ecwid_api/api/customers.rb b/lib/ecwid_api/api/customers.rb new file mode 100644 index 0000000..bae848e --- /dev/null +++ b/lib/ecwid_api/api/customers.rb @@ -0,0 +1,53 @@ +require_relative "../paged_ecwid_response" + +module EcwidApi + module Api + class Customers < Base + # Public: Get all of the Customer objects for the Ecwid store + # + # Returns an Array of Customer objects + def all(params = {}) + PagedEcwidResponse.new(client, "customers", params) do |customer_hash| + Customer.new(customer_hash, client: client) + end + end + + # Public: Finds a single customer by customer ID + # + # id - an Ecwid customer ID + # + # Returns a Customer object, or nil if one can't be found + def find(id) + response = client.get("customers/#{id}") + if response.success? + Customer.new(response.body, client: client) + end + end + + # Public: Creates a new Customer + # + # params - a Hash + # + # Raises an Error if there is a problem + # + # Returns a Customer object + def create(params) + response = client.post("customers", params) + find(response.body["id"]) + end + + # Public: Updates an existing Customer + # + # id - the Ecwid customer ID + # params - a Hash + # + # Raises an Error if there is a problem + # + # Returns a Customer object + def update(id, params) + client.put("customers/#{id}", params) + find(id) + end + end + end +end diff --git a/lib/ecwid_api/client.rb b/lib/ecwid_api/client.rb index 11402fd..1007757 100644 --- a/lib/ecwid_api/client.rb +++ b/lib/ecwid_api/client.rb @@ -20,7 +20,7 @@ class Client attr_reader :token attr_reader :adapter - attr_reader :connection, :categories, :orders, :products + attr_reader :connection, :categories, :customers, :orders, :products # Public: Initializes a new Client to interact with the API # @@ -42,6 +42,7 @@ def initialize(store_id, token, adapter = Faraday.default_adapter) end @categories = Api::Categories.new(self) + @customers = Api::Customers.new(self) @orders = Api::Orders.new(self) @products = Api::Products.new(self) end @@ -97,4 +98,4 @@ def raise_on_failure(response) end end end -end \ No newline at end of file +end From 0659cc01aaeaafa9bfa8e7c94b7a1deba4abeced Mon Sep 17 00:00:00 2001 From: Jeff Emminger Date: Tue, 14 Mar 2017 20:26:10 -0400 Subject: [PATCH 3/8] added customers tests; fixed deprecations; upgraded rspec --- ecwid_api.gemspec | 2 +- lib/ecwid_api/api.rb | 3 +- lib/ecwid_api/order.rb | 6 +- spec/api/orders_spec.rb | 10 +-- spec/api/products_spec.rb | 20 ----- spec/category_spec.rb | 4 +- spec/client_spec.rb | 6 +- spec/entity_spec.rb | 26 +++---- spec/fixtures/products.json | 141 ---------------------------------- spec/helpers/client.rb | 4 +- spec/oauth_spec.rb | 8 +- spec/order_spec.rb | 22 +++--- spec/paged_enumerator_spec.rb | 2 +- spec/spec_helper.rb | 1 - 14 files changed, 47 insertions(+), 208 deletions(-) delete mode 100644 spec/api/products_spec.rb delete mode 100644 spec/fixtures/products.json diff --git a/ecwid_api.gemspec b/ecwid_api.gemspec index fbde583..e7949a3 100644 --- a/ecwid_api.gemspec +++ b/ecwid_api.gemspec @@ -20,7 +20,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler", "~> 1.5" spec.add_development_dependency "rake", "~> 0" - spec.add_development_dependency "rspec", "~> 2.14", ">= 2.14.1" + spec.add_development_dependency "rspec", "~> 3.5", ">= 3.5" spec.add_dependency "faraday", "~> 0.9.0" spec.add_dependency "faraday_middleware", "~> 0.9.1" diff --git a/lib/ecwid_api/api.rb b/lib/ecwid_api/api.rb index 352d799..e9ed5fd 100644 --- a/lib/ecwid_api/api.rb +++ b/lib/ecwid_api/api.rb @@ -4,6 +4,7 @@ module Api require_relative "api/orders" require_relative "api/products" require_relative "api/categories" + require_relative "api/customers" require_relative "api/product_combinations" end -end \ No newline at end of file +end diff --git a/lib/ecwid_api/order.rb b/lib/ecwid_api/order.rb index c00b539..8f3549e 100644 --- a/lib/ecwid_api/order.rb +++ b/lib/ecwid_api/order.rb @@ -51,7 +51,7 @@ def billing_person # Public: Returns the shipping person # - # If there isn't a shipping_person, then it is assumed to be the + # If there isn't a shipping_person, then it is assumed to be the # billing_person # def shipping_person @@ -66,7 +66,7 @@ def items def fulfillment_status=(status) status = status.to_s.upcase unless VALID_FULFILLMENT_STATUSES.include?(status) - raise Error("#{status} is an invalid fullfillment status") + raise ::StandardError.new("#{status} is an invalid fullfillment status") end super(status) end @@ -85,4 +85,4 @@ def build_shipping_person @shipping_person ||= data["shippingPerson"] && Person.new(data["shippingPerson"]) end end -end \ No newline at end of file +end diff --git a/spec/api/orders_spec.rb b/spec/api/orders_spec.rb index ea531f6..7f29b2e 100644 --- a/spec/api/orders_spec.rb +++ b/spec/api/orders_spec.rb @@ -10,21 +10,21 @@ end it "gets the proper response (see fixtures)" do - subject.all.count.should == 2 + expect(subject.all.count).to be 2 end it "gets EcwidApi::Order types" do - subject.all.all? { |order| order.is_a?(EcwidApi::Order) }.should be_true + expect(subject.all.all? { |order| order.is_a?(EcwidApi::Order) }).to be(true) end end describe "#find" do it "is an `EcwidApi::Order`" do - subject.find(35).is_a?(EcwidApi::Order).should be_true + expect(subject.find(35)).to be_a(EcwidApi::Order) end it "is nil when not found" do - subject.find(404).should be_nil + expect(subject.find(404)).to be_nil end end -end \ No newline at end of file +end diff --git a/spec/api/products_spec.rb b/spec/api/products_spec.rb deleted file mode 100644 index 6567e04..0000000 --- a/spec/api/products_spec.rb +++ /dev/null @@ -1,20 +0,0 @@ -require 'spec_helper' - -describe EcwidApi::Api::Products, faraday: true do - subject { client.products } - - describe "#all" do - it "passes any other paramters through" do - expect(client).to receive(:get).with("products", hash_including(from_date: '1982-05-17')) - subject.all(from_date: '1982-05-17') - end - - it "gets the proper response count (see fixture)" do - subject.all.count.should == 5 - end - - it "gets the proper product (see fixture)" do - subject.all.first.sku.should == "NC53090" - end - end -end \ No newline at end of file diff --git a/spec/category_spec.rb b/spec/category_spec.rb index 66792a1..fa8b342 100644 --- a/spec/category_spec.rb +++ b/spec/category_spec.rb @@ -27,8 +27,8 @@ it "returns nil" do expect(client.categories).to_not receive(:find) - subject.parent.should be_nil + expect(subject.parent).to be_nil end end end -end \ No newline at end of file +end diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 093f6d5..47eb9fe 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -4,7 +4,7 @@ subject { client } describe "#store_url" do - its(:store_url) { "http://app.ecwid.com/api/v3/12345" } + it { is_expected.to have_attributes(store_url: "https://app.ecwid.com/api/v3/12345") } end describe "#get", faraday: true do @@ -15,7 +15,7 @@ end it "returns a Faraday::Response" do - subject.get("categories", parent: 1).is_a?(Faraday::Response).should be_true + expect(subject.get("categories", parent: 1)).to be_a(Faraday::Response) end end -end \ No newline at end of file +end diff --git a/spec/entity_spec.rb b/spec/entity_spec.rb index 883a92d..0b5bc82 100644 --- a/spec/entity_spec.rb +++ b/spec/entity_spec.rb @@ -18,7 +18,7 @@ class EntityUrlSubject < EcwidApi::Entity self.url_root = -> { "parent/#{parent_id}/and" } end -describe EcwidApi::Entity do +RSpec.describe EcwidApi::Entity do let(:data) do { "id" => 123, @@ -34,42 +34,42 @@ class EntityUrlSubject < EcwidApi::Entity subject { EntitySubject.new(data) } describe "::url_root" do - its(:url) { should == "stuff/123" } + it { is_expected.to have_attributes(url: "stuff/123") } context "with a proc" do subject { EntityUrlSubject.new(data) } - its(:url) { should == "parent/456/and/123" } + it { is_expected.to have_attributes(url: "parent/456/and/123") } end end describe "#[]" do it "gets data with a symbol key" do - subject[:id].should == 123 + expect(subject[:id]).to eq 123 end it "gets data with a string key" do - subject["parentId"].should == 456 + expect(subject["parentId"]).to eq 456 end it "get nil for unknown data" do - subject["whatever"].should be_nil + expect(subject["whatever"]).to be_nil end it "gets attributes not revealed by ecwid_reader or ecwid_accessor" do - subject["hidden"].should == "tee hee" + expect(subject["hidden"]).to eq "tee hee" end end describe "overrides" do - its(:override) { should == "UPCASE ME" } + it { is_expected.to have_attributes(override: "UPCASE ME") } end describe "accessors" do describe "::ecwid_reader" do it "makes data accessible with a snake cased method" do - subject.parent_id.should == 456 + expect(subject.parent_id).to eq 456 end it "doesn't have a writer" do @@ -80,7 +80,7 @@ class EntityUrlSubject < EcwidApi::Entity describe "::ecwid_writer" do it "creates a writer method" do subject.write_only = "yee haw!" - subject["writeOnly"].should == "yee haw!" + expect(subject["writeOnly"]).to eq "yee haw!" end it "doesn't have a reader" do @@ -91,13 +91,13 @@ class EntityUrlSubject < EcwidApi::Entity describe "::ecwid_accessor" do it "creates a reader and a writer" do subject.the_status = "MATURE" - subject.the_status.should == "MATURE" + expect(subject.the_status).to eq "MATURE" end end describe "without an accessor" do it "is accessible with []" do - subject[:hidden].should == "tee hee" + expect(subject[:hidden]).to eq "tee hee" end it "doesn't have an access method" do @@ -105,4 +105,4 @@ class EntityUrlSubject < EcwidApi::Entity end end end -end \ No newline at end of file +end diff --git a/spec/fixtures/products.json b/spec/fixtures/products.json deleted file mode 100644 index 48716b9..0000000 --- a/spec/fixtures/products.json +++ /dev/null @@ -1,141 +0,0 @@ -{ - "total": 5, - "count": 5, - "offset": 0, - "limit": 10, - "items": [ - { - "id": 41316136, - "sku": "NC53090", - "smallThumbnailUrl": "http://images-cdn.ecwid.com/images/4881025/242429875.jpg", - "thumbnailUrl": "http://images-cdn.ecwid.com/images/4881025/242429874.jpg", - "imageUrl": "http://images-cdn.ecwid.com/images/4881025/245156912.jpg", - "originalImageUrl": "http://images-cdn.ecwid.com/images/4881025/245156911.jpg", - "unlimited": true, - "inStock": true, - "name": "Book: Therapeutic Hand Splints: A Rational Approach", - "price": 80.95, - "weight": 0.063, - "url": "http://preview.myncmstore.com#!/em-Book-em-Therapeutic-Hand-Splints-A-Rational-Approach/p/41316136", - "created": "2014-09-13 01:45:38 -0700", - "updated": "2014-10-15 09:01:31 -0700", - "productClassId": 0, - "enabled": false, - "description": "

Mechanical and biomechanical considerations of hand splinting.
This book takes an in-depth look into the design, manufacture and application of splints for the upper limb and specifically the hand. The first section, Mechanical Considerations, includes immobilization and mobilization splinting, technical considerations, splinting materials and components. The second section, Biomechanical Considerations, addresses anatomical shape and positioning, volume control and pressure, and biokinematic and biokinetic systems. Includes numerous photographs and illustrations. Soft cover, 214 pages. Written by Paul Van Lede, MS, OT and Griet van Veldhoven, OT. Copyright 1998.

", - "descriptionTruncated": false, - "priceInProductList": 80.95, - "categoryIds": [], - "defaultCategoryId": 0, - "favorites": { - "count": 0, - "displayedCount": "0" - } - }, - { - "id": 41316720, - "sku": "T631", - "smallThumbnailUrl": "http://images-cdn.ecwid.com/images/4881025/242433086.jpg", - "thumbnailUrl": "http://images-cdn.ecwid.com/images/4881025/242433085.jpg", - "imageUrl": "http://images-cdn.ecwid.com/images/4881025/245154405.jpg", - "originalImageUrl": "http://images-cdn.ecwid.com/images/4881025/245154403.jpg", - "unlimited": true, - "inStock": true, - "name": "SportsArt T631", - "price": 4315, - "weight": 595, - "url": "http://preview.myncmstore.com#!/SportsArt-T631/p/41316720", - "created": "2014-09-13 01:46:13 -0700", - "updated": "2014-10-15 08:58:41 -0700", - "productClassId": 0, - "enabled": false, - "description": "

4.0 hp ECO-POWR™ motor uses up to 32% less energy without compromising power.

Motor - 4 hp DC

Speed range - .1 to 12 mph (.16 to 19km/h)

Incline range - 0 to 15%

Deck Cushioning - 4-way reversible phenolic, My-Flex™

Display/Feedback - Dot Matrix and LEDs

Programs - 3 Interval (1:1, 1:2, 2:2), Random (x10,000), 2 Glute (30 min, and 45 min), 3 Hill, HRC Cardio, HRC Weight Loss, Quick Star, Manual/Track, Zone Trainer™, Fitness Tests (Bruce, Gerkin, WFI, Army, Marines)

Medical rails - Optional

Maximum user weight - 450 lbs. (181kg)

Unit dimensions - 83.5\" x 38.6\" x 56.3\" (212 x 98 x 143)

Unit weight - 297 lbs. (135kg)

Warranty - Lifetime frame, 5 years parts, Dedicated Power Supply

Power requirements - 120 VAC, 60 Hz, 12A, 1440W; Plug UL NEMA 5-15; Cord 14 AWG CSA-approved; Fuse 12A (6x30 mm), 100-120V, Fast blow

Shipping - Call for freight quote

", - "descriptionTruncated": false, - "priceInProductList": 4315, - "categoryIds": [], - "defaultCategoryId": 0, - "favorites": { - "count": 0, - "displayedCount": "0" - } - }, - { - "id": 41316721, - "sku": "T611", - "smallThumbnailUrl": "http://images-cdn.ecwid.com/images/4881025/242433090.jpg", - "thumbnailUrl": "http://images-cdn.ecwid.com/images/4881025/242433089.jpg", - "imageUrl": "http://images-cdn.ecwid.com/images/4881025/245154466.jpg", - "originalImageUrl": "http://images-cdn.ecwid.com/images/4881025/245154465.jpg", - "unlimited": true, - "inStock": true, - "name": "SportsArt T611", - "price": 2595, - "weight": 0, - "url": "http://preview.myncmstore.com#!/SportsArt-T611/p/41316721", - "created": "2014-09-13 01:46:13 -0700", - "updated": "2014-10-15 08:58:41 -0700", - "productClassId": 0, - "enabled": false, - "description": "

Powerful 3.0 hp DC motor with a large 20\" x 58\" running surface.

Motor: 3 hp DC

Speed range: .1 to 12 mph (.16 to 19km/h)

Incline range: -3 to 15%

Deck Cushioning: 4-way reversible phenolic, My-Flex™

Display/Feedback: Tri-Color Dot Matrix and LEDs

Programs: Programmable Interval, Random (x10,000), 2 Glute (30 min, and 45 min), 3 Hill, HRC Cardio, HRC Weight Loss, Quick Star, Manual/Track, Zone Trainer™

Medical rails: Optional

Maximum user weight: 400 lbs. (181kg)

Unit dimensions: 77.5\" x 38\" x 53\" (197 x 97 x 35cm)

Unit weight: 229 lbs. (104kg)

Warranty: Lifetime frame, 5 years parts, Dedicated Power Supply

Power requirements: 120 VAC, 60 Hz, 12A, 1440W; Plug UL NEMA 5-15; Cord 14 AWG CSA-approved; Fuse 12A (6x30 mm), 100-120V, Fast blow

Shipping: Call for freight quote

", - "descriptionTruncated": false, - "priceInProductList": 2595, - "categoryIds": [], - "defaultCategoryId": 0, - "favorites": { - "count": 0, - "displayedCount": "0" - } - }, - { - "id": 41316486, - "sku": "NC89048", - "smallThumbnailUrl": "http://images-cdn.ecwid.com/images/4881025/242432163.jpg", - "thumbnailUrl": "http://images-cdn.ecwid.com/images/4881025/242432162.jpg", - "imageUrl": "http://images-cdn.ecwid.com/images/4881025/245153078.jpg", - "originalImageUrl": "http://images-cdn.ecwid.com/images/4881025/245153077.jpg", - "unlimited": true, - "inStock": true, - "name": "DVD: Clinical Kinesio Taping", - "price": 41.95, - "weight": 0.192, - "url": "http://preview.myncmstore.com#!/DVD-Clinical-Kinesio-Taping/p/41316486", - "created": "2014-09-13 01:45:57 -0700", - "updated": "2014-10-15 08:56:50 -0700", - "productClassId": 0, - "enabled": false, - "description": "

Introduction, explanation and application of the Kinesio Taping Method.

Certified Kinesio Taping Intstructor Jim Wallis, MS, ATC demonstrates step-by-step application of Kinesio Taping Techniques.  Copyright 2000.

", - "descriptionTruncated": false, - "priceInProductList": 41.95, - "categoryIds": [], - "defaultCategoryId": 0, - "favorites": { - "count": 0, - "displayedCount": "0" - } - }, - { - "id": 43428452, - "sku": "META-2961", - "unlimited": true, - "inStock": true, - "name": "English Pantyhose Aid", - "price": 0, - "weight": 0, - "url": "http://preview.myncmstore.com#!/English-Pantyhose-Aid/p/43428452", - "created": "2014-10-21 14:44:31 -0700", - "updated": "2014-10-21 14:44:31 -0700", - "productClassId": 0, - "enabled": true, - "description": "

test

", - "descriptionTruncated": false, - "priceInProductList": 0, - "categoryIds": [ - 11056311 - ], - "defaultCategoryId": 11056311, - "favorites": { - "count": 0, - "displayedCount": "0" - } - } - ] -} \ No newline at end of file diff --git a/spec/helpers/client.rb b/spec/helpers/client.rb index 5490bab..0e7e1f2 100644 --- a/spec/helpers/client.rb +++ b/spec/helpers/client.rb @@ -7,7 +7,7 @@ def client end def fixtures - %w(categories category orders products) + %w(categories customers category orders products) end def faraday_stubs @@ -29,4 +29,4 @@ def faraday end end end -end \ No newline at end of file +end diff --git a/spec/oauth_spec.rb b/spec/oauth_spec.rb index 1a9ac1c..c7057b9 100644 --- a/spec/oauth_spec.rb +++ b/spec/oauth_spec.rb @@ -10,7 +10,7 @@ end end - its(:oauth_url) { should == "https://my.ecwid.com/api/oauth/authorize?client_id=client_id&scope=scope&response_type=code&redirect_uri=https%3A%2F%2Fexample.com%2Foauth" } + it { is_expected.to have_attributes(oauth_url: "https://my.ecwid.com/api/oauth/authorize?client_id=client_id&scope=scope&response_type=code&redirect_uri=https%3A%2F%2Fexample.com%2Foauth") } describe "#access_token(code)" do let(:response) do @@ -30,11 +30,11 @@ end it "returns an object that has the access_token" do - subject.access_token("code").access_token.should == "the_token" + expect(subject.access_token("code").access_token).to eq "the_token" end it "returns an object that has the store_id" do - subject.access_token("code").store_id.should == "12345" + expect(subject.access_token("code").store_id).to eq "12345" end end -end \ No newline at end of file +end diff --git a/spec/order_spec.rb b/spec/order_spec.rb index e279e9f..8085e19 100644 --- a/spec/order_spec.rb +++ b/spec/order_spec.rb @@ -17,45 +17,45 @@ let(:shipping_person) { nil } - its(:id) { should == 123 } + it { is_expected.to have_attributes(id: 123) } describe "#billing_person" do - its(:billing_person) { should be_a(EcwidApi::Person) } + it { expect(subject.billing_person).to be_a(EcwidApi::Person) } it "has the correct data" do - subject.billing_person.name.should == "John Doe" + expect(subject.billing_person.name).to eq "John Doe" end end describe "#shipping_person" do - its(:shipping_person) { should be_a(EcwidApi::Person) } + it { expect(subject.shipping_person).to be_a(EcwidApi::Person) } context "without a shipping person" do let(:shipping_person) { nil } - its(:shipping_person) { should == subject.billing_person } + it { expect(subject.shipping_person).to be(subject.billing_person) } end context "with a shipping person" do let(:shipping_person) { {"name" => "Jane Doe"} } it "has the correct data" do - subject.shipping_person.name.should == "Jane Doe" + expect(subject.shipping_person.name).to eq "Jane Doe" end end end describe "#items" do it "has the correct number of items" do - subject.items.size.should == 1 + expect(subject.items.size).to eq 1 end it "has the correct data" do - subject.items.first.sku.should == "112233" + expect(subject.items.first.sku).to eq "112233" end end describe "#fulfillment_status=" do it "raises an error with an invalid status" do - expect { subject.fulfillment_status = :stuff }.to raise_error + expect { subject.fulfillment_status = :stuff }.to raise_error(StandardError) end it "doesn't raise an error with a valid status" do @@ -65,7 +65,7 @@ describe "#fulfillment_status" do it "is symbolized" do - subject.fulfillment_status.should == :awaiting_processing + expect(subject.fulfillment_status).to eq :awaiting_processing end end -end \ No newline at end of file +end diff --git a/spec/paged_enumerator_spec.rb b/spec/paged_enumerator_spec.rb index 0e9c8af..a7c937f 100644 --- a/spec/paged_enumerator_spec.rb +++ b/spec/paged_enumerator_spec.rb @@ -26,7 +26,7 @@ end it "contains the whole result set" do - subject.to_a.should == %w(1 2 3 4 5 6) + expect(subject.to_a).to eq %w(1 2 3 4 5 6) end it "iterates over each response once" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d675a9a..89c660c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -9,7 +9,6 @@ require "helpers/client" RSpec.configure do |config| - config.treat_symbols_as_metadata_keys_with_true_values = true config.run_all_when_everything_filtered = true config.filter_run :focus From 5bef20a5427ae840fa301d27d1cc59eb730390ad Mon Sep 17 00:00:00 2001 From: Jeff Emminger Date: Sat, 29 Apr 2017 12:11:48 -0400 Subject: [PATCH 4/8] adding customers and product_types --- lib/ecwid_api.rb | 3 +- lib/ecwid_api/api.rb | 5 ++- lib/ecwid_api/api/product_types.rb | 53 +++++++++++++++++++++++++ lib/ecwid_api/client.rb | 11 ++--- lib/ecwid_api/product_type.rb | 17 ++++++++ lib/ecwid_api/product_type_attribute.rb | 28 +++++++++++++ spec/api/customers_spec.rb | 20 ++++++++++ spec/api/product_types_spec.rb | 20 ++++++++++ spec/fixtures/customers.json | 48 ++++++++++++++++++++++ spec/fixtures/product_types.json | 50 +++++++++++++++++++++++ spec/helpers/client.rb | 2 +- 11 files changed, 248 insertions(+), 9 deletions(-) create mode 100644 lib/ecwid_api/api/product_types.rb create mode 100644 lib/ecwid_api/product_type.rb create mode 100644 lib/ecwid_api/product_type_attribute.rb create mode 100644 spec/api/customers_spec.rb create mode 100644 spec/api/product_types_spec.rb create mode 100644 spec/fixtures/customers.json create mode 100644 spec/fixtures/product_types.json diff --git a/lib/ecwid_api.rb b/lib/ecwid_api.rb index 1eb4cf7..0465d99 100644 --- a/lib/ecwid_api.rb +++ b/lib/ecwid_api.rb @@ -21,6 +21,7 @@ module EcwidApi require_relative "ecwid_api/order_item" require_relative "ecwid_api/person" require_relative "ecwid_api/product_combination" - require_relative "ecwid_api/product" + require_relative "ecwid_api/product_type" + require_relative "ecwid_api/product_type_attribute" end diff --git a/lib/ecwid_api/api.rb b/lib/ecwid_api/api.rb index e9ed5fd..6ab87fb 100644 --- a/lib/ecwid_api/api.rb +++ b/lib/ecwid_api/api.rb @@ -1,10 +1,11 @@ module EcwidApi module Api require_relative "api/base" - require_relative "api/orders" - require_relative "api/products" require_relative "api/categories" require_relative "api/customers" + require_relative "api/orders" require_relative "api/product_combinations" + require_relative "api/product_types" + require_relative "api/products" end end diff --git a/lib/ecwid_api/api/product_types.rb b/lib/ecwid_api/api/product_types.rb new file mode 100644 index 0000000..8527cb0 --- /dev/null +++ b/lib/ecwid_api/api/product_types.rb @@ -0,0 +1,53 @@ +require_relative "../paged_ecwid_response" + +module EcwidApi + module Api + class ProductTypes < Base + # Public: Get all of the ProductType objects for the Ecwid store + # + # Returns an Array of ProductType objects + def all(params = {}) + PagedEcwidResponse.new(client, "product_types", params) do |product_type_hash| + ProductType.new(product_type_hash, client: client) + end + end + + # Public: Finds a single product_type by product_type ID + # + # id - an Ecwid product_type ID + # + # Returns a ProductType object, or nil if one can't be found + def find(id) + response = client.get("product_types/#{id}") + if response.success? + ProductType.new(response.body, client: client) + end + end + + # Public: Creates a new ProductType + # + # params - a Hash + # + # Raises an Error if there is a problem + # + # Returns a ProductType object + def create(params) + response = client.post("product_types", params) + find(response.body["id"]) + end + + # Public: Updates an existing ProductType + # + # id - the Ecwid product_type ID + # params - a Hash + # + # Raises an Error if there is a problem + # + # Returns a ProductType object + def update(id, params) + client.put("product_types/#{id}", params) + find(id) + end + end + end +end diff --git a/lib/ecwid_api/client.rb b/lib/ecwid_api/client.rb index 1007757..30c9372 100644 --- a/lib/ecwid_api/client.rb +++ b/lib/ecwid_api/client.rb @@ -20,7 +20,7 @@ class Client attr_reader :token attr_reader :adapter - attr_reader :connection, :categories, :customers, :orders, :products + attr_reader :connection, :categories, :customers, :orders, :products, :product_types # Public: Initializes a new Client to interact with the API # @@ -41,10 +41,11 @@ def initialize(store_id, token, adapter = Faraday.default_adapter) conn.adapter adapter end - @categories = Api::Categories.new(self) - @customers = Api::Customers.new(self) - @orders = Api::Orders.new(self) - @products = Api::Products.new(self) + @categories = Api::Categories.new(self) + @customers = Api::Customers.new(self) + @orders = Api::Orders.new(self) + @products = Api::Products.new(self) + @product_types = Api::ProductTypes.new(self) end # Public: The URL of the API for the Ecwid Store diff --git a/lib/ecwid_api/product_type.rb b/lib/ecwid_api/product_type.rb new file mode 100644 index 0000000..85d6a8d --- /dev/null +++ b/lib/ecwid_api/product_type.rb @@ -0,0 +1,17 @@ +module EcwidApi + class ProductType < Entity + self.url_root = "classes" + + ecwid_reader :id, :name, :googleTaxonomy, :attributes + + ecwid_writer :name, :attributes + + + # Public: Returns a Array of `ProductTypeAttribute` objects + def attributes + @attributes ||= data["attributes"].map { |attribute| ProductTypeAttribute.new(attribute) } + end + + + end +end diff --git a/lib/ecwid_api/product_type_attribute.rb b/lib/ecwid_api/product_type_attribute.rb new file mode 100644 index 0000000..9f53a9f --- /dev/null +++ b/lib/ecwid_api/product_type_attribute.rb @@ -0,0 +1,28 @@ +module EcwidApi + class ProductTypeAttribute < Entity + + ecwid_reader :id, :name, :type, :show + + VALID_TYPES = %w(CUSTOM UPC BRAND GENDER AGE_GROUP COLOR SIZE PRICE_PER_UNIT UNITS_IN_PRODUCT) + VALID_SHOWS = %w(NOTSHOW DESCR PRICE) + + + def type=(type_type) + type_type = type_type.to_s.upcase + unless VALID_TYPES.include?(type_type) + raise ::StandardError.new("#{type_type} is an invalid 'type'") + end + super(type_type) + end + + + def show=(show_type) + show_type = show_type.to_s.upcase + unless VALID_SHOWS.include?(show_type) + raise ::StandardError.new("#{show_type} is an invalid 'show'") + end + super(show_type) + end + + end +end diff --git a/spec/api/customers_spec.rb b/spec/api/customers_spec.rb new file mode 100644 index 0000000..b232e67 --- /dev/null +++ b/spec/api/customers_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe EcwidApi::Api::Customers, faraday: true do + subject { client.customers } + + describe "#all" do + it "passes any other parameters through" do + expect(client).to receive(:get).with("customers", hash_including(from_date: '1982-05-17')) + subject.all(from_date: '1982-05-17') + end + + it "gets the proper response count (see fixture)" do + expect(subject.all.count).to eq 5 + end + + it "gets the proper customer (see fixture)" do + expect(subject.all.first.name).to eq "Abe Doe" + end + end +end diff --git a/spec/api/product_types_spec.rb b/spec/api/product_types_spec.rb new file mode 100644 index 0000000..3270298 --- /dev/null +++ b/spec/api/product_types_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe EcwidApi::Api::ProductTypes, faraday: true do + subject { client.product_types } + + describe "#all" do + it "passes any other parameters through" do + expect(client).to receive(:get).with("product_types", hash_including(from_date: '1982-05-17')) + subject.all(from_date: '1982-05-17') + end + + it "gets the proper response count (see fixture)" do + expect(subject.all.count).to eq 2 + end + + it "gets the proper product_type (see fixture)" do + expect(subject.all.first.name).to eq "Foo" + end + end +end diff --git a/spec/fixtures/customers.json b/spec/fixtures/customers.json new file mode 100644 index 0000000..30b8e15 --- /dev/null +++ b/spec/fixtures/customers.json @@ -0,0 +1,48 @@ +{ + "total": 5, + "count": 5, + "offset": 0, + "limit": 100, + "items": [ + { + "id": 1, + "name": "Abe Doe", + "email": "abe@example.com", + "totalOrderCount": 1, + "customerGroupId": 0, + "customerGroupName": "General" + }, + { + "id": 2, + "name": "Bob Doe", + "email": "bob@example.com", + "totalOrderCount": 2, + "customerGroupId": 0, + "customerGroupName": "General" + }, + { + "id": 3, + "name": "Chris Doe", + "email": "chris@example.com", + "totalOrderCount": 3, + "customerGroupId": 0, + "customerGroupName": "General" + }, + { + "id": 4, + "name": "Debby Doe", + "email": "debby@example.com", + "totalOrderCount": 0, + "customerGroupId": 0, + "customerGroupName": "General" + }, + { + "id": 5, + "name": "Ellen Doe", + "email": "ellen@example.com", + "totalOrderCount": 0, + "customerGroupId": 0, + "customerGroupName": "General" + } + ] +} diff --git a/spec/fixtures/product_types.json b/spec/fixtures/product_types.json new file mode 100644 index 0000000..b08a165 --- /dev/null +++ b/spec/fixtures/product_types.json @@ -0,0 +1,50 @@ +{ + "total": 2, + "count": 2, + "offset": 0, + "limit": 100, + "items": [ + { + "id": 1, + "name": "Foo", + "googleTaxonomy": "This > That > Other > Foos", + "attributes": [{ + "id": 111, + "name": "UPC", + "type": "UPC", + "show": "DESCR" + }, + { + "id": 222, + "name": "Brand", + "type": "BRAND", + "show": "DESCR" + } + ] + }, + { + "id": 2, + "name": "Bar", + "googleTaxonomy": "This > That > Other > Bars", + "attributes": [{ + "id": 333, + "name": "UPC", + "type": "UPC", + "show": "DESCR" + }, + { + "id": 444, + "name": "Brand", + "type": "BRAND", + "show": "DESCR" + }, + { + "id": 555, + "name": "Blah", + "type": "BRAND", + "show": "NOTSHOW" + } + ] + } + ] +} diff --git a/spec/helpers/client.rb b/spec/helpers/client.rb index 0e7e1f2..4b43d71 100644 --- a/spec/helpers/client.rb +++ b/spec/helpers/client.rb @@ -7,7 +7,7 @@ def client end def fixtures - %w(categories customers category orders products) + %w(categories customers category orders products product_types) end def faraday_stubs From 3b40057e94de92f6d84e2aef883e9c0ab0db3148 Mon Sep 17 00:00:00 2001 From: Jeff Emminger Date: Sat, 29 Apr 2017 14:20:24 -0400 Subject: [PATCH 5/8] api name fix, test fixes --- lib/ecwid_api/api/product_types.rb | 8 ++++---- lib/ecwid_api/product_type.rb | 1 + spec/api/product_types_spec.rb | 2 +- spec/fixtures/{product_types.json => classes.json} | 0 spec/helpers/client.rb | 4 +++- 5 files changed, 9 insertions(+), 6 deletions(-) rename spec/fixtures/{product_types.json => classes.json} (100%) diff --git a/lib/ecwid_api/api/product_types.rb b/lib/ecwid_api/api/product_types.rb index 8527cb0..cf210df 100644 --- a/lib/ecwid_api/api/product_types.rb +++ b/lib/ecwid_api/api/product_types.rb @@ -7,7 +7,7 @@ class ProductTypes < Base # # Returns an Array of ProductType objects def all(params = {}) - PagedEcwidResponse.new(client, "product_types", params) do |product_type_hash| + PagedEcwidResponse.new(client, "classes", params) do |product_type_hash| ProductType.new(product_type_hash, client: client) end end @@ -18,7 +18,7 @@ def all(params = {}) # # Returns a ProductType object, or nil if one can't be found def find(id) - response = client.get("product_types/#{id}") + response = client.get("classes/#{id}") if response.success? ProductType.new(response.body, client: client) end @@ -32,7 +32,7 @@ def find(id) # # Returns a ProductType object def create(params) - response = client.post("product_types", params) + response = client.post("classes", params) find(response.body["id"]) end @@ -45,7 +45,7 @@ def create(params) # # Returns a ProductType object def update(id, params) - client.put("product_types/#{id}", params) + client.put("classes/#{id}", params) find(id) end end diff --git a/lib/ecwid_api/product_type.rb b/lib/ecwid_api/product_type.rb index 85d6a8d..253d383 100644 --- a/lib/ecwid_api/product_type.rb +++ b/lib/ecwid_api/product_type.rb @@ -1,5 +1,6 @@ module EcwidApi class ProductType < Entity + self.url_root = "classes" ecwid_reader :id, :name, :googleTaxonomy, :attributes diff --git a/spec/api/product_types_spec.rb b/spec/api/product_types_spec.rb index 3270298..30f4f05 100644 --- a/spec/api/product_types_spec.rb +++ b/spec/api/product_types_spec.rb @@ -5,7 +5,7 @@ describe "#all" do it "passes any other parameters through" do - expect(client).to receive(:get).with("product_types", hash_including(from_date: '1982-05-17')) + expect(client).to receive(:get).with("classes", hash_including(from_date: '1982-05-17')) subject.all(from_date: '1982-05-17') end diff --git a/spec/fixtures/product_types.json b/spec/fixtures/classes.json similarity index 100% rename from spec/fixtures/product_types.json rename to spec/fixtures/classes.json diff --git a/spec/helpers/client.rb b/spec/helpers/client.rb index 4b43d71..40e70e4 100644 --- a/spec/helpers/client.rb +++ b/spec/helpers/client.rb @@ -7,7 +7,7 @@ def client end def fixtures - %w(categories customers category orders products product_types) + %w(categories customers category orders products classes) end def faraday_stubs @@ -18,6 +18,8 @@ def faraday_stubs stub.get("/categories/5") { [200, {"Content-Type" => "application/json"}, File.read("spec/fixtures/category.json") ] } stub.get("/orders/35") { [200, {"Content-Type" => "application/json"}, File.read("spec/fixtures/order.json") ] } stub.get("/orders/404") { [404, {"Content-Type" => "application/json"}, nil ] } + stub.get("/classes/1") { [200, {"Content-Type" => "application/json"}, File.read("spec/fixtures/order.json") ] } + stub.get("/classes/404") { [404, {"Content-Type" => "application/json"}, nil ] } end end From 5ffafde0a2c2cf1905384ae8bcd6e4e6f82a6097 Mon Sep 17 00:00:00 2001 From: Jeff Emminger Date: Sat, 29 Apr 2017 15:23:20 -0400 Subject: [PATCH 6/8] test fixes --- .gitignore | 1 + lib/ecwid_api/api/product_types.rb | 7 +++-- lib/ecwid_api/unpaged_ecwid_response.rb | 38 +++++++++++++++++++++++++ lib/ecwid_api/version.rb | 2 +- spec/api/product_types_spec.rb | 6 ++-- spec/fixtures/classes.json | 14 +++------ spec/helpers/client.rb | 2 +- 7 files changed, 53 insertions(+), 17 deletions(-) create mode 100644 lib/ecwid_api/unpaged_ecwid_response.rb diff --git a/.gitignore b/.gitignore index d87d4be..a955cfa 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ spec/reports test/tmp test/version_tmp tmp +.DS_Store diff --git a/lib/ecwid_api/api/product_types.rb b/lib/ecwid_api/api/product_types.rb index cf210df..4993865 100644 --- a/lib/ecwid_api/api/product_types.rb +++ b/lib/ecwid_api/api/product_types.rb @@ -1,4 +1,4 @@ -require_relative "../paged_ecwid_response" +require_relative "../unpaged_ecwid_response" module EcwidApi module Api @@ -6,8 +6,11 @@ class ProductTypes < Base # Public: Get all of the ProductType objects for the Ecwid store # # Returns an Array of ProductType objects + # NOTE: This endpoint does not behave like other Ecwid endpoints in that + # it does not return paged results. It simply returns every + # result in an array, without a wrapper with an "items" property. def all(params = {}) - PagedEcwidResponse.new(client, "classes", params) do |product_type_hash| + UnpagedEcwidResponse.new(client, "classes") do |product_type_hash| ProductType.new(product_type_hash, client: client) end end diff --git a/lib/ecwid_api/unpaged_ecwid_response.rb b/lib/ecwid_api/unpaged_ecwid_response.rb new file mode 100644 index 0000000..32fb221 --- /dev/null +++ b/lib/ecwid_api/unpaged_ecwid_response.rb @@ -0,0 +1,38 @@ + +# Public: Presents an Ecwid response as an Array +# +# Example +# +# response = UnpagedEcwidResponse.new(client, "products", priceFrom: 10) do |product_hash| +# Product.new(product_hash, click: client) +# end +# +# response.each do |product| +# # do stuff the the product +# end +# +module EcwidApi + class UnpagedEcwidResponse + include Enumerable + extend Forwardable + + def_delegator :@records, :each + + # Public: Initialize a new UnpagedEcwidResponse + # + # client - an EcwidApi::Client + # path - a String that is the path to retrieve from the client + # params - a Hash of parameters to pass along with the request + # &block - a Block that processes each item returned in the Response + # + def initialize(client, path, params = {}, &block) + block ||= Proc.new { |item| item } + @records = [] + + response = client.get(path, params) + response.body.each do |item| + @records << block.call(item) + end + end + end +end diff --git a/lib/ecwid_api/version.rb b/lib/ecwid_api/version.rb index c0b8cd8..4505820 100644 --- a/lib/ecwid_api/version.rb +++ b/lib/ecwid_api/version.rb @@ -1,3 +1,3 @@ module EcwidApi - VERSION = "0.2.2" + VERSION = "0.2.4" end diff --git a/spec/api/product_types_spec.rb b/spec/api/product_types_spec.rb index 30f4f05..bd78d65 100644 --- a/spec/api/product_types_spec.rb +++ b/spec/api/product_types_spec.rb @@ -4,9 +4,9 @@ subject { client.product_types } describe "#all" do - it "passes any other parameters through" do - expect(client).to receive(:get).with("classes", hash_including(from_date: '1982-05-17')) - subject.all(from_date: '1982-05-17') + it "returns an array" do + expect(client).to receive(:get).with("classes", {}).and_call_original + subject.all end it "gets the proper response count (see fixture)" do diff --git a/spec/fixtures/classes.json b/spec/fixtures/classes.json index b08a165..9322171 100644 --- a/spec/fixtures/classes.json +++ b/spec/fixtures/classes.json @@ -1,10 +1,5 @@ -{ - "total": 2, - "count": 2, - "offset": 0, - "limit": 100, - "items": [ - { +[ + { "id": 1, "name": "Foo", "googleTaxonomy": "This > That > Other > Foos", @@ -22,7 +17,7 @@ } ] }, - { + { "id": 2, "name": "Bar", "googleTaxonomy": "This > That > Other > Bars", @@ -46,5 +41,4 @@ } ] } - ] -} +] diff --git a/spec/helpers/client.rb b/spec/helpers/client.rb index 40e70e4..938300f 100644 --- a/spec/helpers/client.rb +++ b/spec/helpers/client.rb @@ -18,7 +18,7 @@ def faraday_stubs stub.get("/categories/5") { [200, {"Content-Type" => "application/json"}, File.read("spec/fixtures/category.json") ] } stub.get("/orders/35") { [200, {"Content-Type" => "application/json"}, File.read("spec/fixtures/order.json") ] } stub.get("/orders/404") { [404, {"Content-Type" => "application/json"}, nil ] } - stub.get("/classes/1") { [200, {"Content-Type" => "application/json"}, File.read("spec/fixtures/order.json") ] } + stub.get("/classes/1") { [200, {"Content-Type" => "application/json"}, File.read("spec/fixtures/classes.json") ] } stub.get("/classes/404") { [404, {"Content-Type" => "application/json"}, nil ] } end end From d93e1179bbfd46a0c7e4310908e063ef52c761e0 Mon Sep 17 00:00:00 2001 From: Jeff Emminger Date: Sat, 29 Apr 2017 15:39:02 -0400 Subject: [PATCH 7/8] forward all of enumerable --- lib/ecwid_api/unpaged_ecwid_response.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ecwid_api/unpaged_ecwid_response.rb b/lib/ecwid_api/unpaged_ecwid_response.rb index 32fb221..ff7d8ff 100644 --- a/lib/ecwid_api/unpaged_ecwid_response.rb +++ b/lib/ecwid_api/unpaged_ecwid_response.rb @@ -16,7 +16,7 @@ class UnpagedEcwidResponse include Enumerable extend Forwardable - def_delegator :@records, :each + def_delegators :@records, *Enumerable.instance_methods # Public: Initialize a new UnpagedEcwidResponse # From 0e1ff54ba61366d68601d47c434b836703aff0e3 Mon Sep 17 00:00:00 2001 From: Jeff Emminger Date: Thu, 24 Aug 2017 17:12:46 -0400 Subject: [PATCH 8/8] test --- Rakefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Rakefile b/Rakefile index 2995527..809eb56 100644 --- a/Rakefile +++ b/Rakefile @@ -1 +1,2 @@ require "bundler/gem_tasks" +