tinify 1.4.0 → 1.5.0
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.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/CHANGES.md +5 -0
- data/Gemfile.lock +2 -2
- data/lib/data/cacert.pem +462 -900
- data/lib/tinify/client.rb +26 -15
- data/lib/tinify/version.rb +1 -1
- data/test/tinify_client_test.rb +77 -7
- data/update-cacert.sh +17 -0
- metadata +4 -2
data/lib/tinify/client.rb
CHANGED
@@ -4,7 +4,12 @@ require "json"
|
|
4
4
|
module Tinify
|
5
5
|
class Client
|
6
6
|
API_ENDPOINT = "https://api.tinify.com".freeze
|
7
|
+
|
8
|
+
RETRY_COUNT = 1
|
9
|
+
RETRY_DELAY = 500
|
10
|
+
|
7
11
|
USER_AGENT = "Tinify/#{VERSION} Ruby/#{RUBY_VERSION}p#{RUBY_PATCHLEVEL} (#{defined?(RUBY_ENGINE) ? RUBY_ENGINE : "unknown"})".freeze
|
12
|
+
|
8
13
|
CA_BUNDLE = File.expand_path("../../data/cacert.pem", __FILE__).freeze
|
9
14
|
|
10
15
|
def initialize(key, app_identifier = nil, proxy = nil)
|
@@ -24,7 +29,8 @@ module Tinify
|
|
24
29
|
@client.ssl_config.add_trust_ca(CA_BUNDLE)
|
25
30
|
end
|
26
31
|
|
27
|
-
def request(method, url, body = nil
|
32
|
+
def request(method, url, body = nil)
|
33
|
+
header = {}
|
28
34
|
if Hash === body
|
29
35
|
if body.empty?
|
30
36
|
body = nil
|
@@ -34,26 +40,31 @@ module Tinify
|
|
34
40
|
end
|
35
41
|
end
|
36
42
|
|
37
|
-
|
38
|
-
|
39
|
-
rescue HTTPClient::TimeoutError => err
|
40
|
-
raise ConnectionError.new("Timeout while connecting")
|
41
|
-
rescue StandardError => err
|
42
|
-
raise ConnectionError.new("Error while connecting: #{err.message}")
|
43
|
-
end
|
43
|
+
RETRY_COUNT.downto(0) do |retries|
|
44
|
+
sleep RETRY_DELAY / 1000.0 if retries < RETRY_COUNT
|
44
45
|
|
45
|
-
|
46
|
-
|
47
|
-
|
46
|
+
begin
|
47
|
+
response = @client.request(method, url, body: body, header: header)
|
48
|
+
rescue HTTPClient::TimeoutError => err
|
49
|
+
next if retries > 0
|
50
|
+
raise ConnectionError.new("Timeout while connecting")
|
51
|
+
rescue StandardError => err
|
52
|
+
next if retries > 0
|
53
|
+
raise ConnectionError.new("Error while connecting: #{err.message}")
|
54
|
+
end
|
55
|
+
|
56
|
+
if count = response.headers["Compression-Count"]
|
57
|
+
Tinify.compression_count = count.to_i
|
58
|
+
end
|
59
|
+
|
60
|
+
return response if response.ok?
|
48
61
|
|
49
|
-
if response.ok?
|
50
|
-
response
|
51
|
-
else
|
52
62
|
details = begin
|
53
63
|
JSON.parse(response.body)
|
54
64
|
rescue StandardError => err
|
55
|
-
{
|
65
|
+
{"message" => "Error while parsing response: #{err.message}", "error" => "ParseError"}
|
56
66
|
end
|
67
|
+
next if retries > 0 and response.status >= 500
|
57
68
|
raise Error.create(details["message"], details["error"], response.status)
|
58
69
|
end
|
59
70
|
end
|
data/lib/tinify/version.rb
CHANGED
data/test/tinify_client_test.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
require File.expand_path("../helper", __FILE__)
|
2
2
|
|
3
3
|
describe Tinify::Client do
|
4
|
+
Tinify::Client.send(:remove_const, :RETRY_DELAY)
|
5
|
+
Tinify::Client.const_set(:RETRY_DELAY, 10)
|
6
|
+
|
4
7
|
subject do
|
5
8
|
Tinify::Client.new("key")
|
6
9
|
end
|
@@ -96,7 +99,19 @@ describe Tinify::Client do
|
|
96
99
|
end
|
97
100
|
end
|
98
101
|
|
99
|
-
describe "with timeout" do
|
102
|
+
describe "with timeout once" do
|
103
|
+
before do
|
104
|
+
stub_request(:get, "https://api:key@api.tinify.com").to_timeout
|
105
|
+
.then.to_return(status: 201)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should return response" do
|
109
|
+
response = subject.request(:get, "/")
|
110
|
+
assert_equal "", response.body
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe "with timeout repeatedly" do
|
100
115
|
before do
|
101
116
|
stub_request(:get, "https://api:key@api.tinify.com").to_timeout
|
102
117
|
end
|
@@ -114,7 +129,20 @@ describe Tinify::Client do
|
|
114
129
|
end
|
115
130
|
end
|
116
131
|
|
117
|
-
describe "with socket error" do
|
132
|
+
describe "with socket error once" do
|
133
|
+
before do
|
134
|
+
stub_request(:get, "https://api:key@api.tinify.com")
|
135
|
+
.to_raise(SocketError.new("nodename nor servname provided"))
|
136
|
+
.then.to_return(status: 201)
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should return response" do
|
140
|
+
response = subject.request(:get, "/")
|
141
|
+
assert_equal "", response.body
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe "with socket error repeatedly" do
|
118
146
|
before do
|
119
147
|
stub_request(:get, "https://api:key@api.tinify.com").to_raise(SocketError.new("nodename nor servname provided"))
|
120
148
|
end
|
@@ -132,7 +160,20 @@ describe Tinify::Client do
|
|
132
160
|
end
|
133
161
|
end
|
134
162
|
|
135
|
-
describe "with unexpected error" do
|
163
|
+
describe "with unexpected error once" do
|
164
|
+
before do
|
165
|
+
stub_request(:get, "https://api:key@api.tinify.com")
|
166
|
+
.to_raise("some error")
|
167
|
+
.then.to_return(status: 201)
|
168
|
+
end
|
169
|
+
|
170
|
+
it "should return response" do
|
171
|
+
response = subject.request(:get, "/")
|
172
|
+
assert_equal "", response.body
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
describe "with unexpected error repeatedly" do
|
136
177
|
before do
|
137
178
|
stub_request(:get, "https://api:key@api.tinify.com").to_raise("some error")
|
138
179
|
end
|
@@ -150,7 +191,21 @@ describe Tinify::Client do
|
|
150
191
|
end
|
151
192
|
end
|
152
193
|
|
153
|
-
describe "with server error" do
|
194
|
+
describe "with server error once" do
|
195
|
+
before do
|
196
|
+
stub_request(:get, "https://api:key@api.tinify.com").to_return(
|
197
|
+
status: 584,
|
198
|
+
body: '{"error":"InternalServerError","message":"Oops!"}'
|
199
|
+
).then.to_return(status: 201)
|
200
|
+
end
|
201
|
+
|
202
|
+
it "should return response" do
|
203
|
+
response = subject.request(:get, "/")
|
204
|
+
assert_equal "", response.body
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
describe "with server error repeatedly" do
|
154
209
|
before do
|
155
210
|
stub_request(:get, "https://api:key@api.tinify.com").to_return(
|
156
211
|
status: 584,
|
@@ -171,7 +226,21 @@ describe Tinify::Client do
|
|
171
226
|
end
|
172
227
|
end
|
173
228
|
|
174
|
-
describe "with bad server response" do
|
229
|
+
describe "with bad server response once" do
|
230
|
+
before do
|
231
|
+
stub_request(:get, "https://api:key@api.tinify.com").to_return(
|
232
|
+
status: 543,
|
233
|
+
body: '<!-- this is not json -->'
|
234
|
+
).then.to_return(status: 201)
|
235
|
+
end
|
236
|
+
|
237
|
+
it "should return response" do
|
238
|
+
response = subject.request(:get, "/")
|
239
|
+
assert_equal "", response.body
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
describe "with bad server response repeatedly" do
|
175
244
|
before do
|
176
245
|
stub_request(:get, "https://api:key@api.tinify.com").to_return(
|
177
246
|
status: 543,
|
@@ -197,7 +266,7 @@ describe Tinify::Client do
|
|
197
266
|
stub_request(:get, "https://api:key@api.tinify.com").to_return(
|
198
267
|
status: 492,
|
199
268
|
body: '{"error":"BadRequest","message":"Oops!"}'
|
200
|
-
)
|
269
|
+
).then.to_return(status: 201)
|
201
270
|
end
|
202
271
|
|
203
272
|
it "should raise client error" do
|
@@ -213,12 +282,13 @@ describe Tinify::Client do
|
|
213
282
|
end
|
214
283
|
end
|
215
284
|
|
285
|
+
|
216
286
|
describe "with bad credentials" do
|
217
287
|
before do
|
218
288
|
stub_request(:get, "https://api:key@api.tinify.com").to_return(
|
219
289
|
status: 401,
|
220
290
|
body: '{"error":"Unauthorized","message":"Oops!"}'
|
221
|
-
)
|
291
|
+
).then.to_return(status: 201)
|
222
292
|
end
|
223
293
|
|
224
294
|
it "should raise account error" do
|
data/update-cacert.sh
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/bin/sh
|
2
|
+
dir=lib/data
|
3
|
+
|
4
|
+
cert=0
|
5
|
+
curl https://curl.haxx.se/ca/cacert.pem | while read line; do
|
6
|
+
if [ "-----BEGIN CERTIFICATE-----" == "$line" ]; then
|
7
|
+
cert=1
|
8
|
+
echo $line
|
9
|
+
elif [ "-----END CERTIFICATE-----" == "$line" ]; then
|
10
|
+
cert=0
|
11
|
+
echo $line
|
12
|
+
else
|
13
|
+
if [ $cert == 1 ]; then
|
14
|
+
echo $line
|
15
|
+
fi
|
16
|
+
fi
|
17
|
+
done > $dir/cacert.pem
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tinify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rolf Timmermans
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-02-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httpclient
|
@@ -89,6 +89,7 @@ extensions: []
|
|
89
89
|
extra_rdoc_files: []
|
90
90
|
files:
|
91
91
|
- ".travis.yml"
|
92
|
+
- CHANGES.md
|
92
93
|
- Gemfile
|
93
94
|
- Gemfile.lock
|
94
95
|
- LICENSE
|
@@ -112,6 +113,7 @@ files:
|
|
112
113
|
- test/tinify_source_test.rb
|
113
114
|
- test/tinify_test.rb
|
114
115
|
- tinify.gemspec
|
116
|
+
- update-cacert.sh
|
115
117
|
homepage: https://tinify.com/developers
|
116
118
|
licenses:
|
117
119
|
- MIT
|