tradenity 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +7 -0
  3. data/README.md +89 -77
  4. data/lib/tradenity/api_client.rb +408 -0
  5. data/lib/tradenity/api_error.rb +36 -0
  6. data/lib/tradenity/configuration.rb +205 -0
  7. data/lib/tradenity/exceptions.rb +5 -0
  8. data/lib/tradenity/rails/railtie.rb +20 -20
  9. data/lib/tradenity/resources/address.rb +729 -0
  10. data/lib/tradenity/resources/braintree_gateway.rb +804 -0
  11. data/lib/tradenity/resources/brand.rb +732 -0
  12. data/lib/tradenity/resources/cancel_operation.rb +675 -0
  13. data/lib/tradenity/resources/cart_settings.rb +219 -0
  14. data/lib/tradenity/resources/cash_on_delivery_payment.rb +746 -0
  15. data/lib/tradenity/resources/category.rb +763 -0
  16. data/lib/tradenity/resources/collection.rb +734 -0
  17. data/lib/tradenity/resources/contact_info.rb +306 -0
  18. data/lib/tradenity/resources/countries_geo_zone.rb +739 -0
  19. data/lib/tradenity/resources/country.rb +803 -0
  20. data/lib/tradenity/resources/coupon.rb +454 -0
  21. data/lib/tradenity/resources/credit_card_payment.rb +803 -0
  22. data/lib/tradenity/resources/currency.rb +762 -0
  23. data/lib/tradenity/resources/customer.rb +816 -0
  24. data/lib/tradenity/resources/customer_group.rb +723 -0
  25. data/lib/tradenity/resources/dimensions.rb +263 -0
  26. data/lib/tradenity/resources/discount_coupon.rb +811 -0
  27. data/lib/tradenity/resources/discount_promotion.rb +784 -0
  28. data/lib/tradenity/resources/fixed_rate_shipping.rb +837 -0
  29. data/lib/tradenity/resources/free_item_coupon.rb +793 -0
  30. data/lib/tradenity/resources/free_shipping.rb +806 -0
  31. data/lib/tradenity/resources/free_shipping_coupon.rb +788 -0
  32. data/lib/tradenity/resources/free_shipping_promotion.rb +744 -0
  33. data/lib/tradenity/resources/gateway.rb +435 -0
  34. data/lib/tradenity/resources/general_settings.rb +206 -0
  35. data/lib/tradenity/resources/geo_zone.rb +406 -0
  36. data/lib/tradenity/resources/instance_meta.rb +182 -0
  37. data/lib/tradenity/resources/inventory_settings.rb +197 -0
  38. data/lib/tradenity/resources/items_selector.rb +270 -0
  39. data/lib/tradenity/resources/line_item.rb +347 -0
  40. data/lib/tradenity/resources/mail_server_settings.rb +221 -0
  41. data/lib/tradenity/resources/measurement_settings.rb +253 -0
  42. data/lib/tradenity/resources/option.rb +787 -0
  43. data/lib/tradenity/resources/option_set.rb +734 -0
  44. data/lib/tradenity/resources/option_value.rb +680 -0
  45. data/lib/tradenity/resources/order.rb +929 -0
  46. data/lib/tradenity/resources/order_line_item.rb +777 -0
  47. data/lib/tradenity/resources/paging.rb +38 -0
  48. data/lib/tradenity/resources/payment.rb +429 -0
  49. data/lib/tradenity/resources/payment_card.rb +847 -0
  50. data/lib/tradenity/resources/payment_settings.rb +201 -0
  51. data/lib/tradenity/resources/payment_source.rb +392 -0
  52. data/lib/tradenity/resources/payment_token.rb +723 -0
  53. data/lib/tradenity/resources/payment_transaction.rb +740 -0
  54. data/lib/tradenity/resources/photo.rb +341 -0
  55. data/lib/tradenity/resources/product.rb +1156 -0
  56. data/lib/tradenity/resources/promotion.rb +427 -0
  57. data/lib/tradenity/resources/refund_operation.rb +358 -0
  58. data/lib/tradenity/resources/refund_transaction.rb +730 -0
  59. data/lib/tradenity/resources/return_line_item.rb +781 -0
  60. data/lib/tradenity/resources/return_operation.rb +686 -0
  61. data/lib/tradenity/resources/return_settings.rb +255 -0
  62. data/lib/tradenity/resources/shipping_method.rb +530 -0
  63. data/lib/tradenity/resources/shopping_cart.rb +647 -0
  64. data/lib/tradenity/resources/state.rb +675 -0
  65. data/lib/tradenity/resources/states_geo_zone.rb +748 -0
  66. data/lib/tradenity/resources/store_credit.rb +686 -0
  67. data/lib/tradenity/resources/store_credit_payment.rb +764 -0
  68. data/lib/tradenity/resources/store_credit_transaction.rb +730 -0
  69. data/lib/tradenity/resources/store_profile.rb +532 -0
  70. data/lib/tradenity/resources/stripe_gateway.rb +804 -0
  71. data/lib/tradenity/resources/table_rate_rule.rb +712 -0
  72. data/lib/tradenity/resources/table_rate_shipping.rb +834 -0
  73. data/lib/tradenity/resources/tax_class.rb +723 -0
  74. data/lib/tradenity/resources/tax_rate.rb +845 -0
  75. data/lib/tradenity/resources/tax_settings.rb +231 -0
  76. data/lib/tradenity/resources/test_gateway.rb +804 -0
  77. data/lib/tradenity/resources/transaction.rb +523 -0
  78. data/lib/tradenity/resources/utils.rb +7 -0
  79. data/lib/tradenity/resources/variant.rb +877 -0
  80. data/lib/tradenity/resources/web_hooks.rb +262 -0
  81. data/lib/tradenity/resources/weight.rb +235 -0
  82. data/lib/tradenity/resources/wish_list.rb +686 -0
  83. data/lib/tradenity/resources/zip_codes_geo_zone.rb +755 -0
  84. data/lib/tradenity/token_holder.rb +22 -0
  85. data/lib/tradenity/version.rb +13 -3
  86. data/lib/tradenity.rb +117 -7
  87. data/tradenity.gemspec +35 -30
  88. metadata +200 -34
  89. data/lib/tradenity/api.rb +0 -17
  90. data/lib/tradenity/error/exceptions.rb +0 -52
  91. data/lib/tradenity/error/message.rb +0 -38
  92. data/lib/tradenity/http/client.rb +0 -130
  93. data/lib/tradenity/model/base.rb +0 -100
  94. data/lib/tradenity/model/entities.rb +0 -390
  95. data/lib/tradenity/model/paging.rb +0 -23
@@ -1,130 +0,0 @@
1
- require 'unirest'
2
- require 'tradenity/error/exceptions'
3
- require 'tradenity/error/message'
4
-
5
- module Tradenity
6
-
7
- AUTH_TOKEN_HEADER_NAME = 'X-Tradenity-Session-ID'
8
- AUTH_TOKEN_HEADER_SYMBOL = :x_tradenity_session_id
9
-
10
- class HttpClient
11
- @@instance = nil
12
-
13
- def initialize()
14
- @key = Tradenity.api_key
15
- @current_session = nil
16
- end
17
-
18
-
19
- def self.get_instance
20
- unless @@instance
21
- @@instance = HttpClient.new
22
- end
23
- @@instance
24
- end
25
-
26
- def current_session(session)
27
- @current_session = session
28
- end
29
-
30
- def reset_current_session
31
- @current_session[:auth_token] = nil
32
- end
33
-
34
- def auth_token
35
- if @current_session.has_key? :auth_token
36
- @current_session[:auth_token]
37
- else
38
- nil
39
- end
40
- end
41
-
42
- def http_op(op, url, data)
43
- headers = {}
44
- auth = nil
45
- if auth_token == nil
46
- auth = {:user=> @key, :password=> ''}
47
- else
48
- headers[AUTH_TOKEN_HEADER_NAME] = auth_token
49
- end
50
- begin
51
- case op
52
- when 'GET'
53
- response = Unirest.get(url, headers: headers, auth: auth, parameters: data)
54
- when 'POST'
55
- response = Unirest.post(url, headers: headers, auth: auth, parameters: data)
56
- when 'PUT'
57
- response = Unirest.put(url, headers: headers, auth: auth, parameters: data)
58
- when 'DELETE'
59
- response = Unirest.delete(url, headers: headers, auth: auth, parameters: data)
60
- else
61
- raise 'You must provide a valid Http method.'
62
- end
63
- rescue
64
- raise ClientErrorException.new
65
- end
66
-
67
- if response.headers.has_key? AUTH_TOKEN_HEADER_SYMBOL
68
-
69
- @current_session[:auth_token] = response.headers[AUTH_TOKEN_HEADER_SYMBOL]
70
- end
71
-
72
- if response.code >= 400
73
- translate_exception(response)
74
- end
75
-
76
- response.body
77
- end
78
-
79
-
80
- def get(url, data=nil)
81
- http_op('GET', url, data)
82
- end
83
-
84
- def post(url, data=nil)
85
- http_op('POST', url, data)
86
- end
87
-
88
- def put(url, data=nil)
89
- http_op('PUT', url, data)
90
- end
91
-
92
- def delete( url)
93
- http_op('DELETE', url, {})
94
- end
95
-
96
- def translate_exception(response)
97
- error = ErrorMessage.new(response.body)
98
- case response.code
99
- when 500
100
- raise ServerErrorException.new('API server error.')
101
- when 401
102
- if auth_token == nil
103
- raise AuthenticationException.new(error)
104
- else
105
- raise SessionExpiredException.new(error)
106
- end
107
- when 403
108
- raise AuthorizationException.new(error)
109
- when 404
110
- raise EntityNotFoundException.new(error)
111
- when 400
112
- case error.errorCode
113
- when DATA_VALIDATION_ERROR_CODE
114
- raise DataValidationException.new(error)
115
- when INVALID_PAYMENT_ERROR_CODE, GATEWAY_ERROR_ERROR_CODE, REFUND_ERROR_ERROR_CODE
116
- raise PaymentErrorException.new(error)
117
- when EXISTING_USERNAME_ERROR_CODE, EXISTING_EMAIL_ERROR_CODE
118
- raise CustomerCreationException.new(error)
119
- when CART_INVALID_ITEM_ERROR_CODE
120
- raise ShoppingCartException.new(error)
121
- when INVENTORY_INVALID_PRODUCT_ERROR_CODE, INVENTORY_NOT_AVAILABLE_PRODUCT_ERROR_CODE
122
- raise InventoryErrorException.new(error)
123
- else
124
- raise RequestErrorException.new(error)
125
- end
126
- end
127
- end
128
-
129
- end
130
- end
@@ -1,100 +0,0 @@
1
- require 'tradenity/http/client'
2
-
3
- module Tradenity
4
-
5
- class IdentifiableResource
6
-
7
- def id=(id)
8
- @id = id
9
- end
10
-
11
- def id
12
- if instance_variable_defined?('@id') && @id != nil
13
- @id
14
- elsif _links && _links.has_key?('self') && _links['self'].has_key?('href')
15
- href = _links['self']['href']
16
- @id = href.split('/')[-1]
17
- @id
18
- else
19
- ''
20
- end
21
- end
22
-
23
- end
24
-
25
- class BaseEntity < IdentifiableResource
26
-
27
- def self.client
28
- HttpClient.get_instance
29
- end
30
-
31
- def self.resource_name
32
- ''
33
- end
34
-
35
- def self.resource_base_path
36
- "#{Tradenity.api_base}/#{resource_name}"
37
- end
38
-
39
- def self.resource_path(resource_id)
40
- "#{resource_base_path}/#{resource_id}"
41
- end
42
-
43
-
44
- def self.find_all(q={})
45
- search(q)
46
- end
47
-
48
- def self.find_one(q={})
49
- search(q).first
50
- end
51
-
52
- def self.find_by_id(id)
53
- result = client.get(resource_path(id), {})
54
- new(result)
55
- end
56
-
57
-
58
-
59
- def get_create_data
60
- attributes
61
- end
62
-
63
- def get_update_data
64
- attributes
65
- end
66
-
67
- def create
68
- result = self.class.client.post(self.class.resource_base_path, get_create_data)
69
- self.class.new(result)
70
- end
71
-
72
- def update
73
- result = self.class.client.put(self.class.resource_path(id), get_update_data)
74
- self.class.new(result)
75
- end
76
-
77
- def delete
78
- self.class.client.delete(self.class.resource_path(id), {})
79
- end
80
-
81
- protected
82
- def self.search(q={})
83
- result = client.get(resource_base_path, q)
84
- if result.has_key? '_embedded' # && result['_embedded'].has_key?(resource_name)
85
- collection = result['_embedded'][resource_name]
86
- page_info = result['page']
87
- puts page_info
88
-
89
- cats = collection.collect do |c|
90
- new(c)
91
- end
92
-
93
- Page.new(cats, page_info['number'], page_info['size'])
94
- else
95
- Page.new([], 0, 0)
96
- end
97
- end
98
-
99
- end
100
- end
@@ -1,390 +0,0 @@
1
- require 'virtus'
2
- require 'bcrypt'
3
- require 'tradenity/model/base'
4
-
5
- module Tradenity
6
-
7
-
8
- class Address < BaseEntity
9
- include Virtus.model
10
-
11
- def self.resource_name
12
- 'addresses'
13
- end
14
-
15
- attribute :streetLine1, String
16
- attribute :streetLine2, String
17
- attribute :city, String
18
- attribute :state, String
19
- attribute :zipCode, String
20
- attribute :country, String
21
- attribute :createdAt, DateTime
22
- attribute :updatedAt, DateTime
23
- attribute :_links, Hash
24
-
25
- def to_hash(prefix=nil)
26
- data = attributes.select {|key, value| [:streetLine1, :streetLine2, :city, :state, :zipCode, :country].include?(key) }
27
- if prefix != nil
28
- prefixed_data = {}
29
- data.each do |key, value|
30
- prefixed_data["#{prefix}#{key}"] = value
31
- end
32
- prefixed_data
33
- else
34
- data
35
- end
36
- end
37
-
38
- end
39
-
40
-
41
- class Brand < BaseEntity
42
- include Virtus.model
43
-
44
- def self.resource_name
45
- 'brands'
46
- end
47
-
48
- attribute :name, String
49
- attribute :title, String
50
- attribute :status, String
51
- attribute :description, String
52
- attribute :createdAt, DateTime
53
- attribute :updatedAt, DateTime
54
- attribute :_links, Hash
55
-
56
-
57
- end
58
-
59
-
60
- class Category < BaseEntity
61
- include Virtus.model
62
-
63
- def self.resource_name
64
- 'categories'
65
- end
66
-
67
- attribute :name, String
68
- attribute :title, String
69
- attribute :status, String
70
- attribute :description, String
71
- attribute :depth, Integer
72
- attribute :position, Integer
73
- attribute :createdAt, DateTime
74
- attribute :updatedAt, DateTime
75
- attribute :_links, Hash
76
- end
77
-
78
-
79
- class Currency < BaseEntity
80
- include Virtus.model
81
-
82
- def self.resource_name
83
- 'currencies'
84
- end
85
-
86
- attribute :code, String
87
- attribute :title, String
88
- attribute :status, String
89
- attribute :exchangeRate, Float
90
- attribute :createdAt, DateTime
91
- attribute :updatedAt, DateTime
92
- attribute :_links, Hash
93
-
94
- end
95
-
96
-
97
- class Customer < BaseEntity
98
- include Virtus.model
99
-
100
- def self.resource_name
101
- 'customers'
102
- end
103
-
104
- attribute :firstName, String
105
- attribute :lastName, String
106
- attribute :email, String
107
- attribute :username, String
108
- attribute :password, String
109
- attribute :status, String
110
- attribute :createdAt, DateTime
111
- attribute :updatedAt, DateTime
112
- attribute :_links, Hash
113
-
114
- def self.find_by_username(username)
115
- find_one(username: username)
116
- end
117
-
118
- def valid_password?(pass)
119
- BCrypt::Password.new(password) == pass
120
- end
121
-
122
- def get_create_data
123
- data = attributes.select {|key, value| [:firstName, :lastName, :email, :username, :password].include?(key) }
124
- data[:status] = 'active'
125
- data
126
- end
127
-
128
- end
129
-
130
-
131
- class Gateway < BaseEntity
132
- include Virtus.model
133
-
134
- def self.resource_name
135
- 'gateways'
136
- end
137
-
138
- attribute :name, String
139
- attribute :title, String
140
- attribute :status, String
141
- attribute :description, String
142
- attribute :mode, String
143
- attribute :accountId, String
144
- attribute :secretKey, String
145
- attribute :publicKey, String
146
- attribute :testSecretKey, String
147
- attribute :testPublicKey, String
148
- attribute :createdAt, DateTime
149
- attribute :updatedAt, DateTime
150
- attribute :_links, Hash
151
-
152
- end
153
-
154
-
155
- class Photo
156
- include Virtus.model
157
-
158
- attribute :name, String
159
- attribute :url, String
160
- attribute :createdAt, DateTime
161
- attribute :updatedAt, DateTime
162
- end
163
-
164
- class Dimensions
165
- include Virtus.model
166
-
167
- attribute :unit, String
168
- attribute :width, Float
169
- attribute :height, Float
170
- attribute :depth, Float
171
- end
172
-
173
- class Weight
174
- include Virtus.model
175
-
176
- attribute :unit, String
177
- attribute :amount, Float
178
- end
179
-
180
- class Product < BaseEntity
181
- include Virtus.model
182
-
183
- def self.resource_name
184
- 'products'
185
- end
186
-
187
- attribute :name, String
188
- attribute :title, String
189
- attribute :status, String
190
- attribute :sku, String
191
- attribute :description, String
192
- attribute :shortDescription, String
193
- attribute :fullDescription, String
194
- attribute :specifications, String
195
- attribute :stockLevel, Integer
196
- attribute :minimumStockLevel, Integer
197
- attribute :hasSellLimit, Boolean
198
- attribute :maximumSellCount, Integer
199
- attribute :stockStatus, String
200
- attribute :price, Float
201
- attribute :itemWeight, Weight
202
- attribute :packageWeight, Weight
203
- attribute :itemDimensions, Dimensions
204
- attribute :packageDimensions, Dimensions
205
- attribute :createdAt, DateTime
206
- attribute :updatedAt, DateTime
207
- attribute :mainPhoto, Photo
208
- attribute :photos, Array[Photo]
209
- attribute :_links, Hash
210
- end
211
-
212
-
213
- class Collection < BaseEntity
214
- include Virtus.model
215
-
216
- def self.resource_name
217
- 'collections'
218
- end
219
-
220
- attribute :name, String
221
- attribute :title, String
222
- attribute :status, String
223
- attribute :description, String
224
- attribute :createdAt, DateTime
225
- attribute :updatedAt, DateTime
226
- attribute :products, Array[Product]
227
- attribute :_links, Hash
228
- end
229
-
230
-
231
- class LineItem < IdentifiableResource
232
- include Virtus.model
233
-
234
- attribute :quantity, Integer
235
- attribute :unitPrice, Float
236
- attribute :subtotal, Float
237
- attribute :total, Float
238
- attribute :taxes, Float
239
- attribute :shippingCost, Float
240
- attribute :product, Product
241
- attribute :_links, Hash
242
-
243
- end
244
-
245
- class ShippingMethod < BaseEntity
246
- include Virtus.model
247
-
248
- def self.resource_name
249
- 'shippingMethods'
250
- end
251
-
252
- attribute :name, String
253
- attribute :title, String
254
- attribute :status, String
255
- attribute :description, String
256
- attribute :costType, String
257
- attribute :fixedRate, Float
258
- attribute :costPerUnitWeight, Float
259
- attribute :weightUnit, String
260
- attribute :_links, Hash
261
- end
262
-
263
- class ShippingOption
264
- include Virtus.model
265
-
266
- attribute :shippingMethod, ShippingMethod
267
- attribute :cost, Float
268
- attribute :orderTotal, Float
269
- end
270
-
271
- class ShoppingCart < BaseEntity
272
- include Virtus.model
273
-
274
- def self.resource_name
275
- 'shoppingCarts'
276
- end
277
-
278
- attribute :total, Float
279
- attribute :subtotal, Float
280
- attribute :count, Integer
281
- attribute :items, Array[LineItem]
282
- attribute :_links, Hash
283
-
284
- def self.get
285
- new(client.get(resource_base_path))
286
- end
287
-
288
- def self.add(item)
289
- new(client.post(resource_base_path, {product: item.product.id, quantity: item.quantity}))
290
- end
291
-
292
- def self.remove(item_id)
293
- new(client.delete("#{resource_base_path}/#{item_id}"))
294
- end
295
- end
296
-
297
- class Checkout
298
- include Virtus.model
299
-
300
- attribute :subtotal, Float
301
- attribute :total, Float
302
- attribute :taxes, Float
303
- attribute :items, Array[LineItem]
304
- attribute :shippingOptions, Array[ShippingOption]
305
- attribute :currency, Currency
306
- attribute :gateway, Gateway
307
- end
308
-
309
- class Order < BaseEntity
310
- include Virtus.model
311
-
312
- def self.resource_name
313
- 'orders'
314
- end
315
-
316
- attribute :count, Integer
317
- attribute :total, Float
318
- attribute :subtotal, Float
319
- attribute :shippingCost, Float
320
- attribute :items, Array[LineItem]
321
- attribute :customer, Customer
322
- attribute :billingAddress, Address
323
- attribute :shippingAddress, Address
324
- attribute :status, String
325
- attribute :createdAt, DateTime
326
- attribute :updatedAt, DateTime
327
- attribute :purchasedAt, DateTime
328
- attribute :completedAt, DateTime
329
- attribute :_links, Hash
330
-
331
- def self.find_all_by_customer(customer_id)
332
- find_all(customer: customer_id)
333
- end
334
-
335
- def get_checkout_data
336
- data = {customer: customer.id}
337
- data.merge!(billingAddress.to_hash('billingAddress.'))
338
- data.merge!(shippingAddress.to_hash('shippingAddress.'))
339
- data
340
- end
341
-
342
- def checkout(payment_source)
343
- data = get_checkout_data
344
- data['paymentSource'] = payment_source
345
- result = self.class.client.post(self.class.resource_base_path, data)
346
- Transaction.new(result)
347
- end
348
-
349
- def self.refund(order_id)
350
- result = client.put("#{resource_base_path}/#{order_id}/refund", data={})
351
- Transaction.new(result)
352
- end
353
-
354
- end
355
-
356
- class Tax < BaseEntity
357
- include Virtus.model
358
-
359
- def self.resource_name
360
- 'taxes'
361
- end
362
-
363
- attribute :name, String
364
- attribute :title, String
365
- attribute :status, String
366
- attribute :description, String
367
- attribute :createdAt, DateTime
368
- attribute :updatedAt, DateTime
369
- attribute :_links, Hash
370
-
371
- end
372
-
373
-
374
- class Transaction < BaseEntity
375
- include Virtus.model
376
-
377
- def self.resource_name
378
- 'transactions'
379
- end
380
-
381
- attribute :type, String
382
- attribute :status, String
383
- attribute :gatewayOperationId, String
384
- attribute :order, Order
385
- attribute :gateway, Gateway
386
- attribute :createdAt, DateTime
387
- attribute :updatedAt, DateTime
388
- attribute :_links, Hash
389
- end
390
- end
@@ -1,23 +0,0 @@
1
- module Tradenity
2
-
3
- class Page < Array
4
- def initialize(content=[], page=0, size=0)
5
- concat(content)
6
- @page_number = page
7
- @page_size = size
8
- end
9
-
10
- def is_empty?
11
- @page_size <= 0
12
- end
13
- end
14
-
15
-
16
- class PageRequest
17
- def initialize(page, size)
18
- @page_number = page
19
- @page_size = size
20
- end
21
- end
22
-
23
- end