ecwid_api 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +8 -0
  3. data/Gemfile +2 -0
  4. data/README.md +228 -144
  5. data/ecwid_api.gemspec +1 -0
  6. data/lib/ecwid_api/api/base.rb +17 -0
  7. data/lib/ecwid_api/api/categories.rb +57 -0
  8. data/lib/ecwid_api/api/orders.rb +36 -0
  9. data/lib/ecwid_api/api/product_combinations.rb +51 -0
  10. data/lib/ecwid_api/api/products.rb +64 -0
  11. data/lib/ecwid_api/api.rb +31 -0
  12. data/lib/ecwid_api/category.rb +59 -4
  13. data/lib/ecwid_api/client.rb +65 -94
  14. data/lib/ecwid_api/entity.rb +138 -26
  15. data/lib/ecwid_api/error.rb +12 -2
  16. data/lib/ecwid_api/o_auth.rb +106 -0
  17. data/lib/ecwid_api/order.rb +82 -0
  18. data/lib/ecwid_api/order_item.rb +17 -0
  19. data/lib/ecwid_api/paged_ecwid_response.rb +55 -0
  20. data/lib/ecwid_api/paged_enumerator.rb +66 -0
  21. data/lib/ecwid_api/person.rb +7 -0
  22. data/lib/ecwid_api/product.rb +77 -0
  23. data/lib/ecwid_api/product_combination.rb +34 -0
  24. data/lib/ecwid_api/version.rb +1 -1
  25. data/lib/ecwid_api.rb +25 -39
  26. data/lib/ext/string.rb +12 -4
  27. data/spec/api/categories_spec.rb +31 -0
  28. data/spec/api/orders_spec.rb +30 -0
  29. data/spec/api/products_spec.rb +20 -0
  30. data/spec/category_spec.rb +33 -38
  31. data/spec/client_spec.rb +20 -48
  32. data/spec/entity_spec.rb +90 -5
  33. data/spec/fixtures/categories.json +28 -22
  34. data/spec/fixtures/order.json +162 -0
  35. data/spec/fixtures/orders.json +302 -0
  36. data/spec/fixtures/products.json +141 -0
  37. data/spec/helpers/client.rb +32 -0
  38. data/spec/oauth_spec.rb +40 -0
  39. data/spec/order_item_spec.rb +12 -0
  40. data/spec/order_spec.rb +71 -0
  41. data/spec/paged_enumerator_spec.rb +38 -0
  42. data/spec/spec_helper.rb +24 -23
  43. metadata +53 -9
  44. data/lib/ecwid_api/category_api.rb +0 -62
  45. data/spec/category_api_spec.rb +0 -36
  46. data/spec/ecwid_api_spec.rb +0 -15
  47. data/spec/helpers/faraday.rb +0 -30
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 67727922f8d2fc5ab19839abda31f4ae709b67e8
4
- data.tar.gz: 8e78ebe2a0dc6e83da2a9235e08a57704a04b9c4
3
+ metadata.gz: 115d13f16d0747c45335873d314215544f80bd4c
4
+ data.tar.gz: c5e1874c0963ec608800587e42a9a19d2d648949
5
5
  SHA512:
6
- metadata.gz: f8e57e93e2c5fc0e4109955f3a1aefe311e5cf9edd868a8fb0a5feb1bca59e06eb81d07161b2f53af4c426b9eff96eacc9a4bee59adfb79c7bd4db3b82bbbe9b
7
- data.tar.gz: 5065b8ba1deff18ee542f71c68ec6b478859e19fc1ca079a419cd46dac3295c6df934c9befc467d80b46fa178b6cbbb327bf82cd7a5f309e8d060a364b521509
6
+ metadata.gz: 3d057bae684a112491485aa7338c54680ed9b961a069b4f28d7ca58d325219218e8ca0c13eb971b73565a5eb803913186ef430e4ef26712bd73d9a74fb25e723
7
+ data.tar.gz: e769e8ca8535b17e3f15b1df4ce239a01d2c7fc2acc8b193d177f98910c7f6d6d21ba0a0bc06e65a88e36b7845a1e9ac4f9daea613a56d34c1123c08a1bf3de4
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - "2.1.0"
5
+ - "2.0.0"
6
+ - "1.9.3"
7
+
8
+ script: bundle exec rspec spec
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in ecwid_api.gemspec
4
4
  gemspec
5
+
6
+ # gem "faraday", git: "https://github.com/davidbiehl/faraday.git", branch: "preserve_raw"
data/README.md CHANGED
@@ -1,144 +1,228 @@
1
- # EcwidApi
2
-
3
- A gem to interface with the Ecwid REST APIs.
4
-
5
- [![Code Climate](https://codeclimate.com/github/davidbiehl/ecwid_api.png)](https://codeclimate.com/github/davidbiehl/ecwid_api)
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- gem 'ecwid_api'
12
-
13
- And then execute:
14
-
15
- $ bundle
16
-
17
- Or install it yourself as:
18
-
19
- $ gem install ecwid_api
20
-
21
- ## Usage
22
-
23
- ### Configure an new Client
24
-
25
- A `Client` will interface with a single Ecwid store. The `store_id` will need
26
- to be configured for each new `Client`.
27
-
28
- require 'ecwid_api'
29
-
30
- client = EcwidApi::Client.new do |config|
31
- config.store_id = '12345' # your Ecwid Store ID
32
- config.url = 'https://app.ecwid.com/api/v1' # default
33
- config.order_secret_key = 'ORDER_SECRET_KEY'
34
- config.product_secret_key = 'PRODUCT_SECRET_KEY'
35
- end
36
-
37
- ## APIs
38
-
39
- ### Category API
40
-
41
- The Category API will allow you to access the categories for an Ecwid store.
42
- An instance of the Category API is available on the client.
43
-
44
- api = client.categories
45
- # => #<EcwidApi::CategoryApi>
46
-
47
- api.all
48
- # Returns an Array of all of the `EcwidApi::Category` objects
49
-
50
- api.root
51
- # Returns an Array of the top-level `EcwidApi::Category` objects for the
52
- # store
53
-
54
- api.find(123)
55
- # Returns the `EcwidApi::Category` with an ID of 123
56
-
57
- #### EcwidApi::Category Objects
58
-
59
- The properties of an `EcwidApi::Category` object can be accessed using the `[]`
60
- method, or with special snake_cased helper methods.
61
-
62
- cat = client.categories.find(123)
63
- # An example response from the API
64
- # {
65
- # "id": 123,
66
- # "parentId": 456,
67
- # "name": "Special Category"
68
- # }
69
-
70
- cat[:id] # Access with a Symbol
71
- # => 123
72
-
73
- cat["parentId"] # Access with a String (case sensitive)
74
- # => 456
75
-
76
- cat.parent_id # Access with a snake_case method
77
- # => 456
78
-
79
- Each `EcwidApi::Category` also has methods to find any sub-categories, and the
80
- parent category, if there is one.
81
-
82
- cat.parent
83
- # Returns the parent `EcwidApi::Category`
84
-
85
- cat.sub_categories
86
- # Returns an Array of `EcwidApi::Category`
87
-
88
- ### Making Ad-Hoc Requests with the Client
89
-
90
- To make a request, simply call the `#get` method on the client passing in the
91
- relative path and any parameters it requires.
92
- For example, to get some categories:
93
-
94
- # GET https://app.ecwid.com/api/v1/[STORE-ID]/categories?parent=1
95
-
96
- client.get("categories", parent: 1)
97
-
98
- # => #<Faraday::Response>
99
-
100
- The `Client` is responsible for making raw requests, which is why it returns
101
- a `Faraday::Response`. The JSON parsing middleware is also active on the Faraday
102
- connection, so calling `Faraday::Response#body` will return a Hash of the parsed
103
- JSON.
104
-
105
- ### Ecwid API Documentation
106
-
107
- The [Ecwid API documentation](http://kb.ecwid.com/w/page/25232810/API)
108
- should give you a good idea of what is possible to retreive. It also defines
109
- which properties are available on each of the entities it provies. Please note
110
- that resources requiring the secret keys will be inaccessible until we implement
111
- that feature.
112
-
113
- ## Contributing
114
-
115
- 1. Fork it ( http://github.com/davidbiehl/ecwid_api/fork )
116
- 2. Create your feature branch (`git checkout -b my-new-feature`)
117
- 3. Commit your changes (`git commit -am 'Add some feature'`)
118
- 4. Push to the branch (`git push origin my-new-feature`)
119
- 5. Create new Pull Request
120
-
121
- ## License
122
-
123
- The MIT License (MIT)
124
-
125
- Copyright (c) 2014 David Biehl
126
-
127
- Permission is hereby granted, free of charge, to any person obtaining a copy
128
- of this software and associated documentation files (the "Software"), to deal
129
- in the Software without restriction, including without limitation the rights
130
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
131
- copies of the Software, and to permit persons to whom the Software is
132
- furnished to do so, subject to the following conditions:
133
-
134
- The above copyright notice and this permission notice shall be included in
135
- all copies or substantial portions of the Software.
136
-
137
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
138
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
139
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
140
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
141
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
142
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
143
- THE SOFTWARE.
144
-
1
+ # EcwidApi
2
+
3
+ A gem to interface with the Ecwid REST APIs.
4
+
5
+ [![Code Climate](https://codeclimate.com/github/davidbiehl/ecwid_api.png)](https://codeclimate.com/github/davidbiehl/ecwid_api)
6
+ [![Build Status](https://travis-ci.org/davidbiehl/ecwid_api.svg?branch=master)](https://travis-ci.org/davidbiehl/ecwid_api)
7
+
8
+ ## API v3 Warning!
9
+
10
+ This is for the latest version of the API, also known as v3, which is currently
11
+ in closed beta! The (incomplete) v1 API is still available on the
12
+ [api-v1 branch](https://github.com/davidbiehl/ecwid_api/tree/api-v1).
13
+
14
+ To participate in the beta, please contact Ecwid and they will give you the
15
+ information necessary to configure and authorize your application with OAuth2.
16
+
17
+ [Ecwid's API Documentation](http://api.ecwid.com) will be an important reference
18
+ in order to understand what their API is capable of.
19
+
20
+ ## Installation
21
+
22
+ Add this line to your application's Gemfile:
23
+
24
+ gem 'ecwid_api'
25
+
26
+ And then execute:
27
+
28
+ $ bundle
29
+
30
+ Or install it yourself as:
31
+
32
+ $ gem install ecwid_api
33
+
34
+ ## Usage
35
+
36
+ ### Get Authorized with OAuth2
37
+
38
+ Ecwid API v3 uses OAuth2 to authorize 3rd party apps to use the API with a
39
+ store. The `EcwidApi::OAuth` class helps facilitate this process. Once you
40
+ get setup with a `client_id` and `client_secret` from Ecwid, configure a new
41
+ instance like so:
42
+
43
+ @auth = EcwidApi::OAuth.new do |config|
44
+ config.client_id = "the client id"
45
+ config.client_secret = "the client secret (shh...)"
46
+ config.request_uri = "https://example.com/oauth"
47
+ config.scope = "the_permissions_i_want"
48
+ end
49
+
50
+ The `#oauth_url` method will provide the URL that the user needs to go to
51
+ to authorize your application with their store. It can be used in Rails like so:
52
+
53
+ link_to @auth.oauth_url, "Click here to Authorize this Groovy App!"
54
+
55
+ When the user authorizes your app, they will be redirected to the `request_uri`
56
+ with a `code` parameter in the query string.
57
+ Just send that code to the `#access_token` method to complete the authorization
58
+ and get your `access_token` and `store_id`.
59
+
60
+ # https://example.com/oauth?code=super_secret_temporary_code
61
+
62
+ token = @auth.access_token(params[:code])
63
+
64
+ token.access_token # the token for the Client
65
+ token.store_id # the store_id for the Client
66
+
67
+ ### Configure an new Client
68
+
69
+ A `Client` will interface with a single Ecwid store. The `store_id` and OAuth
70
+ `access_token` will need to be provided to the client.
71
+
72
+ require 'ecwid_api'
73
+
74
+ client = EcwidApi::Client.new(store_id, access_token)
75
+
76
+ ## The APIs
77
+
78
+ ### Entities
79
+
80
+ Instead of returning raw JSON from the API, there are Entities that will help
81
+ you work with the data. The [Ecwid API](http://api.ecwid.com)
82
+ will give you all of the fields that are available for every entity. Our
83
+ Entities will give you access to the data with the `[]` method, or a snake_case
84
+ version of the property name. For example, with an `EcwidApi::Category` the
85
+ following would be possible:
86
+
87
+ cat = client.categories.find(123)
88
+ # An example response from the API
89
+ # {
90
+ # "id": 123,
91
+ # "parentId": 456,
92
+ # "name": "Special Category"
93
+ # }
94
+
95
+ cat[:id] # Access with a Symbol
96
+ # => 123
97
+
98
+ cat["parentId"] # Access with a String (case sensitive)
99
+ # => 456
100
+
101
+ cat.parent_id # Access with a snake_case method
102
+ # => 456
103
+
104
+ ### Category API
105
+
106
+ The Category API will allow you to access the categories for an Ecwid store.
107
+ An instance of the Category API is available on the client.
108
+
109
+ api = client.categories
110
+ # => #<EcwidApi::Api::Categories>
111
+
112
+ api.all
113
+ # Returns an Array of all of the `EcwidApi::Category` objects
114
+
115
+ api.root
116
+ # Returns an Array of the top-level `EcwidApi::Category` objects for the
117
+ # store
118
+
119
+ api.find(123)
120
+ # Returns the `EcwidApi::Category` with an ID of 123
121
+
122
+ #### EcwidApi::Category Entities
123
+
124
+ Each `EcwidApi::Category` has methods to find sub-categories and the
125
+ parent category, if there is one.
126
+
127
+ cat.parent
128
+ # Returns the parent `EcwidApi::Category`
129
+
130
+ cat.sub_categories
131
+ # Returns an Array of `EcwidApi::Category`
132
+
133
+ ### Order API
134
+
135
+ The Order API will allow you to access the orders that have been placed in an
136
+ Ecwid store. An instance of the Order API is available to the client
137
+
138
+ api = client.orders
139
+
140
+ api.all
141
+ # Returns a `PagedEnumerator` containing all of the orders for the store
142
+
143
+ api.all({date: "1982-05-17"})
144
+ # Paremters can be passed as a Hash.
145
+ # See http://kb.ecwid.com/w/page/43697230/Order%20API#Parameters for
146
+ # a list of available parameters
147
+
148
+ api.find(123)
149
+ # Returns an `EcwidApi::Order` object for order 123
150
+
151
+ #### EcwidApi::Order Entities
152
+
153
+ There are a few helper methods on the `EcwidApi::Order` that assist in accessing
154
+ related Entities.
155
+
156
+ order.billing_person
157
+ # Returns a EcwidApi::Person
158
+
159
+ order.shipping_person
160
+ # Returns an EcwidApi::Person
161
+
162
+ order.items
163
+ # Returns an Array of EcwidApi::OrderItem objects
164
+
165
+ The fulfillment status and shipping tracking code can also be updated for an
166
+ `EcwidApi::Order` object.
167
+
168
+ order.fulfillment_status = :processing
169
+ order.shipping_tracking_code = "1Z1234567890"
170
+ order.save
171
+
172
+ ### Making Ad-Hoc Requests with the Client
173
+
174
+ To make a request, simply call the `#get` method on the client passing in the
175
+ relative path and any parameters it requires.
176
+ For example, to get some categories:
177
+
178
+ # GET https://app.ecwid.com/api/v3/[STORE-ID]/categories?parent=1
179
+
180
+ client.get("categories", parent: 1)
181
+
182
+ # => #<Faraday::Response>
183
+
184
+ The `Client` is responsible for making raw requests, which is why it returns
185
+ a `Faraday::Response`. The JSON parsing middleware is also active on the Faraday
186
+ connection, so calling `Faraday::Response#body` will return a Hash of the parsed
187
+ JSON.
188
+
189
+ ### Ecwid API Documentation
190
+
191
+ The [Ecwid API documentation](http://api.ecwid.com)
192
+ should give you a good idea of what is possible to retreive. It also defines
193
+ which properties are available on each of the entities it provies. Please note
194
+ that resources requiring the secret keys will be inaccessible until we implement
195
+ that feature.
196
+
197
+ ## Contributing
198
+
199
+ 1. Fork it ( http://github.com/davidbiehl/ecwid_api/fork )
200
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
201
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
202
+ 4. Push to the branch (`git push origin my-new-feature`)
203
+ 5. Create new Pull Request
204
+
205
+ ## License
206
+
207
+ The MIT License (MIT)
208
+
209
+ Copyright (c) 2014 David Biehl
210
+
211
+ Permission is hereby granted, free of charge, to any person obtaining a copy
212
+ of this software and associated documentation files (the "Software"), to deal
213
+ in the Software without restriction, including without limitation the rights
214
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
215
+ copies of the Software, and to permit persons to whom the Software is
216
+ furnished to do so, subject to the following conditions:
217
+
218
+ The above copyright notice and this permission notice shall be included in
219
+ all copies or substantial portions of the Software.
220
+
221
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
222
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
223
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
224
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
225
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
226
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
227
+ THE SOFTWARE.
228
+
data/ecwid_api.gemspec CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.add_development_dependency "bundler", "~> 1.5"
21
21
  spec.add_development_dependency "rake"
22
22
  spec.add_development_dependency "rspec", "~> 2.14.1"
23
+ spec.add_development_dependency "pry"
23
24
 
24
25
  spec.add_dependency "faraday", "~> 0.9.0"
25
26
  spec.add_dependency "faraday_middleware", "~> 0.9.1"
@@ -0,0 +1,17 @@
1
+ module EcwidApi
2
+ module Api
3
+ # Internal: A base class for common API functionality
4
+ class Base
5
+ include Api
6
+
7
+ # Public: Initializes a new EcwidApi::CategoryApi
8
+ #
9
+ # client - The EcwidApi::Client to use with the API
10
+ #
11
+ def initialize(client)
12
+ @client = client
13
+ raise Error.new("The client cannot be nil") unless client
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,57 @@
1
+ require_relative "../paged_ecwid_response"
2
+
3
+ module EcwidApi
4
+ module Api
5
+ class Categories < Base
6
+ # Public: Returns all of the sub-categories for a given category
7
+ #
8
+ # See: http://kb.ecwid.com/w/page/25285101/Product%20API#RESTAPIMethodcategories
9
+ #
10
+ # parent - The Category ID of the parent category. If the parent is 0 then
11
+ # a list of the root categories will be returned. If the parent is
12
+ # nil, then all of the categories will be returned
13
+ #
14
+ # Returns an array of EcwidApi::Category objects
15
+ def all(params = {})
16
+ PagedEcwidResponse.new(client, "categories", params) do |category_hash|
17
+ Category.new(category_hash, client: client)
18
+ end.sort_by(&:order_by)
19
+ end
20
+
21
+ # Public: Returns an Array of the root level EcwidApi::Category objects
22
+ def root(params = {})
23
+ all(params.merge(parent: 0))
24
+ end
25
+
26
+ # Public: Returns a single EcwidApi::Category
27
+ #
28
+ # See: http://kb.ecwid.com/w/page/25285101/Product%20API#RESTAPIMethodcategory
29
+ #
30
+ # category_id - A Category ID to get
31
+ #
32
+ # Returns an EcwidApi::Category, or nil if it can't be found
33
+ def find(id)
34
+ response = client.get("categories/#{id}")
35
+
36
+ if response.success?
37
+ Category.new(response.body, client: client)
38
+ else
39
+ nil
40
+ end
41
+ rescue Zlib::BufError
42
+ nil
43
+ end
44
+
45
+ # Public: Creates a new Category
46
+ #
47
+ # params - a Hash of API keys and their corresponding values
48
+ #
49
+ # Returns a new Category entity
50
+ def create(params)
51
+ response = client.post("categories", params)
52
+
53
+ raise_on_failure(response) { |response| find(response.body["id"]) }
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,36 @@
1
+ require_relative "../paged_ecwid_response"
2
+
3
+ module EcwidApi
4
+ module Api
5
+ # Public: This is the Ecwid API for Orders. It abstracts the end-points
6
+ # of the Ecwid API that deal with orders.
7
+ class Orders < Base
8
+ # Public: Gets Orders from the Ecwid API
9
+ #
10
+ # params - optional parameters that can be used to filter the request.
11
+ # For a list of params, please refer to the API documentation:
12
+ # http://kb.ecwid.com/w/page/43697230/Order%20API
13
+ # Note that the limit and offset parameters will be overridden
14
+ # since all orders will be returned and enumerated
15
+ #
16
+ # Returns a PagedEnumerator of `EcwidApi::Order` objects
17
+ def all(params = {})
18
+ PagedEcwidResponse.new(client, "orders", params) do |order_hash|
19
+ Order.new(order_hash, client: client)
20
+ end
21
+ end
22
+
23
+ # Public: Finds a an Order given an Ecwid order_number
24
+ #
25
+ # order_number - an Integer that is the Ecwid Order number
26
+ #
27
+ # Returns an EcwidApi::Order if found, nil if not
28
+ def find(order_number)
29
+ response = client.get("orders/#{order_number}")
30
+ if response.success?
31
+ Order.new(response.body, client: client)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,51 @@
1
+ module EcwidApi
2
+ module Api
3
+ class ProductCombinations < Base
4
+ include Api
5
+ include Enumerable
6
+
7
+ attr_reader :product
8
+
9
+ def initialize(product, client)
10
+ @product = product
11
+ super(client)
12
+ end
13
+
14
+ def all
15
+ response = client.get("products/#{product.id}/combinations")
16
+
17
+ if response.success?
18
+ response.body.map do |data|
19
+ ProductCombination.new(data, client: client, product: product)
20
+ end
21
+ end
22
+ end
23
+
24
+ def each(&block)
25
+ all = self.all || []
26
+
27
+ all.each(&block)
28
+ end
29
+
30
+ def find(id)
31
+ response = client.get("products/#{product.id}/combinations/#{id}")
32
+
33
+ if response.success?
34
+ ProductCombination.new(response.body, product: product, client: client)
35
+ end
36
+ end
37
+
38
+ def create(params)
39
+ response = client.post("products/#{product.id}/combinations", params)
40
+
41
+ raise_on_failure(response) { find(response.body["id"]) }
42
+ end
43
+
44
+ def delete_all!
45
+ client.delete("products/#{product.id}/combinations").tap do |response|
46
+ raise_on_failure(response)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,64 @@
1
+ require_relative "../paged_ecwid_response"
2
+
3
+ module EcwidApi
4
+ module Api
5
+ class Products < Base
6
+ # Public: Get all of the Product objects for the Ecwid store
7
+ #
8
+ # Returns an Array of Product objects
9
+ def all(params = {})
10
+ PagedEcwidResponse.new(client, "products", params) do |product_hash|
11
+ Product.new(product_hash, client: client)
12
+ end
13
+ end
14
+
15
+ # Public: Finds a single product by product ID
16
+ #
17
+ # id - an Ecwid product ID
18
+ #
19
+ # Returns a Product object, or nil if one can't be found
20
+ def find(id)
21
+ response = client.get("products/#{id}")
22
+ if response.success?
23
+ Product.new(response.body, client: client)
24
+ end
25
+ end
26
+
27
+ # Public: Finds a single Product by SKU
28
+ #
29
+ # sku - a SKU of a product
30
+ #
31
+ # Returns a Product object, or nil if one can't be found
32
+ def find_by_sku(sku)
33
+ all(keyword: sku).find { |product| product[:sku] == sku }
34
+ end
35
+
36
+ # Public: Creates a new Product
37
+ #
38
+ # params - a Hash
39
+ #
40
+ # Raises an Error if there is a problem
41
+ #
42
+ # Returns a Product object
43
+ def create(params)
44
+ response = client.post("products", params)
45
+
46
+ raise_on_failure(response) {|response| find(response.body["id"]) }
47
+ end
48
+
49
+ # Public: Updates an existing Product
50
+ #
51
+ # id - the Ecwid product ID
52
+ # params - a Hash
53
+ #
54
+ # Raises an Error if there is a problem
55
+ #
56
+ # Returns a Product object
57
+ def update(id, params)
58
+ response = client.put("products/#{id}", params)
59
+
60
+ raise_on_failure(response) { find(id) }
61
+ end
62
+ end
63
+ end
64
+ end