tindie_api 0.1.9 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1779e3faf74733ae9d38c9e079b0cd48580d0b8d4d60524e653930b8bff4ba90
4
- data.tar.gz: a030c9c6440f8ee5383ed0a9f7d41b1a29c9f569054aaa9d61c3d721df8cb52a
3
+ metadata.gz: 36daa88fb4fb4d1f4db8b756fed22867831dcbb78e41bdba37de2b7d27f38c92
4
+ data.tar.gz: aabeb3fdbf7fd8196f6ef426d4254581999f99f2b1980efbb83542dd164e36ff
5
5
  SHA512:
6
- metadata.gz: 9805356be076b7d086d3ea557401ca2543f198f6ded69c5f858c1367c6620dee0936741e33a2c5c35dfbc22ebffe62b8036e552066fe662a821d0ac25feaccd6
7
- data.tar.gz: 2d507819ce2ce35d0e910c283d513d04f62269af140bd260eaec0a5eb27311f0a410a327e0ccf07bf8cc2acd28b5be63c24ff010b28800089c4a984f3fe1ff53
6
+ metadata.gz: d7f25354104e21579d7982124b746041f9e0fb6178cd6f4412e4f480c3438e3ca31df521a13c805d285a1bcf497af8466c89efe1acd08db1be87c19f68df86a6
7
+ data.tar.gz: 264a53ac6ae677d480a9b98273ad5a8e80d4043d040a957e1ddb71671b0ffb2f970344ff5fb4792a2fd2c96fcfb8c89c97158d2d72c98df5c9b57ba7cf5f25a6
data/README.md CHANGED
@@ -28,7 +28,7 @@ get "/orders" do
28
28
  @api = TindieApi::TindieOrdersAPI.new(@username, @api_key)
29
29
 
30
30
  # false means unshipped
31
- orders = @api.get_orders_json(false)
31
+ orders = @api.get_all_orders(false)
32
32
 
33
33
  puts orders.inspect
34
34
 
@@ -36,6 +36,13 @@ get "/orders" do
36
36
  end
37
37
  ```
38
38
 
39
+ Note that the Tindie api uses pagination with 20 items (default) and 50 items (max).
40
+
41
+ There are 3 ways to get orders depending on the level of abstraction you desire
42
+
43
+ - `get_orders` (Returns TindieAPI Objects)
44
+ - `get_orders_json` (Returns Json)
45
+ - `get_all_orders` (Returns TindieAPI Objects)
39
46
 
40
47
  ## Examples
41
48
 
@@ -1,3 +1,3 @@
1
1
  module TindieApi
2
- VERSION = "0.1.9"
2
+ VERSION = "0.1.11"
3
3
  end
data/lib/tindie_api.rb CHANGED
@@ -32,10 +32,9 @@ module TindieApi
32
32
  def initialize(data)
33
33
  @json_parsed = data
34
34
  @date = DateTime.parse(data['date'])
35
- @date_shipped = DateTime.parse(data['date_shipped'])
36
35
  @products = data['items'].map { |i| TindieProduct.new(i) }
37
- @shipped = data['shipped']
38
36
  @refunded = data['refunded']
37
+ @shipped = data['shipped']
39
38
  @order_number = data['number']
40
39
  @recipient_email = data['email']
41
40
  @recipient_phone = data['phone']
@@ -58,6 +57,7 @@ module TindieApi
58
57
  @tindie_fee = data['total_tindiefee']
59
58
  @cc_fee = data['total_ccfee']
60
59
  if @shipped
60
+ @date_shipped = DateTime.parse(data['date_shipped'])
61
61
  @tracking_code = data['tracking_code']
62
62
  @tracking_url = data['tracking_url']
63
63
  end
@@ -65,15 +65,20 @@ module TindieApi
65
65
  end
66
66
 
67
67
  class TindieOrdersAPI
68
+ MAX_ITEMS_PER_REQUEST = 50
69
+
68
70
  def initialize(username, api_key)
69
71
  @usr = username
70
72
  @api = api_key
71
73
  @cache = { false => nil, true => nil, nil => nil }
72
74
  end
73
75
 
74
- def get_orders_json(shipped = nil)
76
+ def get_orders_json(shipped = nil, limit = nil, offset = nil)
75
77
  url = "https://www.tindie.com/api/v1/order/?format=json&api_key=#{@api}&username=#{@usr}"
76
78
  url += "&shipped=#{shipped}" unless shipped.nil?
79
+ url += "&limit=#{limit}" unless limit.nil?
80
+ url += "&offset=#{offset}" unless offset.nil?
81
+
77
82
  uri = URI(url)
78
83
  response = Net::HTTP.get(uri)
79
84
 
@@ -85,13 +90,31 @@ module TindieApi
85
90
  rescue JSON::ParserError => e
86
91
  puts "Failed to parse JSON: #{response}"
87
92
  raise e
88
- end
93
+ end
89
94
 
90
95
  def get_orders(shipped = nil)
91
96
  raise ArgumentError, "shipped must be true, false, or nil" if !shipped.nil? && ![true, false].include?(shipped)
92
- result = get_orders_json(shipped)['orders'].map { |i| TindieOrder.new(i) }
93
- @cache[shipped] = [Time.now + EXPIRES_TIME, result]
94
- result
97
+ result = get_orders_json(shipped)
98
+ orders = result['orders'].map { |i| TindieOrder.new(i) }
99
+ @cache[shipped] = [Time.now + EXPIRES_TIME, orders]
100
+ orders
101
+ end
102
+
103
+ def get_all_orders(shipped = nil)
104
+ results = []
105
+ offset = 0
106
+
107
+ loop do
108
+ response = get_orders_json(shipped, MAX_ITEMS_PER_REQUEST, offset)
109
+ orders = response['orders'].map { |i| TindieOrder.new(i) }
110
+ results.concat(orders)
111
+
112
+ break if response['meta'].nil? || response['meta']['next'].nil?
113
+ offset += MAX_ITEMS_PER_REQUEST
114
+ sleep(0.5)
115
+ end
116
+
117
+ results
95
118
  end
96
119
 
97
120
  def _get_cache_(shipped = nil)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tindie_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.1.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Spencer Owen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-12-26 00:00:00.000000000 Z
11
+ date: 2025-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler