ecwid_api 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -0
  3. data/.rspec +0 -0
  4. data/.travis.yml +0 -0
  5. data/Gemfile +0 -2
  6. data/LICENSE.txt +0 -0
  7. data/README.md +228 -228
  8. data/Rakefile +0 -0
  9. data/ecwid_api.gemspec +4 -4
  10. data/lib/ecwid_api.rb +25 -25
  11. data/lib/ecwid_api/api.rb +8 -30
  12. data/lib/ecwid_api/api/base.rb +17 -16
  13. data/lib/ecwid_api/api/categories.rb +55 -56
  14. data/lib/ecwid_api/api/orders.rb +36 -36
  15. data/lib/ecwid_api/api/product_combinations.rb +2 -5
  16. data/lib/ecwid_api/api/products.rb +61 -63
  17. data/lib/ecwid_api/category.rb +1 -7
  18. data/lib/ecwid_api/client.rb +99 -65
  19. data/lib/ecwid_api/entity.rb +4 -6
  20. data/lib/ecwid_api/error.rb +12 -12
  21. data/lib/ecwid_api/o_auth.rb +105 -105
  22. data/lib/ecwid_api/order.rb +0 -0
  23. data/lib/ecwid_api/order_item.rb +0 -0
  24. data/lib/ecwid_api/paged_ecwid_response.rb +0 -0
  25. data/lib/ecwid_api/paged_enumerator.rb +0 -0
  26. data/lib/ecwid_api/person.rb +0 -0
  27. data/lib/ecwid_api/product.rb +3 -15
  28. data/lib/ecwid_api/product_combination.rb +1 -5
  29. data/lib/ecwid_api/version.rb +1 -1
  30. data/lib/ext/string.rb +12 -12
  31. data/spec/api/categories_spec.rb +30 -30
  32. data/spec/api/orders_spec.rb +29 -29
  33. data/spec/api/products_spec.rb +0 -0
  34. data/spec/category_spec.rb +33 -33
  35. data/spec/client_spec.rb +20 -20
  36. data/spec/entity_spec.rb +0 -0
  37. data/spec/fixtures/categories.json +0 -0
  38. data/spec/fixtures/category.json +0 -0
  39. data/spec/fixtures/order.json +0 -0
  40. data/spec/fixtures/orders.json +0 -0
  41. data/spec/fixtures/products.json +0 -0
  42. data/spec/helpers/client.rb +31 -31
  43. data/spec/oauth_spec.rb +39 -39
  44. data/spec/order_item_spec.rb +11 -11
  45. data/spec/order_spec.rb +0 -0
  46. data/spec/paged_enumerator_spec.rb +0 -0
  47. data/spec/spec_helper.rb +24 -24
  48. metadata +26 -34
@@ -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.post("categories/#{id}/image") do |req|
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
@@ -1,66 +1,100 @@
1
- module EcwidApi
2
- # Public: Client objects manage the connection and interface to a single Ecwid
3
- # store.
4
- #
5
- # Examples
6
- #
7
- # client = EcwidApi::Client.new(store_id: '12345', token: 'the access_token')
8
- # client.get "/products"
9
- #
10
- class Client
11
- extend Forwardable
12
-
13
- # The default base URL for the Ecwid API
14
- DEFAULT_URL = "https://app.ecwid.com/api/v3"
15
-
16
- # Public: Returns the Ecwid Store ID
17
- attr_reader :store_id
18
- attr_reader :token
19
- attr_reader :adapter
20
-
21
- # Public: Initializes a new Client to interact with the API
22
- #
23
- # store_id - the Ecwid store_id to interact with
24
- # token - the authorization token provided by oAuth. See the
25
- # Authentication class
26
- #
27
- def initialize(store_id, token, adapter = Faraday.default_adapter)
28
- @store_id, @token, @adapter = store_id, token, adapter
29
- end
30
-
31
- # Public: The URL of the API for the Ecwid Store
32
- def store_url
33
- "#{DEFAULT_URL}/#{store_id}"
34
- end
35
-
36
- def_delegators :connection, :get, :post, :put, :delete
37
-
38
- # Public: Returns the Category API
39
- def categories
40
- @categories ||= Api::Categories.new(self)
41
- end
42
-
43
- # Public: Returns the Order API
44
- def orders
45
- @orders ||= Api::Orders.new(self)
46
- end
47
-
48
- # Public: Returns the Products API
49
- def products
50
- @products ||= Api::Products.new(self)
51
- end
52
-
53
- # Private: Returns a Faraday connection to interface with the Ecwid API
54
- def connection
55
- @connection ||= Faraday.new store_url do |conn|
56
- conn.request :oauth2, token, param_name: :token
57
- conn.request :json
58
-
59
- conn.response :json, content_type: /\bjson$/
60
- conn.response :logger
61
-
62
- conn.adapter adapter
63
- end
64
- end
65
- end
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
@@ -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).tap do |response|
167
- raise_on_failure(response)
168
- end
166
+ client.delete(url)
169
167
  end
170
168
 
171
169
  def to_hash
@@ -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
@@ -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