lce 0.0.1

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.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +1 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/Gemfile +6 -0
  7. data/LICENSE +21 -0
  8. data/README.md +98 -0
  9. data/Rakefile +5 -0
  10. data/lce.gemspec +30 -0
  11. data/lib/lce.rb +72 -0
  12. data/lib/lce/client.rb +34 -0
  13. data/lib/lce/client/connection.rb +23 -0
  14. data/lib/lce/client/errors.rb +25 -0
  15. data/lib/lce/client/request.rb +80 -0
  16. data/lib/lce/offer.rb +25 -0
  17. data/lib/lce/order.rb +51 -0
  18. data/lib/lce/quote.rb +34 -0
  19. data/lib/lce/version.rb +4 -0
  20. data/lib/paginated_array.rb +25 -0
  21. data/spec/fixtures/access_denied +20 -0
  22. data/spec/fixtures/account_disabled +20 -0
  23. data/spec/fixtures/check +464 -0
  24. data/spec/fixtures/offers/available_delivery_locations/found +785 -0
  25. data/spec/fixtures/offers/find/found +66 -0
  26. data/spec/fixtures/offers/find/not_found +15 -0
  27. data/spec/fixtures/order_params +23 -0
  28. data/spec/fixtures/orders/all/page_1 +111 -0
  29. data/spec/fixtures/orders/find/found +106 -0
  30. data/spec/fixtures/orders/find/not_found +15 -0
  31. data/spec/fixtures/orders/labels/labels.pdf +0 -0
  32. data/spec/fixtures/orders/labels/response +0 -0
  33. data/spec/fixtures/orders/place/created +106 -0
  34. data/spec/fixtures/orders/place/empty_params +15 -0
  35. data/spec/fixtures/orders/place/missing_params +15 -0
  36. data/spec/fixtures/orders/tracking +30 -0
  37. data/spec/fixtures/quote_params +25 -0
  38. data/spec/fixtures/quotes/all/page_1 +11594 -0
  39. data/spec/fixtures/quotes/all/page_2 +1665 -0
  40. data/spec/fixtures/quotes/find/found +492 -0
  41. data/spec/fixtures/quotes/find/not_found +15 -0
  42. data/spec/fixtures/quotes/request/created +492 -0
  43. data/spec/fixtures/quotes/request/empty_quote +15 -0
  44. data/spec/fixtures/quotes/request/missing_params +15 -0
  45. data/spec/lce/client_spec.rb +41 -0
  46. data/spec/lce/offer_spec.rb +85 -0
  47. data/spec/lce/order_spec.rb +136 -0
  48. data/spec/lce/quote_spec.rb +111 -0
  49. data/spec/lce_spec.rb +109 -0
  50. data/spec/paginated_array_spec.rb +55 -0
  51. data/spec/spec_helper.rb +17 -0
  52. data/tasks/rspec.rake +3 -0
  53. metadata +239 -0
@@ -0,0 +1,51 @@
1
+ require 'hashie'
2
+ require 'paginated_array'
3
+
4
+ module Lce
5
+ class Order < Hashie::Mash
6
+ include Hashie::Extensions::Coercion
7
+
8
+ coerce_key :quote, Lce::Quote
9
+
10
+ class << self
11
+ def place(params)
12
+ response = Lce.client.post('orders', {order: params})
13
+ new(response)
14
+ end
15
+
16
+ def all(page = nil)
17
+ if page
18
+ page = 1 if page <= 0
19
+ options = {page: page}
20
+ end
21
+ response = Lce.client.get('orders', nil, nil, nil , options)
22
+ response.map! do |q|
23
+ new(q)
24
+ end
25
+ end
26
+
27
+ def find(id)
28
+ response = Lce.client.get('orders', id)
29
+ new(response)
30
+ end
31
+
32
+ end
33
+
34
+ def labels
35
+ @labels ||= Lce.client.get('orders', id, 'labels', 'pdf')
36
+ end
37
+
38
+ def write_labels(name = nil)
39
+ name = (name.is_a?(String)) ? name : "labels-#{id}.pdf"
40
+ File.open(name, "wb") do |f|
41
+ f.write(labels)
42
+ end
43
+ end
44
+
45
+ def tracking
46
+ Lce.client.get('orders', id, 'tracking').map! do |t|
47
+ Hashie::Mash.new(t)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,34 @@
1
+ require 'hashie'
2
+ require 'paginated_array'
3
+
4
+ module Lce
5
+ class Quote < Hashie::Mash
6
+ include Hashie::Extensions::Coercion
7
+
8
+ coerce_key :offers, Array[Lce::Offer]
9
+
10
+ class << self
11
+ def request(params)
12
+ response = Lce.client.post('quotes', {quote: params})
13
+ new(response)
14
+ end
15
+
16
+ def all(page = nil)
17
+ if page
18
+ page = 1 if page <= 0
19
+ options = {page: page}
20
+ end
21
+ response = Lce.client.get('quotes', nil, nil, nil , options)
22
+ response.map! do |q|
23
+ new(q)
24
+ end
25
+ end
26
+
27
+ def find(id)
28
+ response = Lce.client.get('quotes', id)
29
+ new(response)
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,4 @@
1
+ module Lce
2
+ VERSION = "0.0.1"
3
+ end
4
+
@@ -0,0 +1,25 @@
1
+ class PaginatedArray < Array
2
+ attr_reader :total_count, :current_page, :per_page
3
+
4
+
5
+ def initialize(total_count, current_page, per_page)
6
+ @total_count = total_count
7
+ @current_page = current_page
8
+ @per_page = per_page
9
+ super()
10
+ end
11
+
12
+ def total_page
13
+ (total_count.fdiv(per_page)).ceil
14
+ end
15
+
16
+ def next_page
17
+ next_page = current_page + 1
18
+ next_page <= total_page ? next_page : false
19
+ end
20
+
21
+ def previous_page
22
+ previous_page = current_page - 1
23
+ previous_page > 0 ? previous_page : false
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ HTTP/1.1 401 Unauthorized
2
+ Server: nginx/1.6.0
3
+ Date: Wed, 06 Aug 2014 12:11:35 GMT
4
+ Content-Type: application/json
5
+ Content-Length: 267
6
+ Connection: keep-alive
7
+ WWW-Authenticate: Basic realm="lce.io API"
8
+ Lce-Env: staging
9
+
10
+ {
11
+ "status":"failure",
12
+ "error":{
13
+ "type":"access_denied",
14
+ "message":"Access denied.",
15
+ "details":[
16
+ "Make sure you are using the correct credentials. Contacts us via support@lce.io if you need some help."
17
+ ]
18
+ },
19
+ "self":"https://test.lce.io/"
20
+ }
@@ -0,0 +1,20 @@
1
+ HTTP/1.1 403 Forbidden
2
+ Server: nginx/1.6.0
3
+ Date: Wed, 06 Aug 2014 12:52:25 GMT
4
+ Content-Type: application/json
5
+ Content-Length: 256
6
+ Connection: keep-alive
7
+ WWW-Authenticate: Basic realm="lce.io API"
8
+ Lce-Env: staging
9
+
10
+ {
11
+ "status":"failure",
12
+ "error":{
13
+ "type":"account_disabled",
14
+ "message":"Account disabled.",
15
+ "details":[
16
+ "Please contact our support team at support@lce.io if you need this account activated."
17
+ ]
18
+ },
19
+ "self":"https://test.lce.io/"
20
+ }
@@ -0,0 +1,464 @@
1
+ HTTP/1.1 200 OK
2
+ Server: nginx/1.6.0
3
+ Date: Wed, 06 Aug 2014 12:08:30 GMT
4
+ Content-Type: application/json
5
+ Content-Length: 11655
6
+ Connection: keep-alive
7
+ Lce-Env: staging
8
+
9
+ {
10
+ "status":"success",
11
+ "self":"https://test.lce.io/",
12
+ "data":{
13
+ "time":"2014-08-06T12:08:30Z",
14
+ "host":"test.lce.io",
15
+ "port":443,
16
+ "protocol":"https",
17
+ "routes":[
18
+ {
19
+ "method":"GET",
20
+ "path":"/v1/products(.:format)",
21
+ "version":"v1",
22
+ "description":"Returns a list of all products."
23
+ },
24
+ {
25
+ "method":"GET",
26
+ "path":"/v1/quotes(.:format)",
27
+ "version":"v1",
28
+ "description":"Returns a list of all quotes."
29
+ },
30
+ {
31
+ "method":"GET",
32
+ "path":"/v1/quotes/:id(.:format)",
33
+ "version":"v1",
34
+ "description":"Returns a specifig quote."
35
+ },
36
+ {
37
+ "method":"POST",
38
+ "path":"/v1/quotes(.:format)",
39
+ "version":"v1",
40
+ "description":"Request a new quote."
41
+ },
42
+ {
43
+ "method":"GET",
44
+ "path":"/v1/offers/:id(.:format)",
45
+ "version":"v1",
46
+ "description":"Returns a specific offer."
47
+ },
48
+ {
49
+ "method":"GET",
50
+ "path":"/v1/offers/:id/available_delivery_locations(.:format)",
51
+ "version":"v1",
52
+ "description":"Returns available delivery locations."
53
+ },
54
+ {
55
+ "method":"GET",
56
+ "path":"/v1/orders(.:format)",
57
+ "version":"v1",
58
+ "description":"Returns a list of all orders."
59
+ },
60
+ {
61
+ "method":"GET",
62
+ "path":"/v1/orders/:id(.:format)",
63
+ "version":"v1",
64
+ "description":"Returns a specific order."
65
+ },
66
+ {
67
+ "method":"GET",
68
+ "path":"/v1/orders/:id/tracking(.:format)",
69
+ "version":"v1",
70
+ "description":"Returns the tracking statuses of an order."
71
+ },
72
+ {
73
+ "method":"POST",
74
+ "path":"/v1/orders(.:format)",
75
+ "version":"v1",
76
+ "description":"Books a specific offer via an order."
77
+ },
78
+ {
79
+ "method":"POST",
80
+ "path":"/v1/orders/bulk(.:format)",
81
+ "version":"v1",
82
+ "description":"Book multiple orders."
83
+ },
84
+ {
85
+ "method":"GET",
86
+ "path":"/v1/orders/:id/labels(.:format)",
87
+ "version":"v1",
88
+ "description":"Returns the labels of an order."
89
+ },
90
+ {
91
+ "method":"GET",
92
+ "path":"/(.:format)",
93
+ "version":null,
94
+ "description":"Returns a description of the service."
95
+ },
96
+ {
97
+ "method":"OPTIONS",
98
+ "path":"/:version/products(.:format)(.:format)",
99
+ "version":null,
100
+ "description":null
101
+ },
102
+ {
103
+ "method":"PUT",
104
+ "path":"/:version/products(.:format)(.:format)",
105
+ "version":null,
106
+ "description":null
107
+ },
108
+ {
109
+ "method":"POST",
110
+ "path":"/:version/products(.:format)(.:format)",
111
+ "version":null,
112
+ "description":null
113
+ },
114
+ {
115
+ "method":"DELETE",
116
+ "path":"/:version/products(.:format)(.:format)",
117
+ "version":null,
118
+ "description":null
119
+ },
120
+ {
121
+ "method":"PATCH",
122
+ "path":"/:version/products(.:format)(.:format)",
123
+ "version":null,
124
+ "description":null
125
+ },
126
+ {
127
+ "method":"OPTIONS",
128
+ "path":"/:version/quotes(.:format)(.:format)",
129
+ "version":null,
130
+ "description":null
131
+ },
132
+ {
133
+ "method":"PUT",
134
+ "path":"/:version/quotes(.:format)(.:format)",
135
+ "version":null,
136
+ "description":null
137
+ },
138
+ {
139
+ "method":"DELETE",
140
+ "path":"/:version/quotes(.:format)(.:format)",
141
+ "version":null,
142
+ "description":null
143
+ },
144
+ {
145
+ "method":"PATCH",
146
+ "path":"/:version/quotes(.:format)(.:format)",
147
+ "version":null,
148
+ "description":null
149
+ },
150
+ {
151
+ "method":"OPTIONS",
152
+ "path":"/:version/quotes/:id(.:format)(.:format)",
153
+ "version":null,
154
+ "description":null
155
+ },
156
+ {
157
+ "method":"PUT",
158
+ "path":"/:version/quotes/:id(.:format)(.:format)",
159
+ "version":null,
160
+ "description":null
161
+ },
162
+ {
163
+ "method":"POST",
164
+ "path":"/:version/quotes/:id(.:format)(.:format)",
165
+ "version":null,
166
+ "description":null
167
+ },
168
+ {
169
+ "method":"DELETE",
170
+ "path":"/:version/quotes/:id(.:format)(.:format)",
171
+ "version":null,
172
+ "description":null
173
+ },
174
+ {
175
+ "method":"PATCH",
176
+ "path":"/:version/quotes/:id(.:format)(.:format)",
177
+ "version":null,
178
+ "description":null
179
+ },
180
+ {
181
+ "method":"OPTIONS",
182
+ "path":"/:version/offers/:id(.:format)(.:format)",
183
+ "version":null,
184
+ "description":null
185
+ },
186
+ {
187
+ "method":"PUT",
188
+ "path":"/:version/offers/:id(.:format)(.:format)",
189
+ "version":null,
190
+ "description":null
191
+ },
192
+ {
193
+ "method":"POST",
194
+ "path":"/:version/offers/:id(.:format)(.:format)",
195
+ "version":null,
196
+ "description":null
197
+ },
198
+ {
199
+ "method":"DELETE",
200
+ "path":"/:version/offers/:id(.:format)(.:format)",
201
+ "version":null,
202
+ "description":null
203
+ },
204
+ {
205
+ "method":"PATCH",
206
+ "path":"/:version/offers/:id(.:format)(.:format)",
207
+ "version":null,
208
+ "description":null
209
+ },
210
+ {
211
+ "method":"OPTIONS",
212
+ "path":"/:version/offers/:id/available_delivery_locations(.:format)(.:format)",
213
+ "version":null,
214
+ "description":null
215
+ },
216
+ {
217
+ "method":"PUT",
218
+ "path":"/:version/offers/:id/available_delivery_locations(.:format)(.:format)",
219
+ "version":null,
220
+ "description":null
221
+ },
222
+ {
223
+ "method":"POST",
224
+ "path":"/:version/offers/:id/available_delivery_locations(.:format)(.:format)",
225
+ "version":null,
226
+ "description":null
227
+ },
228
+ {
229
+ "method":"DELETE",
230
+ "path":"/:version/offers/:id/available_delivery_locations(.:format)(.:format)",
231
+ "version":null,
232
+ "description":null
233
+ },
234
+ {
235
+ "method":"PATCH",
236
+ "path":"/:version/offers/:id/available_delivery_locations(.:format)(.:format)",
237
+ "version":null,
238
+ "description":null
239
+ },
240
+ {
241
+ "method":"OPTIONS",
242
+ "path":"/:version/orders(.:format)(.:format)",
243
+ "version":null,
244
+ "description":null
245
+ },
246
+ {
247
+ "method":"PUT",
248
+ "path":"/:version/orders(.:format)(.:format)",
249
+ "version":null,
250
+ "description":null
251
+ },
252
+ {
253
+ "method":"DELETE",
254
+ "path":"/:version/orders(.:format)(.:format)",
255
+ "version":null,
256
+ "description":null
257
+ },
258
+ {
259
+ "method":"PATCH",
260
+ "path":"/:version/orders(.:format)(.:format)",
261
+ "version":null,
262
+ "description":null
263
+ },
264
+ {
265
+ "method":"OPTIONS",
266
+ "path":"/:version/orders/:id(.:format)(.:format)",
267
+ "version":null,
268
+ "description":null
269
+ },
270
+ {
271
+ "method":"PUT",
272
+ "path":"/:version/orders/:id(.:format)(.:format)",
273
+ "version":null,
274
+ "description":null
275
+ },
276
+ {
277
+ "method":"POST",
278
+ "path":"/:version/orders/:id(.:format)(.:format)",
279
+ "version":null,
280
+ "description":null
281
+ },
282
+ {
283
+ "method":"DELETE",
284
+ "path":"/:version/orders/:id(.:format)(.:format)",
285
+ "version":null,
286
+ "description":null
287
+ },
288
+ {
289
+ "method":"PATCH",
290
+ "path":"/:version/orders/:id(.:format)(.:format)",
291
+ "version":null,
292
+ "description":null
293
+ },
294
+ {
295
+ "method":"OPTIONS",
296
+ "path":"/:version/orders/:id/tracking(.:format)(.:format)",
297
+ "version":null,
298
+ "description":null
299
+ },
300
+ {
301
+ "method":"PUT",
302
+ "path":"/:version/orders/:id/tracking(.:format)(.:format)",
303
+ "version":null,
304
+ "description":null
305
+ },
306
+ {
307
+ "method":"POST",
308
+ "path":"/:version/orders/:id/tracking(.:format)(.:format)",
309
+ "version":null,
310
+ "description":null
311
+ },
312
+ {
313
+ "method":"DELETE",
314
+ "path":"/:version/orders/:id/tracking(.:format)(.:format)",
315
+ "version":null,
316
+ "description":null
317
+ },
318
+ {
319
+ "method":"PATCH",
320
+ "path":"/:version/orders/:id/tracking(.:format)(.:format)",
321
+ "version":null,
322
+ "description":null
323
+ },
324
+ {
325
+ "method":"OPTIONS",
326
+ "path":"/:version/orders/bulk(.:format)(.:format)",
327
+ "version":null,
328
+ "description":null
329
+ },
330
+ {
331
+ "method":"GET",
332
+ "path":"/:version/orders/bulk(.:format)(.:format)",
333
+ "version":null,
334
+ "description":null
335
+ },
336
+ {
337
+ "method":"PUT",
338
+ "path":"/:version/orders/bulk(.:format)(.:format)",
339
+ "version":null,
340
+ "description":null
341
+ },
342
+ {
343
+ "method":"DELETE",
344
+ "path":"/:version/orders/bulk(.:format)(.:format)",
345
+ "version":null,
346
+ "description":null
347
+ },
348
+ {
349
+ "method":"PATCH",
350
+ "path":"/:version/orders/bulk(.:format)(.:format)",
351
+ "version":null,
352
+ "description":null
353
+ },
354
+ {
355
+ "method":"HEAD",
356
+ "path":"/:version/orders/bulk(.:format)(.:format)",
357
+ "version":null,
358
+ "description":null
359
+ },
360
+ {
361
+ "method":"OPTIONS",
362
+ "path":"/:version/orders/:id/labels(.:format)(.:format)",
363
+ "version":null,
364
+ "description":null
365
+ },
366
+ {
367
+ "method":"PUT",
368
+ "path":"/:version/orders/:id/labels(.:format)(.:format)",
369
+ "version":null,
370
+ "description":null
371
+ },
372
+ {
373
+ "method":"POST",
374
+ "path":"/:version/orders/:id/labels(.:format)(.:format)",
375
+ "version":null,
376
+ "description":null
377
+ },
378
+ {
379
+ "method":"DELETE",
380
+ "path":"/:version/orders/:id/labels(.:format)(.:format)",
381
+ "version":null,
382
+ "description":null
383
+ },
384
+ {
385
+ "method":"PATCH",
386
+ "path":"/:version/orders/:id/labels(.:format)(.:format)",
387
+ "version":null,
388
+ "description":null
389
+ },
390
+ {
391
+ "method":"OPTIONS",
392
+ "path":"/(.:format)(.:format)",
393
+ "version":null,
394
+ "description":null
395
+ },
396
+ {
397
+ "method":"PUT",
398
+ "path":"/(.:format)(.:format)",
399
+ "version":null,
400
+ "description":null
401
+ },
402
+ {
403
+ "method":"POST",
404
+ "path":"/(.:format)(.:format)",
405
+ "version":null,
406
+ "description":null
407
+ },
408
+ {
409
+ "method":"DELETE",
410
+ "path":"/(.:format)(.:format)",
411
+ "version":null,
412
+ "description":null
413
+ },
414
+ {
415
+ "method":"PATCH",
416
+ "path":"/(.:format)(.:format)",
417
+ "version":null,
418
+ "description":null
419
+ },
420
+ {
421
+ "method":"OPTIONS",
422
+ "path":"/*path(.:format)(.:format)",
423
+ "version":null,
424
+ "description":null
425
+ },
426
+ {
427
+ "method":"GET",
428
+ "path":"/*path(.:format)(.:format)",
429
+ "version":null,
430
+ "description":null
431
+ },
432
+ {
433
+ "method":"PUT",
434
+ "path":"/*path(.:format)(.:format)",
435
+ "version":null,
436
+ "description":null
437
+ },
438
+ {
439
+ "method":"POST",
440
+ "path":"/*path(.:format)(.:format)",
441
+ "version":null,
442
+ "description":null
443
+ },
444
+ {
445
+ "method":"DELETE",
446
+ "path":"/*path(.:format)(.:format)",
447
+ "version":null,
448
+ "description":null
449
+ },
450
+ {
451
+ "method":"PATCH",
452
+ "path":"/*path(.:format)(.:format)",
453
+ "version":null,
454
+ "description":null
455
+ },
456
+ {
457
+ "method":"HEAD",
458
+ "path":"/*path(.:format)(.:format)",
459
+ "version":null,
460
+ "description":null
461
+ }
462
+ ]
463
+ }
464
+ }