commerce 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/lib/commerce.rb +38 -0
- data/lib/commerce/api_helper.rb +151 -0
- data/lib/commerce/base_controller.rb +19 -0
- data/lib/commerce/cart.rb +221 -0
- data/lib/commerce/checkout.rb +451 -0
- data/lib/commerce/exceptions/api_exception.rb +16 -0
- data/lib/commerce/http/auth/custom_header_auth.rb +11 -0
- data/lib/commerce/http/faraday_client.rb +41 -0
- data/lib/commerce/http/http_call_back.rb +17 -0
- data/lib/commerce/http/http_client.rb +82 -0
- data/lib/commerce/http/http_context.rb +15 -0
- data/lib/commerce/http/http_method_enum.rb +7 -0
- data/lib/commerce/http/http_request.rb +44 -0
- data/lib/commerce/http/http_response.rb +21 -0
- data/lib/commerce/order.rb +113 -0
- data/lib/commerce/product.rb +60 -0
- data/lib/commerce/service.rb +85 -0
- data/spec/api/cart_api_spec.rb +167 -0
- data/spec/api/checkout_api_spec.rb +309 -0
- data/spec/api/order_api_spec.rb +100 -0
- data/spec/api/product_api_spec.rb +69 -0
- data/spec/api/service_api_spec.rb +82 -0
- data/spec/api_client_spec.rb +226 -0
- data/spec/configuration_spec.rb +42 -0
- data/spec/spec_helper.rb +111 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 504043403dfe7eab378ed90dec6a9be7808f4085
|
4
|
+
data.tar.gz: eef0d809d0bf5571cb2ca18733d0f53a06b2ee66
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9f7ac949f28cf1879fb64deb95b8451ace69db4e79b9255146b33ef9ebf30fa3aadb5854f443a14897e32599c7e60a0314ab5678190c3561d204c60ee286832a
|
7
|
+
data.tar.gz: 823e606f2a2dd70ec1eb416e566606ce7c46ace8da9035974c5899df3e9114ede8367267eab8b8856abf131f717b79a7f3220c87e88468f6ace5b4aa4e09c289
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2016 Chec (Chec Commerce, Inc)
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/lib/commerce.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'faraday'
|
3
|
+
require 'certifi'
|
4
|
+
|
5
|
+
# Common files
|
6
|
+
require_relative 'commerce/api_helper.rb'
|
7
|
+
|
8
|
+
# Http
|
9
|
+
require_relative 'commerce/http/http_call_back.rb'
|
10
|
+
require_relative 'commerce/http/http_client.rb'
|
11
|
+
require_relative 'commerce/http/http_method_enum.rb'
|
12
|
+
require_relative 'commerce/http/http_request.rb'
|
13
|
+
require_relative 'commerce/http/http_response.rb'
|
14
|
+
require_relative 'commerce/http/http_context.rb'
|
15
|
+
require_relative 'commerce/http/faraday_client.rb'
|
16
|
+
require_relative 'commerce/http/auth/custom_header_auth.rb'
|
17
|
+
|
18
|
+
# APIs
|
19
|
+
require_relative 'commerce/base_controller.rb'
|
20
|
+
require_relative 'commerce/cart.rb'
|
21
|
+
require_relative 'commerce/checkout.rb'
|
22
|
+
require_relative 'commerce/order.rb'
|
23
|
+
require_relative 'commerce/product.rb'
|
24
|
+
require_relative 'commerce/service.rb'
|
25
|
+
|
26
|
+
module Commerce
|
27
|
+
|
28
|
+
@api_base = 'https://api.chec.io/v1'
|
29
|
+
@uploads_base = 'https://uploads.chec.io'
|
30
|
+
|
31
|
+
class << self
|
32
|
+
attr_accessor :api_key, :api_base
|
33
|
+
def Auth(api_key)
|
34
|
+
@api_key = api_key
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,151 @@
|
|
1
|
+
# This file was automatically generated by APIMATIC v2.0 ( https://apimatic.io ).
|
2
|
+
|
3
|
+
module Commerce
|
4
|
+
class APIHelper
|
5
|
+
# Replaces template parameters in the given url
|
6
|
+
# @param [String] The query string builder to replace the template parameters
|
7
|
+
# @param [Hash] The parameters to replace in the url
|
8
|
+
def self.append_url_with_template_parameters(query_builder, parameters)
|
9
|
+
# perform parameter validation
|
10
|
+
raise ArgumentError, 'Given value for parameter \"query_builder\" is invalid.' unless query_builder.instance_of? String
|
11
|
+
|
12
|
+
# return if there are no parameters to replace
|
13
|
+
if parameters.nil?
|
14
|
+
query_builder
|
15
|
+
else
|
16
|
+
# iterate and append parameters
|
17
|
+
parameters.each do |key, value|
|
18
|
+
replace_value = ''
|
19
|
+
|
20
|
+
if value.nil?
|
21
|
+
replace_value = ''
|
22
|
+
elsif value.instance_of? Array
|
23
|
+
value.map!{|element| CGI.escape(element.to_s)}
|
24
|
+
replace_value = value.join('/')
|
25
|
+
else
|
26
|
+
replace_value = CGI.escape(value.to_s)
|
27
|
+
end
|
28
|
+
|
29
|
+
# find the template parameter and replace it with its value
|
30
|
+
query_builder = query_builder.gsub('{' + key.to_s + '}', replace_value)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
return query_builder
|
34
|
+
end
|
35
|
+
|
36
|
+
# Appends the given set of parameters to the given query string
|
37
|
+
# @param [String] The query string builder to replace the template parameters
|
38
|
+
# @param [Hash] The parameters to append
|
39
|
+
def self.append_url_with_query_parameters(query_builder, parameters)
|
40
|
+
# perform parameter validation
|
41
|
+
raise ArgumentError, 'Given value for parameter \"query_builder\" is invalid.' unless query_builder.instance_of? String
|
42
|
+
|
43
|
+
# return if there are no parameters to replace
|
44
|
+
if parameters.nil?
|
45
|
+
return query_builder
|
46
|
+
else
|
47
|
+
# remove any nil values
|
48
|
+
parameters = parameters.reject { |_key, value| value.nil? }
|
49
|
+
|
50
|
+
# does the query string already has parameters
|
51
|
+
has_params = query_builder.include? '?'
|
52
|
+
separator = has_params ? '&' : '?'
|
53
|
+
|
54
|
+
# append query with separator and parameters and return
|
55
|
+
return query_builder << separator << URI.encode_www_form(parameters)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# Validates and processes the given Url
|
60
|
+
# @param [String] The given Url to process
|
61
|
+
# @return [String] Pre-processed Url as string
|
62
|
+
def self.clean_url(url)
|
63
|
+
# perform parameter validation
|
64
|
+
raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
|
65
|
+
|
66
|
+
# ensure that the urls are absolute
|
67
|
+
matches = url.match(%r{^(https?:\/\/[^\/]+)})
|
68
|
+
raise ArgumentError, 'Invalid Url format.' if matches.nil?
|
69
|
+
|
70
|
+
# get the http protocol match
|
71
|
+
protocol = matches[1]
|
72
|
+
|
73
|
+
# check if parameters exist
|
74
|
+
index = url.index('?')
|
75
|
+
|
76
|
+
# remove redundant forward slashes
|
77
|
+
query = url[protocol.length...(index != nil ? index : url.length)]
|
78
|
+
query.gsub!(%r{\/\/+}, '/')
|
79
|
+
|
80
|
+
# get the parameters
|
81
|
+
parameters = index != nil ? url[url.index('?')...url.length] : ""
|
82
|
+
|
83
|
+
# return processed url
|
84
|
+
return protocol + query + parameters
|
85
|
+
end
|
86
|
+
|
87
|
+
# Parses JSON string.
|
88
|
+
# @param [String] A JSON string.
|
89
|
+
def self.json_deserialize(json)
|
90
|
+
begin
|
91
|
+
return JSON.parse(json)
|
92
|
+
rescue
|
93
|
+
raise TypeError, "Server responded with invalid JSON."
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Form encodes a hash of parameters.
|
98
|
+
# @param [Hash] The hash of parameters to encode.
|
99
|
+
# @return [Hash] A hash with the same parameters form encoded.
|
100
|
+
def self.form_encode_parameters(form_parameters)
|
101
|
+
encoded = Hash.new
|
102
|
+
form_parameters.each do |key, value|
|
103
|
+
encoded.merge!(APIHelper.form_encode value, key)
|
104
|
+
end
|
105
|
+
return encoded
|
106
|
+
end
|
107
|
+
|
108
|
+
# Form encodes an object.
|
109
|
+
# @param [Dynamic] An object to form encode.
|
110
|
+
# @param [String] The name of the object.
|
111
|
+
# @return [Hash] A form encoded representation of the object in the form of a hash.
|
112
|
+
def self.form_encode(obj, instance_name)
|
113
|
+
retval = Hash.new
|
114
|
+
|
115
|
+
# If this is a structure, resolve it's field names.
|
116
|
+
if obj.kind_of? BaseModel
|
117
|
+
obj = obj.to_hash
|
118
|
+
end
|
119
|
+
|
120
|
+
# Create a form encoded hash for this object.
|
121
|
+
if obj == nil
|
122
|
+
nil
|
123
|
+
elsif obj.instance_of? Array
|
124
|
+
obj.each_with_index do |value, index|
|
125
|
+
retval.merge!(APIHelper.form_encode(value, instance_name + "[" + index.to_s + "]"))
|
126
|
+
end
|
127
|
+
elsif obj.instance_of? Hash
|
128
|
+
obj.each do |key, value|
|
129
|
+
retval.merge!(APIHelper.form_encode(value, instance_name + "[" + key + "]"))
|
130
|
+
end
|
131
|
+
else
|
132
|
+
retval[instance_name] = obj
|
133
|
+
end
|
134
|
+
return retval
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
# extend types to support to_bool
|
140
|
+
module ToBoolean
|
141
|
+
def to_bool
|
142
|
+
return true if self == true || self.to_s.strip =~ /^(true|yes|y|1)$/i
|
143
|
+
return false
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
class NilClass; include ToBoolean; end
|
148
|
+
class TrueClass; include ToBoolean; end
|
149
|
+
class FalseClass; include ToBoolean; end
|
150
|
+
class Numeric; include ToBoolean; end
|
151
|
+
class String; include ToBoolean; end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# This file was automatically generated by APIMATIC v2.0 ( https://apimatic.io ).
|
2
|
+
|
3
|
+
module Commerce
|
4
|
+
class BaseController
|
5
|
+
attr_accessor :http_client, :http_call_back
|
6
|
+
|
7
|
+
@@http_client = FaradayClient.new(60)
|
8
|
+
|
9
|
+
@@global_headers = {
|
10
|
+
'user-agent' => 'Commerce.ruby/v1'
|
11
|
+
}
|
12
|
+
|
13
|
+
def initialize(http_client: nil, http_call_back: nil)
|
14
|
+
@http_client = http_client ||= @@http_client
|
15
|
+
@http_call_back = http_call_back
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,221 @@
|
|
1
|
+
module Commerce
|
2
|
+
class Cart < BaseController
|
3
|
+
|
4
|
+
def self.add(id, _parameters, _query_parameters = [])
|
5
|
+
|
6
|
+
_query_builder = Commerce.api_base.dup
|
7
|
+
_query_builder << '/carts/{id}'
|
8
|
+
|
9
|
+
_query_builder = APIHelper.append_url_with_template_parameters _query_builder, {
|
10
|
+
'id' => id
|
11
|
+
}
|
12
|
+
|
13
|
+
|
14
|
+
_query_url = APIHelper.clean_url _query_builder
|
15
|
+
|
16
|
+
_request = @@http_client.POST _query_url, parameters: _parameters
|
17
|
+
|
18
|
+
|
19
|
+
CustomHeaderAuth.apply(_request)
|
20
|
+
|
21
|
+
_request.headers = @@global_headers.clone.merge(_request.headers)
|
22
|
+
|
23
|
+
_response = @@http_client.execute_as_string(_request)
|
24
|
+
|
25
|
+
context = HttpContext.new(_request, _response)
|
26
|
+
|
27
|
+
return context.response.array
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.contents(id, _query_parameters = [])
|
32
|
+
|
33
|
+
_query_builder = Commerce.api_base.dup
|
34
|
+
_query_builder << '/carts/{id}/items'
|
35
|
+
|
36
|
+
_query_builder = APIHelper.append_url_with_template_parameters _query_builder, {
|
37
|
+
'id' => id
|
38
|
+
}
|
39
|
+
|
40
|
+
|
41
|
+
_query_url = APIHelper.clean_url _query_builder
|
42
|
+
|
43
|
+
|
44
|
+
_request = @@http_client.GET _query_url
|
45
|
+
|
46
|
+
CustomHeaderAuth.apply(_request)
|
47
|
+
|
48
|
+
_request.headers = @@global_headers.clone.merge(_request.headers)
|
49
|
+
|
50
|
+
_response = @@http_client.execute_as_string(_request)
|
51
|
+
|
52
|
+
context = HttpContext.new(_request, _response)
|
53
|
+
|
54
|
+
return context.response.array
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.create( _query_parameters = [])
|
59
|
+
|
60
|
+
_query_builder = Commerce.api_base.dup
|
61
|
+
_query_builder << '/carts'
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
_query_url = APIHelper.clean_url _query_builder
|
66
|
+
|
67
|
+
|
68
|
+
_request = @@http_client.GET _query_url
|
69
|
+
|
70
|
+
CustomHeaderAuth.apply(_request)
|
71
|
+
|
72
|
+
_request.headers = @@global_headers.clone.merge(_request.headers)
|
73
|
+
|
74
|
+
_response = @@http_client.execute_as_string(_request)
|
75
|
+
|
76
|
+
context = HttpContext.new(_request, _response)
|
77
|
+
|
78
|
+
return context.response.array
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.delete(id, _query_parameters = [])
|
83
|
+
|
84
|
+
_query_builder = Commerce.api_base.dup
|
85
|
+
_query_builder << '/carts/{id}'
|
86
|
+
|
87
|
+
_query_builder = APIHelper.append_url_with_template_parameters _query_builder, {
|
88
|
+
'id' => id
|
89
|
+
}
|
90
|
+
|
91
|
+
|
92
|
+
_query_url = APIHelper.clean_url _query_builder
|
93
|
+
|
94
|
+
|
95
|
+
_request = @@http_client.DELETE _query_url
|
96
|
+
|
97
|
+
CustomHeaderAuth.apply(_request)
|
98
|
+
|
99
|
+
_request.headers = @@global_headers.clone.merge(_request.headers)
|
100
|
+
|
101
|
+
_response = @@http_client.execute_as_string(_request)
|
102
|
+
|
103
|
+
context = HttpContext.new(_request, _response)
|
104
|
+
|
105
|
+
return context.response.array
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
def self.remove(id, line_item_id, _query_parameters = [])
|
110
|
+
|
111
|
+
_query_builder = Commerce.api_base.dup
|
112
|
+
_query_builder << '/carts/{id}/items/{line_item_id}'
|
113
|
+
|
114
|
+
_query_builder = APIHelper.append_url_with_template_parameters _query_builder, {
|
115
|
+
'id' => id,
|
116
|
+
'line_item_id' => line_item_id
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
_query_url = APIHelper.clean_url _query_builder
|
121
|
+
|
122
|
+
|
123
|
+
_request = @@http_client.DELETE _query_url
|
124
|
+
|
125
|
+
CustomHeaderAuth.apply(_request)
|
126
|
+
|
127
|
+
_request.headers = @@global_headers.clone.merge(_request.headers)
|
128
|
+
|
129
|
+
_response = @@http_client.execute_as_string(_request)
|
130
|
+
|
131
|
+
context = HttpContext.new(_request, _response)
|
132
|
+
|
133
|
+
return context.response.array
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
def self.reset(id, _query_parameters = [])
|
138
|
+
|
139
|
+
_query_builder = Commerce.api_base.dup
|
140
|
+
_query_builder << '/carts/{id}/items'
|
141
|
+
|
142
|
+
_query_builder = APIHelper.append_url_with_template_parameters _query_builder, {
|
143
|
+
'id' => id
|
144
|
+
}
|
145
|
+
|
146
|
+
|
147
|
+
_query_url = APIHelper.clean_url _query_builder
|
148
|
+
|
149
|
+
|
150
|
+
_request = @@http_client.DELETE _query_url
|
151
|
+
|
152
|
+
CustomHeaderAuth.apply(_request)
|
153
|
+
|
154
|
+
_request.headers = @@global_headers.clone.merge(_request.headers)
|
155
|
+
|
156
|
+
_response = @@http_client.execute_as_string(_request)
|
157
|
+
|
158
|
+
context = HttpContext.new(_request, _response)
|
159
|
+
|
160
|
+
return context.response.array
|
161
|
+
|
162
|
+
end
|
163
|
+
|
164
|
+
def self.retrieve(id, _query_parameters = [])
|
165
|
+
|
166
|
+
_query_builder = Commerce.api_base.dup
|
167
|
+
_query_builder << '/carts/{id}'
|
168
|
+
|
169
|
+
_query_builder = APIHelper.append_url_with_template_parameters _query_builder, {
|
170
|
+
'id' => id
|
171
|
+
}
|
172
|
+
|
173
|
+
|
174
|
+
_query_url = APIHelper.clean_url _query_builder
|
175
|
+
|
176
|
+
|
177
|
+
_request = @@http_client.GET _query_url
|
178
|
+
|
179
|
+
CustomHeaderAuth.apply(_request)
|
180
|
+
|
181
|
+
_request.headers = @@global_headers.clone.merge(_request.headers)
|
182
|
+
|
183
|
+
_response = @@http_client.execute_as_string(_request)
|
184
|
+
|
185
|
+
context = HttpContext.new(_request, _response)
|
186
|
+
|
187
|
+
return context.response.array
|
188
|
+
|
189
|
+
end
|
190
|
+
|
191
|
+
def self.update(id, line_item_id, _parameters, _query_parameters = [])
|
192
|
+
|
193
|
+
_query_builder = Commerce.api_base.dup
|
194
|
+
_query_builder << '/carts/{id}/items/{line_item_id}'
|
195
|
+
|
196
|
+
_query_builder = APIHelper.append_url_with_template_parameters _query_builder, {
|
197
|
+
'id' => id,
|
198
|
+
'line_item_id' => line_item_id
|
199
|
+
}
|
200
|
+
|
201
|
+
|
202
|
+
_query_url = APIHelper.clean_url _query_builder
|
203
|
+
|
204
|
+
_request = @@http_client.PUT _query_url, parameters: _parameters
|
205
|
+
|
206
|
+
CustomHeaderAuth.apply(_request)
|
207
|
+
|
208
|
+
_request.headers = @@global_headers.clone.merge(_request.headers)
|
209
|
+
|
210
|
+
_response = @@http_client.execute_as_string(_request)
|
211
|
+
|
212
|
+
context = HttpContext.new(_request, _response)
|
213
|
+
|
214
|
+
return context.response.array
|
215
|
+
|
216
|
+
end
|
217
|
+
|
218
|
+
|
219
|
+
end
|
220
|
+
|
221
|
+
end
|