Skip to content
This repository has been archived by the owner on Dec 7, 2020. It is now read-only.

Add Customer to api client #7

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ spec/reports
test/tmp
test/version_tmp
tmp
.DS_Store
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
require "bundler/gem_tasks"

2 changes: 1 addition & 1 deletion ecwid_api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion lib/ecwid_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ 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"
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
8 changes: 5 additions & 3 deletions lib/ecwid_api/api.rb
Original file line number Diff line number Diff line change
@@ -1,9 +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
end
53 changes: 53 additions & 0 deletions lib/ecwid_api/api/customers.rb
Original file line number Diff line number Diff line change
@@ -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
56 changes: 56 additions & 0 deletions lib/ecwid_api/api/product_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require_relative "../unpaged_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
# 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 = {})
UnpagedEcwidResponse.new(client, "classes") 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("classes/#{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("classes", 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("classes/#{id}", params)
find(id)
end
end
end
end
12 changes: 7 additions & 5 deletions lib/ecwid_api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, :product_types

# Public: Initializes a new Client to interact with the API
#
Expand All @@ -41,9 +41,11 @@ def initialize(store_id, token, adapter = Faraday.default_adapter)
conn.adapter adapter
end

@categories = Api::Categories.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
Expand Down Expand Up @@ -97,4 +99,4 @@ def raise_on_failure(response)
end
end
end
end
end
10 changes: 10 additions & 0 deletions lib/ecwid_api/customer.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions lib/ecwid_api/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -85,4 +85,4 @@ def build_shipping_person
@shipping_person ||= data["shippingPerson"] && Person.new(data["shippingPerson"])
end
end
end
end
18 changes: 18 additions & 0 deletions lib/ecwid_api/product_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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
28 changes: 28 additions & 0 deletions lib/ecwid_api/product_type_attribute.rb
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions lib/ecwid_api/unpaged_ecwid_response.rb
Original file line number Diff line number Diff line change
@@ -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_delegators :@records, *Enumerable.instance_methods

# 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
2 changes: 1 addition & 1 deletion lib/ecwid_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module EcwidApi
VERSION = "0.2.2"
VERSION = "0.2.4"
end
20 changes: 20 additions & 0 deletions spec/api/customers_spec.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 5 additions & 5 deletions spec/api/orders_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
end
20 changes: 20 additions & 0 deletions spec/api/product_types_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'spec_helper'

describe EcwidApi::Api::ProductTypes, faraday: true do
subject { client.product_types }

describe "#all" do
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
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
Loading