tindie_api 0.1.8 → 0.1.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -1
- data/lib/tindie_api/version.rb +1 -1
- data/lib/tindie_api.rb +35 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e7f2772b5cbd8199e679fee7a49471f9736a47c5403134561071e41d2c104991
|
4
|
+
data.tar.gz: b1bceb5dab129206977631fba20b0ed90b80173f7fafccaa58303aa2983febb3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 70485fff29c3b8f7ed10d722102a19b5e71ff6cf9ea62621f3aee9025fa1f5bc93479d09349164d6502bed3373c6fb0fb4e09a55a9cae3bcafbe59d3d6d87cf2
|
7
|
+
data.tar.gz: 4f75114e6970805f7ee15c2dcd05086e941cb856c703d5061e0ab89c8d9ba2c27e51c1c3d3bd9444fdbec4ebbf5c01918b88f7d2200ef900b131d6290ea19633
|
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.
|
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
|
|
data/lib/tindie_api/version.rb
CHANGED
data/lib/tindie_api.rb
CHANGED
@@ -65,26 +65,56 @@ 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
|
+
if response.nil? || response.empty?
|
86
|
+
raise JSON::ParserError, "Empty response from Tindie API"
|
87
|
+
end
|
88
|
+
|
80
89
|
JSON.parse(response)
|
90
|
+
rescue JSON::ParserError => e
|
91
|
+
puts "Failed to parse JSON: #{response}"
|
92
|
+
raise e
|
81
93
|
end
|
82
94
|
|
83
95
|
def get_orders(shipped = nil)
|
84
96
|
raise ArgumentError, "shipped must be true, false, or nil" if !shipped.nil? && ![true, false].include?(shipped)
|
85
|
-
result = get_orders_json(shipped)
|
86
|
-
|
87
|
-
|
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
|
88
118
|
end
|
89
119
|
|
90
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.
|
4
|
+
version: 0.1.10
|
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-
|
11
|
+
date: 2024-12-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|