clayful 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e2a9aaad387dd8b0bcb19a627a4a47b54341e9bd87946fc536e62ea2700f6ce6
4
+ data.tar.gz: 04b7890835ab15bd81c0b10e6936016d2617c74c9f8632be80a8c58fe63255b4
5
+ SHA512:
6
+ metadata.gz: 4637fd8d3db1c22c97c7ecde2637358962b9a8cae29bc3e28af29c0c971200b0c0812c64decddb880f0f8a1898701478e1d96f7e2eb2d3268cfd6ae5eb8ed82e
7
+ data.tar.gz: 461a1012c113a6d284aeb1d5565ee9d9864b39c2f1d54b36128defcef77db43617b7fc4559ce44f9a45c5aa5c7e95f3f4362e41d5e55fdeac3dbbf178fe343a4
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2017 Clayful
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,13 @@
1
+ # Clayful Ruby SDK
2
+
3
+ Official Ruby SDK for [clayful.io](https://clayful.io).
4
+
5
+ ## Tests
6
+
7
+ ```sh
8
+ ruby test/run.rb
9
+ ```
10
+
11
+ ## Links
12
+
13
+ - [API & SDK Documentation](https://dev.clayful.io)
@@ -0,0 +1,314 @@
1
+ require 'erb'
2
+ require_relative './exception'
3
+ require_relative './requester'
4
+ require_relative './models/binder'
5
+
6
+ module Clayful
7
+
8
+ @@base_url = 'https://api.clayful.io'
9
+
10
+ @@default_headers = {
11
+ 'Accept-Encoding' => 'gzip',
12
+ 'User-Agent' => 'clayful-ruby',
13
+ 'Clayful-SDK' => 'clayful-ruby'
14
+ }
15
+
16
+ @@plugins = {
17
+ 'request' => Clayful::Requester.request
18
+ }
19
+
20
+ @@listeners = {
21
+ 'request' => [],
22
+ 'response' => []
23
+ }
24
+
25
+
26
+ def self.base_url; @@base_url end;
27
+ def self.base_url=v; @@base_url = v end;
28
+
29
+ def self.default_headers; @@default_headers end;
30
+ def self.default_headers=v; @@default_headers = v end;
31
+
32
+ def self.plugins; @@plugins end;
33
+ def self.plugins=v; @@plugins = v end;
34
+
35
+ def self.listeners; @@listeners end;
36
+ def self.listeners=v; @@listeners = v end;
37
+
38
+
39
+ def self.options_to_headers(o = {})
40
+
41
+ headers = {}
42
+
43
+ if o['language']
44
+ headers['Accept-Language'] = o['language']
45
+ end
46
+
47
+ if o['currency']
48
+ headers['Accept-Currency'] = o['currency']
49
+ end
50
+
51
+ if o['time_zone']
52
+ headers['Accept-Time-Zone'] = o['time_zone']
53
+ end
54
+
55
+ if o['client']
56
+ headers['Authorization'] = 'Bearer ' + o['client']
57
+ end
58
+
59
+ if o['customer']
60
+ headers['Authorization-Customer'] = o['customer']
61
+ end
62
+
63
+ if o['reCAPTCHA']
64
+ headers['reCAPTCHA-Response'] = o['reCAPTCHA']
65
+ end
66
+
67
+ if o['debug_language']
68
+ headers['Accept-Debug-Language'] = o['debug_language']
69
+ end
70
+
71
+ if o['headers']
72
+ headers = headers.merge(o['headers'])
73
+ end
74
+
75
+ headers
76
+
77
+ end
78
+
79
+
80
+ def self.get_endpoint(path)
81
+
82
+ @@base_url + path
83
+
84
+ end
85
+
86
+
87
+ def self.normalize_query_values(query = {})
88
+
89
+ Hash[ query.map { |k, v| [k.to_s, ERB::Util.url_encode(v.to_s)] } ]
90
+
91
+ end
92
+
93
+
94
+ def self.extract_request_arguments(options = {})
95
+
96
+ result = {
97
+ 'http_method' => options['http_method'],
98
+ 'request_url' => options['path'],
99
+ 'payload' => nil,
100
+ 'query' => {},
101
+ 'headers' => {},
102
+ 'meta' => {}
103
+ }
104
+
105
+ rest = options['args'].slice(options['params'].length..-1)
106
+
107
+ options['params'].each.with_index do |key, i|
108
+
109
+ result['request_url'] = result['request_url'].sub('{' + key + '}', options['args'][i].to_s)
110
+ end
111
+
112
+ http_method = options['http_method']
113
+
114
+ if (http_method == 'POST' or http_method == 'PUT') and (not options['without_payload'])
115
+
116
+ result['payload'] = rest.shift
117
+ end
118
+
119
+ query_headers = rest[0] ? rest[0] : {}
120
+ query = query_headers.fetch('query', {})
121
+ meta = query_headers.fetch('meta', {})
122
+
123
+ result['query'] = self.normalize_query_values(query)
124
+ result['headers'] = self.options_to_headers(query_headers)
125
+ result['meta'] = meta
126
+
127
+ result
128
+
129
+ end
130
+
131
+
132
+ def self.call_api(options)
133
+
134
+ extracted = self.extract_request_arguments(options)
135
+
136
+ extracted = extracted.merge({
137
+ 'request_url' => self.get_endpoint(extracted['request_url']),
138
+ 'model_name' => options['model_name'],
139
+ 'method_name' => options['method_name'],
140
+ 'uses_form_data' => !!options['uses_form_data'],
141
+ 'error' => nil,
142
+ 'response' => nil,
143
+ })
144
+
145
+ # Extend default headers with header options
146
+ extracted['headers'] = @@default_headers.clone.merge(extracted['headers'])
147
+
148
+ self.trigger('request', extracted)
149
+
150
+ begin
151
+
152
+ response = self.plugins['request'].call(extracted)
153
+
154
+ extracted['response'] = response
155
+
156
+ self.trigger('response', extracted)
157
+
158
+ return response
159
+
160
+ rescue Clayful::Exception => e
161
+
162
+ extracted['error'] = e
163
+
164
+ self.trigger('response', extracted)
165
+
166
+ raise # re-raise the error
167
+
168
+ end
169
+
170
+ end
171
+
172
+
173
+ def self.config(options = {})
174
+
175
+ headers = self.options_to_headers(options)
176
+
177
+ @@default_headers = @@default_headers.merge(headers)
178
+
179
+ end
180
+
181
+
182
+ def self.install(scope, plugin)
183
+
184
+ if @@plugins[scope]
185
+
186
+ @@plugins[scope] = plugin
187
+ end
188
+
189
+ end
190
+
191
+
192
+ def self.on(event_name, callback)
193
+
194
+ listeners = @@listeners[event_name]
195
+
196
+ if listeners
197
+
198
+ listeners.push(callback)
199
+ end
200
+
201
+ end
202
+
203
+
204
+ def self.off(event_name, callback)
205
+
206
+ listeners = @@listeners[event_name]
207
+
208
+ if listeners
209
+
210
+ listeners.delete(callback)
211
+ end
212
+
213
+ end
214
+
215
+
216
+ def self.trigger(event_name, data)
217
+
218
+ listeners = @@listeners[event_name]
219
+
220
+ if listeners
221
+
222
+ listeners.each do |listener|
223
+
224
+ listener.call(data)
225
+ end
226
+ end
227
+
228
+ end
229
+
230
+
231
+ def self.format_image_url(base_url, options = {})
232
+
233
+ query = []
234
+
235
+ normalized = self.normalize_query_values(options)
236
+
237
+ normalized.each do |k, v|
238
+ query.push("#{k}=#{v}")
239
+ end
240
+
241
+ query = query.join('&')
242
+
243
+ query.empty? ? base_url : base_url + '?' + query
244
+
245
+ end
246
+
247
+
248
+ def self.format_number(number, currency = {})
249
+
250
+ if not number.is_a? Numeric
251
+
252
+ return ''
253
+ end
254
+
255
+ precision = currency.fetch('precision', nil)
256
+ delimiter = currency.fetch('delimiter', {})
257
+ thousands = delimiter.fetch('thousands', '')
258
+ decimal = delimiter.fetch('decimal', '.')
259
+
260
+ if precision.is_a? Numeric
261
+
262
+ # Conver to float to keep '.n'
263
+ n = (10 ** precision).to_f
264
+
265
+ number = (number * n).round / n
266
+
267
+ # To deal with 0.0 case..
268
+ if precision == 0
269
+
270
+ number = number.round
271
+ end
272
+
273
+ end
274
+
275
+ a, b = number.to_s.split('.')
276
+
277
+ a = a.reverse.scan(/.{1,3}/).join(thousands).reverse
278
+ b = b ? b : ''
279
+
280
+ if precision.is_a? Numeric
281
+
282
+ diff = precision - b.length
283
+ diff = diff < 0 ? 0 : diff
284
+
285
+ b += '0' * diff
286
+
287
+ end
288
+
289
+ decimal = b.empty? ? '' : decimal
290
+
291
+ [a, b].join(decimal)
292
+
293
+ end
294
+
295
+
296
+ def self.format_price(number, currency = {})
297
+
298
+ formatted_number = self.format_number(number, currency)
299
+
300
+ if formatted_number.empty?
301
+
302
+ return ''
303
+ end
304
+
305
+ symbol = currency.fetch('symbol', '')
306
+ format_string = currency.fetch('format', '{price}')
307
+
308
+ format_string
309
+ .sub('{symbol}', symbol)
310
+ .sub('{price}', formatted_number)
311
+
312
+ end
313
+
314
+ end
@@ -0,0 +1,38 @@
1
+ module Clayful
2
+
3
+ class Exception < StandardError
4
+
5
+ attr_reader :is_clayful
6
+ attr_reader :model
7
+ attr_reader :method
8
+ attr_reader :status
9
+ attr_reader :headers
10
+ attr_reader :code
11
+ attr_reader :message
12
+ attr_reader :validation
13
+
14
+ def initialize(
15
+ model = nil,
16
+ method = nil,
17
+ status = nil,
18
+ headers = nil,
19
+ code = nil,
20
+ message = '',
21
+ validation = nil)
22
+
23
+ @is_clayful = true
24
+ @model = model
25
+ @method = method
26
+ @status = status
27
+ @headers = headers
28
+ @code = code
29
+ @message = message
30
+ @validation = validation
31
+
32
+ super(message)
33
+
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,26 @@
1
+ require_relative './brand'
2
+ require_relative './cart'
3
+ require_relative './catalog'
4
+ require_relative './collection'
5
+ require_relative './country'
6
+ require_relative './coupon'
7
+ require_relative './currency'
8
+ require_relative './customer'
9
+ require_relative './discount'
10
+ require_relative './downloadable'
11
+ require_relative './group'
12
+ require_relative './image'
13
+ require_relative './impression'
14
+ require_relative './order'
15
+ require_relative './order_tag'
16
+ require_relative './payment_method'
17
+ require_relative './product'
18
+ require_relative './review'
19
+ require_relative './review_comment'
20
+ require_relative './shipping_method'
21
+ require_relative './store'
22
+ require_relative './subscription'
23
+ require_relative './subscription_plan'
24
+ require_relative './tax_category'
25
+ require_relative './tracker'
26
+ require_relative './wish_list'
@@ -0,0 +1,109 @@
1
+ module Clayful
2
+
3
+ class Brand
4
+
5
+ @@name = 'Brand'
6
+ @@path = 'brands'
7
+
8
+ def self.name
9
+ @@name
10
+ end
11
+
12
+ def self.path
13
+ @@path
14
+ end
15
+
16
+ def self.list(*args)
17
+
18
+ Clayful.call_api({
19
+ 'model_name' => @@name,
20
+ 'method_name' => 'list',
21
+ 'http_method' => 'GET',
22
+ 'path' => '/v1/brands',
23
+ 'params' => [],
24
+ 'args' => args
25
+ })
26
+
27
+ end
28
+
29
+ def self.count(*args)
30
+
31
+ Clayful.call_api({
32
+ 'model_name' => @@name,
33
+ 'method_name' => 'count',
34
+ 'http_method' => 'GET',
35
+ 'path' => '/v1/brands/count',
36
+ 'params' => [],
37
+ 'args' => args
38
+ })
39
+
40
+ end
41
+
42
+ def self.get(*args)
43
+
44
+ Clayful.call_api({
45
+ 'model_name' => @@name,
46
+ 'method_name' => 'get',
47
+ 'http_method' => 'GET',
48
+ 'path' => '/v1/brands/{brandId}',
49
+ 'params' => ['brandId', ],
50
+ 'args' => args
51
+ })
52
+
53
+ end
54
+
55
+ def self.pull_from_metafield(*args)
56
+
57
+ Clayful.call_api({
58
+ 'model_name' => @@name,
59
+ 'method_name' => 'pull_from_metafield',
60
+ 'http_method' => 'POST',
61
+ 'path' => '/v1/brands/{brandId}/meta/{field}/pull',
62
+ 'params' => ['brandId', 'field', ],
63
+ 'args' => args
64
+ })
65
+
66
+ end
67
+
68
+ def self.push_to_metafield(*args)
69
+
70
+ Clayful.call_api({
71
+ 'model_name' => @@name,
72
+ 'method_name' => 'push_to_metafield',
73
+ 'http_method' => 'POST',
74
+ 'path' => '/v1/brands/{brandId}/meta/{field}/push',
75
+ 'params' => ['brandId', 'field', ],
76
+ 'args' => args
77
+ })
78
+
79
+ end
80
+
81
+ def self.increase_metafield(*args)
82
+
83
+ Clayful.call_api({
84
+ 'model_name' => @@name,
85
+ 'method_name' => 'increase_metafield',
86
+ 'http_method' => 'POST',
87
+ 'path' => '/v1/brands/{brandId}/meta/{field}/inc',
88
+ 'params' => ['brandId', 'field', ],
89
+ 'args' => args
90
+ })
91
+
92
+ end
93
+
94
+ def self.delete_metafield(*args)
95
+
96
+ Clayful.call_api({
97
+ 'model_name' => @@name,
98
+ 'method_name' => 'delete_metafield',
99
+ 'http_method' => 'DELETE',
100
+ 'path' => '/v1/brands/{brandId}/meta/{field}',
101
+ 'params' => ['brandId', 'field', ],
102
+ 'args' => args
103
+ })
104
+
105
+ end
106
+
107
+ end
108
+
109
+ end