ecwid_api 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +0 -0
- data/.rspec +0 -0
- data/.travis.yml +0 -0
- data/Gemfile +0 -2
- data/LICENSE.txt +0 -0
- data/README.md +228 -228
- data/Rakefile +0 -0
- data/ecwid_api.gemspec +4 -4
- data/lib/ecwid_api.rb +25 -25
- data/lib/ecwid_api/api.rb +8 -30
- data/lib/ecwid_api/api/base.rb +17 -16
- data/lib/ecwid_api/api/categories.rb +55 -56
- data/lib/ecwid_api/api/orders.rb +36 -36
- data/lib/ecwid_api/api/product_combinations.rb +2 -5
- data/lib/ecwid_api/api/products.rb +61 -63
- data/lib/ecwid_api/category.rb +1 -7
- data/lib/ecwid_api/client.rb +99 -65
- data/lib/ecwid_api/entity.rb +4 -6
- data/lib/ecwid_api/error.rb +12 -12
- data/lib/ecwid_api/o_auth.rb +105 -105
- data/lib/ecwid_api/order.rb +0 -0
- data/lib/ecwid_api/order_item.rb +0 -0
- data/lib/ecwid_api/paged_ecwid_response.rb +0 -0
- data/lib/ecwid_api/paged_enumerator.rb +0 -0
- data/lib/ecwid_api/person.rb +0 -0
- data/lib/ecwid_api/product.rb +3 -15
- data/lib/ecwid_api/product_combination.rb +1 -5
- data/lib/ecwid_api/version.rb +1 -1
- data/lib/ext/string.rb +12 -12
- data/spec/api/categories_spec.rb +30 -30
- data/spec/api/orders_spec.rb +29 -29
- data/spec/api/products_spec.rb +0 -0
- data/spec/category_spec.rb +33 -33
- data/spec/client_spec.rb +20 -20
- data/spec/entity_spec.rb +0 -0
- data/spec/fixtures/categories.json +0 -0
- data/spec/fixtures/category.json +0 -0
- data/spec/fixtures/order.json +0 -0
- data/spec/fixtures/orders.json +0 -0
- data/spec/fixtures/products.json +0 -0
- data/spec/helpers/client.rb +31 -31
- data/spec/oauth_spec.rb +39 -39
- data/spec/order_item_spec.rb +11 -11
- data/spec/order_spec.rb +0 -0
- data/spec/paged_enumerator_spec.rb +0 -0
- data/spec/spec_helper.rb +24 -24
- metadata +26 -34
data/lib/ecwid_api/category.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
require "open-uri"
|
2
|
-
|
3
1
|
module EcwidApi
|
4
2
|
class Category < Entity
|
5
3
|
self.url_root = "categories"
|
@@ -59,11 +57,7 @@ module EcwidApi
|
|
59
57
|
#
|
60
58
|
# Returns a Faraday::Response object
|
61
59
|
def upload_image!(filename)
|
62
|
-
client.
|
63
|
-
req.body = open(filename).read
|
64
|
-
end.tap do |response|
|
65
|
-
raise_on_failure(response)
|
66
|
-
end
|
60
|
+
client.post_image("categories/#{id}/image", filename)
|
67
61
|
end
|
68
62
|
end
|
69
63
|
end
|
data/lib/ecwid_api/client.rb
CHANGED
@@ -1,66 +1,100 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
attr_reader :
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
#
|
26
|
-
#
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
@orders
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
1
|
+
require "open-uri"
|
2
|
+
|
3
|
+
module EcwidApi
|
4
|
+
# Public: Client objects manage the connection and interface to a single Ecwid
|
5
|
+
# store.
|
6
|
+
#
|
7
|
+
# Examples
|
8
|
+
#
|
9
|
+
# client = EcwidApi::Client.new(store_id: '12345', token: 'the access_token')
|
10
|
+
# client.get "/products"
|
11
|
+
#
|
12
|
+
class Client
|
13
|
+
extend Forwardable
|
14
|
+
|
15
|
+
# The default base URL for the Ecwid API
|
16
|
+
DEFAULT_URL = "https://app.ecwid.com/api/v3"
|
17
|
+
|
18
|
+
# Public: Returns the Ecwid Store ID
|
19
|
+
attr_reader :store_id
|
20
|
+
attr_reader :token
|
21
|
+
attr_reader :adapter
|
22
|
+
|
23
|
+
attr_reader :connection, :categories, :orders, :products
|
24
|
+
|
25
|
+
# Public: Initializes a new Client to interact with the API
|
26
|
+
#
|
27
|
+
# store_id - the Ecwid store_id to interact with
|
28
|
+
# token - the authorization token provided by oAuth. See the
|
29
|
+
# Authentication class
|
30
|
+
#
|
31
|
+
def initialize(store_id, token, adapter = Faraday.default_adapter)
|
32
|
+
@store_id, @token, @adapter = store_id, token, adapter
|
33
|
+
|
34
|
+
@connection = Faraday.new store_url do |conn|
|
35
|
+
conn.request :oauth2, token, param_name: :token
|
36
|
+
conn.request :json
|
37
|
+
|
38
|
+
conn.response :json, content_type: /\bjson$/
|
39
|
+
conn.response :logger
|
40
|
+
|
41
|
+
conn.adapter adapter
|
42
|
+
end
|
43
|
+
|
44
|
+
@categories = Api::Categories.new(self)
|
45
|
+
@orders = Api::Orders.new(self)
|
46
|
+
@products = Api::Products.new(self)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Public: The URL of the API for the Ecwid Store
|
50
|
+
def store_url
|
51
|
+
"#{DEFAULT_URL}/#{store_id}"
|
52
|
+
end
|
53
|
+
|
54
|
+
def_delegators :connection, :get
|
55
|
+
|
56
|
+
def post(*args, &block)
|
57
|
+
raise_on_failure connection.post(*args, &block)
|
58
|
+
end
|
59
|
+
|
60
|
+
def put(*args, &block)
|
61
|
+
raise_on_failure connection.put(*args, &block)
|
62
|
+
end
|
63
|
+
|
64
|
+
def delete(*args, &block)
|
65
|
+
raise_on_failure connection.delete(*args, &block)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Public: A helper method for POSTing an image
|
69
|
+
#
|
70
|
+
# url - the URL to POST the image to
|
71
|
+
# filename - the path or URL to the image to upload
|
72
|
+
#
|
73
|
+
# Returns a Faraday::Response
|
74
|
+
#
|
75
|
+
def post_image(url, filename)
|
76
|
+
post(url) do |req|
|
77
|
+
req.body = open(filename).read
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
# Private: Raises a ResponseError if the request failed
|
84
|
+
#
|
85
|
+
# response - a Faraday::Response object that is the result of a request
|
86
|
+
#
|
87
|
+
# Raises ResponseError if the request wasn't successful
|
88
|
+
#
|
89
|
+
# Returns the original response if the request was successful
|
90
|
+
#
|
91
|
+
#
|
92
|
+
def raise_on_failure(response)
|
93
|
+
if response.success?
|
94
|
+
response
|
95
|
+
else
|
96
|
+
raise ResponseError.new(response)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
66
100
|
end
|
data/lib/ecwid_api/entity.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
module EcwidApi
|
2
2
|
class Entity
|
3
|
-
include Api
|
4
|
-
|
5
3
|
# Private: Gets the Hash of data
|
6
4
|
attr_reader :data
|
7
5
|
protected :data
|
8
6
|
|
7
|
+
attr_reader :client
|
8
|
+
private :client
|
9
|
+
|
9
10
|
class << self
|
10
11
|
attr_accessor :url_root
|
11
12
|
|
@@ -154,7 +155,6 @@ module EcwidApi
|
|
154
155
|
def save
|
155
156
|
unless @new_data.empty?
|
156
157
|
client.put(url, @new_data).tap do |response|
|
157
|
-
raise_on_failure(response)
|
158
158
|
@data.merge!(@new_data)
|
159
159
|
@new_data.clear
|
160
160
|
end
|
@@ -163,9 +163,7 @@ module EcwidApi
|
|
163
163
|
|
164
164
|
# Public: Destroys the Entity
|
165
165
|
def destroy!
|
166
|
-
client.delete(url)
|
167
|
-
raise_on_failure(response)
|
168
|
-
end
|
166
|
+
client.delete(url)
|
169
167
|
end
|
170
168
|
|
171
169
|
def to_hash
|
data/lib/ecwid_api/error.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
-
module EcwidApi
|
2
|
-
class Error < StandardError; end;
|
3
|
-
|
4
|
-
class ResponseError < Error
|
5
|
-
def initialize(response)
|
6
|
-
if response.respond_to?(:reason_phrase)
|
7
|
-
super("#{response.reason_phrase} (#{response.status})")
|
8
|
-
else
|
9
|
-
super "The Ecwid API responded with an error (#{response.status})"
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
1
|
+
module EcwidApi
|
2
|
+
class Error < StandardError; end;
|
3
|
+
|
4
|
+
class ResponseError < Error
|
5
|
+
def initialize(response)
|
6
|
+
if response.respond_to?(:reason_phrase)
|
7
|
+
super("#{response.reason_phrase} (#{response.status})")
|
8
|
+
else
|
9
|
+
super "The Ecwid API responded with an error (#{response.status})"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
13
|
end
|
data/lib/ecwid_api/o_auth.rb
CHANGED
@@ -1,106 +1,106 @@
|
|
1
|
-
require "cgi"
|
2
|
-
require "ostruct"
|
3
|
-
|
4
|
-
module EcwidApi
|
5
|
-
# Public: Authentication objects manage OAuth authentication with an Ecwid
|
6
|
-
# store.
|
7
|
-
#
|
8
|
-
# Examples
|
9
|
-
#
|
10
|
-
# app = EcwidApi::Authentication.new do |config|
|
11
|
-
# # see initialize for configuration
|
12
|
-
# end
|
13
|
-
#
|
14
|
-
# app.oauth_url # send the user here to authorize the app
|
15
|
-
#
|
16
|
-
# token = app.access_token(params[:code]) # this is the code they provide
|
17
|
-
# # to the redirect_uri
|
18
|
-
# token.access_token
|
19
|
-
# token.store_id # these are what you need to access the API
|
20
|
-
#
|
21
|
-
class OAuth
|
22
|
-
CONFIG = %w(client_id client_secret scope redirect_uri)
|
23
|
-
attr_accessor *CONFIG
|
24
|
-
|
25
|
-
# Public: Initializes a new Ecwid Authentication for OAuth
|
26
|
-
#
|
27
|
-
# Examples
|
28
|
-
#
|
29
|
-
# app = EcwidApi::Authentication.new do |config|
|
30
|
-
# config.client_id = "some client id"
|
31
|
-
# config.client_secret = "some client secret"
|
32
|
-
# config.scope "this_is_what_i_want_to_do oh_and_that_too"
|
33
|
-
# config.redirect_uri = "https://example.com/oauth"
|
34
|
-
# end
|
35
|
-
#
|
36
|
-
def initialize
|
37
|
-
yield(self) if block_given?
|
38
|
-
CONFIG.each do |method|
|
39
|
-
raise Error.new("#{method} is required to initialize a new EcwidApi::Authentication") unless send(method)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
# Public: The URL for OAuth authorization.
|
44
|
-
#
|
45
|
-
# This is the URL that the user will need to go to to authorize the app
|
46
|
-
# with the Ecwid store.
|
47
|
-
#
|
48
|
-
def oauth_url
|
49
|
-
"https://my.ecwid.com/api/oauth/authorize?" + oauth_query
|
50
|
-
end
|
51
|
-
|
52
|
-
# Public: Obtain the access token in order to use the API
|
53
|
-
#
|
54
|
-
# code - the temporary code obtained from the authorization callback
|
55
|
-
#
|
56
|
-
# Examples
|
57
|
-
#
|
58
|
-
# token = app.access_token(params[:code])
|
59
|
-
# token.access_token # the access token that authenticates each API request
|
60
|
-
# token.store_id # the authenticated Ecwid store_id
|
61
|
-
#
|
62
|
-
# Returns an OpenStruct which responds with the information needed to
|
63
|
-
# access the API for a store.
|
64
|
-
def access_token(code)
|
65
|
-
response = connection.post("/api/oauth/token",
|
66
|
-
client_id: client_id,
|
67
|
-
client_secret: client_secret,
|
68
|
-
code: code,
|
69
|
-
redirect_uri: redirect_uri,
|
70
|
-
grant_type: "authorization_code"
|
71
|
-
)
|
72
|
-
|
73
|
-
if response.success?
|
74
|
-
OpenStruct.new(response.body)
|
75
|
-
else
|
76
|
-
raise Error.new(response.body["error_description"])
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
private
|
81
|
-
|
82
|
-
# Private: The query parameters for the OAuth authorization request
|
83
|
-
#
|
84
|
-
# Returns a String of query parameters
|
85
|
-
def oauth_query
|
86
|
-
{
|
87
|
-
client_id: client_id,
|
88
|
-
scope: scope,
|
89
|
-
response_type: "code",
|
90
|
-
redirect_uri: redirect_uri
|
91
|
-
}.map do |key, val|
|
92
|
-
"#{CGI.escape(key.to_s)}=#{CGI.escape(val.to_s)}"
|
93
|
-
end.join(?&)
|
94
|
-
end
|
95
|
-
|
96
|
-
# Private: Returns a connection for obtaining an access token from Ecwid
|
97
|
-
#
|
98
|
-
def connection
|
99
|
-
@connection ||= Faraday.new "https://my.ecwid.com" do |conn|
|
100
|
-
conn.request :url_encoded
|
101
|
-
conn.response :json, content_type: /\bjson$/
|
102
|
-
conn.adapter Faraday.default_adapter
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
1
|
+
require "cgi"
|
2
|
+
require "ostruct"
|
3
|
+
|
4
|
+
module EcwidApi
|
5
|
+
# Public: Authentication objects manage OAuth authentication with an Ecwid
|
6
|
+
# store.
|
7
|
+
#
|
8
|
+
# Examples
|
9
|
+
#
|
10
|
+
# app = EcwidApi::Authentication.new do |config|
|
11
|
+
# # see initialize for configuration
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# app.oauth_url # send the user here to authorize the app
|
15
|
+
#
|
16
|
+
# token = app.access_token(params[:code]) # this is the code they provide
|
17
|
+
# # to the redirect_uri
|
18
|
+
# token.access_token
|
19
|
+
# token.store_id # these are what you need to access the API
|
20
|
+
#
|
21
|
+
class OAuth
|
22
|
+
CONFIG = %w(client_id client_secret scope redirect_uri)
|
23
|
+
attr_accessor *CONFIG
|
24
|
+
|
25
|
+
# Public: Initializes a new Ecwid Authentication for OAuth
|
26
|
+
#
|
27
|
+
# Examples
|
28
|
+
#
|
29
|
+
# app = EcwidApi::Authentication.new do |config|
|
30
|
+
# config.client_id = "some client id"
|
31
|
+
# config.client_secret = "some client secret"
|
32
|
+
# config.scope "this_is_what_i_want_to_do oh_and_that_too"
|
33
|
+
# config.redirect_uri = "https://example.com/oauth"
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
def initialize
|
37
|
+
yield(self) if block_given?
|
38
|
+
CONFIG.each do |method|
|
39
|
+
raise Error.new("#{method} is required to initialize a new EcwidApi::Authentication") unless send(method)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Public: The URL for OAuth authorization.
|
44
|
+
#
|
45
|
+
# This is the URL that the user will need to go to to authorize the app
|
46
|
+
# with the Ecwid store.
|
47
|
+
#
|
48
|
+
def oauth_url
|
49
|
+
"https://my.ecwid.com/api/oauth/authorize?" + oauth_query
|
50
|
+
end
|
51
|
+
|
52
|
+
# Public: Obtain the access token in order to use the API
|
53
|
+
#
|
54
|
+
# code - the temporary code obtained from the authorization callback
|
55
|
+
#
|
56
|
+
# Examples
|
57
|
+
#
|
58
|
+
# token = app.access_token(params[:code])
|
59
|
+
# token.access_token # the access token that authenticates each API request
|
60
|
+
# token.store_id # the authenticated Ecwid store_id
|
61
|
+
#
|
62
|
+
# Returns an OpenStruct which responds with the information needed to
|
63
|
+
# access the API for a store.
|
64
|
+
def access_token(code)
|
65
|
+
response = connection.post("/api/oauth/token",
|
66
|
+
client_id: client_id,
|
67
|
+
client_secret: client_secret,
|
68
|
+
code: code,
|
69
|
+
redirect_uri: redirect_uri,
|
70
|
+
grant_type: "authorization_code"
|
71
|
+
)
|
72
|
+
|
73
|
+
if response.success?
|
74
|
+
OpenStruct.new(response.body)
|
75
|
+
else
|
76
|
+
raise Error.new(response.body["error_description"])
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
# Private: The query parameters for the OAuth authorization request
|
83
|
+
#
|
84
|
+
# Returns a String of query parameters
|
85
|
+
def oauth_query
|
86
|
+
{
|
87
|
+
client_id: client_id,
|
88
|
+
scope: scope,
|
89
|
+
response_type: "code",
|
90
|
+
redirect_uri: redirect_uri
|
91
|
+
}.map do |key, val|
|
92
|
+
"#{CGI.escape(key.to_s)}=#{CGI.escape(val.to_s)}"
|
93
|
+
end.join(?&)
|
94
|
+
end
|
95
|
+
|
96
|
+
# Private: Returns a connection for obtaining an access token from Ecwid
|
97
|
+
#
|
98
|
+
def connection
|
99
|
+
@connection ||= Faraday.new "https://my.ecwid.com" do |conn|
|
100
|
+
conn.request :url_encoded
|
101
|
+
conn.response :json, content_type: /\bjson$/
|
102
|
+
conn.adapter Faraday.default_adapter
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
106
|
end
|