tinify 1.2.0 → 1.3.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 +8 -8
- data/Gemfile.lock +2 -2
- data/LICENSE +1 -1
- data/lib/tinify.rb +4 -4
- data/lib/tinify/source.rb +5 -0
- data/lib/tinify/version.rb +1 -1
- data/test/examples/voormedia.png +0 -0
- data/test/integration.rb +49 -9
- data/test/tinify_source_test.rb +158 -46
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
OTg3Y2VkOGIxZWJjNjVjNzIzOGU0ZWM4NjQ1MDE0ZGEyNzUyOGM4ZQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NzJiYjliMmNhYTQ1ZGJjN2FjMDdmNTNiYTcxNzIyZmYyZDU2MDM0Yw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ODI3MmJmMDhlYTg2MGE1YzVlOWYzYjRkNjAwMDRiNjMzNjlkZWJhODgzM2Rk
|
10
|
+
NDE1Njk3ODc2OWZkODlhNjNiNGYyMjE2N2I4MTUxMzNmODRhYTk5ODVlOWRm
|
11
|
+
YTc0MTVkM2NiZDQ1NjNhMDRmMTFlM2FlNGU5YjliZWQwODRiMTg=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MzIxZTJlZWQwMDZhMzU4ZDkyODA3N2I5M2JiNGEwZTM4ODI0NjcxYTcxMWZj
|
14
|
+
ZTBjYjc5ZGJlYjg5MmU5MGZlZDVlYmI4YmViYjMxY2QzYWQyNWFlMWZmY2I3
|
15
|
+
YjY0N2Q3ZmU0YTQzNjk1ZTQ3NWI1YTEyOTMwNTgyYzM1MDdkNmI=
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
tinify (1.
|
4
|
+
tinify (1.3.0)
|
5
5
|
httpclient (~> 2.6)
|
6
6
|
|
7
7
|
GEM
|
@@ -10,7 +10,7 @@ GEM
|
|
10
10
|
addressable (2.3.6)
|
11
11
|
crack (0.4.2)
|
12
12
|
safe_yaml (~> 1.0.0)
|
13
|
-
httpclient (2.7.
|
13
|
+
httpclient (2.7.1)
|
14
14
|
minitest (5.5.1)
|
15
15
|
rake (10.4.2)
|
16
16
|
safe_yaml (1.0.4)
|
data/LICENSE
CHANGED
data/lib/tinify.rb
CHANGED
@@ -10,8 +10,8 @@ require "thread"
|
|
10
10
|
|
11
11
|
module Tinify
|
12
12
|
class << self
|
13
|
-
|
14
|
-
|
13
|
+
attr_reader :key
|
14
|
+
attr_reader :app_identifier
|
15
15
|
attr_accessor :compression_count
|
16
16
|
|
17
17
|
def key=(key)
|
@@ -45,10 +45,10 @@ module Tinify
|
|
45
45
|
@@mutex = Mutex.new
|
46
46
|
|
47
47
|
def client
|
48
|
-
raise AccountError.new("Provide an API key with Tinify.key = ...") unless
|
48
|
+
raise AccountError.new("Provide an API key with Tinify.key = ...") unless key
|
49
49
|
return @client if @client
|
50
50
|
@@mutex.synchronize do
|
51
|
-
@client ||= Client.new(
|
51
|
+
@client ||= Client.new(key, app_identifier).freeze
|
52
52
|
end
|
53
53
|
end
|
54
54
|
end
|
data/lib/tinify/source.rb
CHANGED
@@ -20,6 +20,11 @@ module Tinify
|
|
20
20
|
@url, @commands = url.freeze, commands.freeze
|
21
21
|
end
|
22
22
|
|
23
|
+
def preserve(*options)
|
24
|
+
options = Array(options).flatten
|
25
|
+
self.class.new(@url, @commands.merge(preserve: options))
|
26
|
+
end
|
27
|
+
|
23
28
|
def resize(options)
|
24
29
|
self.class.new(@url, @commands.merge(resize: options))
|
25
30
|
end
|
data/lib/tinify/version.rb
CHANGED
data/test/examples/voormedia.png
CHANGED
Binary file
|
data/test/integration.rb
CHANGED
@@ -12,27 +12,67 @@ describe "client integration" do
|
|
12
12
|
optimized = Tinify.from_file(unoptimized_path)
|
13
13
|
|
14
14
|
it "should compress from file" do
|
15
|
-
Tempfile.open("optimized.png") do |file|
|
15
|
+
Tempfile.open("optimized.png", encoding: "binary") do |file|
|
16
16
|
optimized.to_file(file.path)
|
17
|
-
|
18
|
-
|
17
|
+
|
18
|
+
size = file.size
|
19
|
+
contents = file.read
|
20
|
+
|
21
|
+
assert_operator size, :>, 1000
|
22
|
+
assert_operator size, :<, 1500
|
23
|
+
|
24
|
+
# width == 137
|
25
|
+
assert_includes contents, "\0\0\0\x89".force_encoding("binary")
|
26
|
+
refute_includes contents, "Copyright Voormedia".force_encoding("binary")
|
19
27
|
end
|
20
28
|
end
|
21
29
|
|
22
30
|
it "should compress from url" do
|
23
31
|
source = Tinify.from_url("https://raw.githubusercontent.com/tinify/tinify-ruby/master/test/examples/voormedia.png")
|
24
|
-
Tempfile.open("optimized.png") do |file|
|
32
|
+
Tempfile.open("optimized.png", encoding: "binary") do |file|
|
25
33
|
source.to_file(file.path)
|
26
|
-
|
27
|
-
|
34
|
+
|
35
|
+
size = file.size
|
36
|
+
contents = file.read
|
37
|
+
|
38
|
+
assert_operator size, :>, 1000
|
39
|
+
assert_operator size, :<, 1500
|
40
|
+
|
41
|
+
# width == 137
|
42
|
+
assert_includes contents, "\0\0\0\x89".force_encoding("binary")
|
43
|
+
refute_includes contents, "Copyright Voormedia".force_encoding("binary")
|
28
44
|
end
|
29
45
|
end
|
30
46
|
|
31
47
|
it "should resize" do
|
32
|
-
Tempfile.open("
|
48
|
+
Tempfile.open("optimized.png", encoding: "binary") do |file|
|
33
49
|
optimized.resize(method: "fit", width: 50, height: 20).to_file(file.path)
|
34
|
-
|
35
|
-
|
50
|
+
|
51
|
+
size = file.size
|
52
|
+
contents = file.read
|
53
|
+
|
54
|
+
assert_operator size, :>, 500
|
55
|
+
assert_operator size, :<, 1000
|
56
|
+
|
57
|
+
# width == 50
|
58
|
+
assert_includes contents, "\0\0\0\x32".force_encoding("binary")
|
59
|
+
refute_includes contents, "Copyright Voormedia".force_encoding("binary")
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should preserve metadata" do
|
64
|
+
Tempfile.open("optimized.png", encoding: "binary") do |file|
|
65
|
+
optimized.preserve(:copyright, :creation).to_file(file.path)
|
66
|
+
|
67
|
+
size = file.size
|
68
|
+
contents = file.read
|
69
|
+
|
70
|
+
assert_operator size, :>, 1000
|
71
|
+
assert_operator size, :<, 2000
|
72
|
+
|
73
|
+
# width == 137
|
74
|
+
assert_includes contents, "\0\0\0\x89".force_encoding("binary")
|
75
|
+
assert_includes contents, "Copyright Voormedia".force_encoding("binary")
|
36
76
|
end
|
37
77
|
end
|
38
78
|
end
|
data/test/tinify_source_test.rb
CHANGED
@@ -7,10 +7,10 @@ describe Tinify::Source do
|
|
7
7
|
before do
|
8
8
|
Tinify.key = "invalid"
|
9
9
|
|
10
|
-
stub_request(:post, "https://api:invalid@api.tinify.com/shrink")
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
stub_request(:post, "https://api:invalid@api.tinify.com/shrink")
|
11
|
+
.to_return(
|
12
|
+
status: 401,
|
13
|
+
body: '{"error":"Unauthorized","message":"Credentials are invalid"}')
|
14
14
|
end
|
15
15
|
|
16
16
|
describe "from_file" do
|
@@ -41,42 +41,22 @@ describe Tinify::Source do
|
|
41
41
|
describe "with valid api key" do
|
42
42
|
before do
|
43
43
|
Tinify.key = "valid"
|
44
|
-
|
45
|
-
stub_request(:post, "https://api:valid@api.tinify.com/shrink")
|
46
|
-
.to_return(
|
47
|
-
status: 201,
|
48
|
-
headers: { Location: "https://api.tinify.com/some/location" },
|
49
|
-
body: '{}'
|
50
|
-
)
|
51
|
-
|
52
|
-
stub_request(:get, "https://api:valid@api.tinify.com/some/location").to_return(
|
53
|
-
status: 200,
|
54
|
-
body: "compressed file"
|
55
|
-
)
|
56
|
-
|
57
|
-
stub_request(:get, "https://api:valid@api.tinify.com/some/location").with(
|
58
|
-
body: '{"resize":{"width":400}}'
|
59
|
-
).to_return(
|
60
|
-
status: 200,
|
61
|
-
body: "small file"
|
62
|
-
)
|
63
|
-
|
64
|
-
stub_request(:post, "https://api:valid@api.tinify.com/some/location").with(
|
65
|
-
body: '{"store":{"service":"s3"}}'
|
66
|
-
).to_return(
|
67
|
-
status: 200,
|
68
|
-
headers: { Location: "https://bucket.s3.amazonaws.com/example" }
|
69
|
-
)
|
70
|
-
|
71
|
-
stub_request(:post, "https://api:valid@api.tinify.com/some/location").with(
|
72
|
-
body: '{"resize":{"width":400},"store":{"service":"s3"}}'
|
73
|
-
).to_return(
|
74
|
-
status: 200,
|
75
|
-
headers: { Location: "https://bucket.s3.amazonaws.com/example" }
|
76
|
-
)
|
77
44
|
end
|
78
45
|
|
79
46
|
describe "from_file" do
|
47
|
+
before do
|
48
|
+
stub_request(:post, "https://api:valid@api.tinify.com/shrink")
|
49
|
+
.to_return(
|
50
|
+
status: 201,
|
51
|
+
headers: { Location: "https://api.tinify.com/some/location" },
|
52
|
+
body: '{}')
|
53
|
+
|
54
|
+
stub_request(:get, "https://api:valid@api.tinify.com/some/location")
|
55
|
+
.to_return(
|
56
|
+
status: 200,
|
57
|
+
body: "compressed file")
|
58
|
+
end
|
59
|
+
|
80
60
|
it "should return source" do
|
81
61
|
assert_kind_of Tinify::Source, Tinify::Source.from_file(dummy_file)
|
82
62
|
end
|
@@ -87,6 +67,19 @@ describe Tinify::Source do
|
|
87
67
|
end
|
88
68
|
|
89
69
|
describe "from_buffer" do
|
70
|
+
before do
|
71
|
+
stub_request(:post, "https://api:valid@api.tinify.com/shrink")
|
72
|
+
.to_return(
|
73
|
+
status: 201,
|
74
|
+
headers: { Location: "https://api.tinify.com/some/location" },
|
75
|
+
body: '{}')
|
76
|
+
|
77
|
+
stub_request(:get, "https://api:valid@api.tinify.com/some/location")
|
78
|
+
.to_return(
|
79
|
+
status: 200,
|
80
|
+
body: "compressed file")
|
81
|
+
end
|
82
|
+
|
90
83
|
it "should return source" do
|
91
84
|
assert_kind_of Tinify::Source, Tinify::Source.from_buffer("png file")
|
92
85
|
end
|
@@ -103,20 +96,18 @@ describe Tinify::Source do
|
|
103
96
|
.to_return(
|
104
97
|
status: 201,
|
105
98
|
headers: { Location: "https://api.tinify.com/some/location" },
|
106
|
-
body: '{}'
|
107
|
-
)
|
99
|
+
body: '{}')
|
108
100
|
|
109
101
|
stub_request(:post, "https://api:valid@api.tinify.com/shrink")
|
110
102
|
.with(body: '{"source":{"url":"file://wrong"}}')
|
111
103
|
.to_return(
|
112
104
|
status: 400,
|
113
|
-
body: '{"error":"Source not found","message":"Cannot parse URL"}'
|
114
|
-
)
|
105
|
+
body: '{"error":"Source not found","message":"Cannot parse URL"}')
|
115
106
|
|
116
|
-
stub_request(:get, "https://api:valid@api.tinify.com/some/location")
|
117
|
-
|
118
|
-
|
119
|
-
|
107
|
+
stub_request(:get, "https://api:valid@api.tinify.com/some/location")
|
108
|
+
.to_return(
|
109
|
+
status: 200,
|
110
|
+
body: "compressed file")
|
120
111
|
end
|
121
112
|
|
122
113
|
it "should return source" do
|
@@ -135,12 +126,84 @@ describe Tinify::Source do
|
|
135
126
|
end
|
136
127
|
|
137
128
|
describe "result" do
|
129
|
+
before do
|
130
|
+
stub_request(:post, "https://api:valid@api.tinify.com/shrink")
|
131
|
+
.to_return(
|
132
|
+
status: 201,
|
133
|
+
headers: { Location: "https://api.tinify.com/some/location" },
|
134
|
+
body: '{}')
|
135
|
+
|
136
|
+
stub_request(:get, "https://api:valid@api.tinify.com/some/location")
|
137
|
+
.to_return(
|
138
|
+
status: 200,
|
139
|
+
body: "compressed file")
|
140
|
+
end
|
141
|
+
|
138
142
|
it "should return result" do
|
139
143
|
assert_kind_of Tinify::Result, Tinify::Source.from_buffer("png file").result
|
140
144
|
end
|
141
145
|
end
|
142
146
|
|
147
|
+
describe "preserve" do
|
148
|
+
before do
|
149
|
+
stub_request(:post, "https://api:valid@api.tinify.com/shrink")
|
150
|
+
.to_return(
|
151
|
+
status: 201,
|
152
|
+
headers: { Location: "https://api.tinify.com/some/location" },
|
153
|
+
body: '{}')
|
154
|
+
|
155
|
+
stub_request(:get, "https://api:valid@api.tinify.com/some/location")
|
156
|
+
.with(
|
157
|
+
body: '{"preserve":["copyright","location"]}')
|
158
|
+
.to_return(
|
159
|
+
status: 200,
|
160
|
+
body: "copyrighted file")
|
161
|
+
end
|
162
|
+
|
163
|
+
it "should return source" do
|
164
|
+
source = Tinify::Source.from_buffer("png file").preserve(:copyright, :location)
|
165
|
+
assert_kind_of Tinify::Source, source
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should return source with data" do
|
169
|
+
source = Tinify::Source.from_buffer("png file").preserve(:copyright, :location)
|
170
|
+
assert_equal "copyrighted file", source.to_buffer
|
171
|
+
end
|
172
|
+
|
173
|
+
it "should return source with data for array" do
|
174
|
+
source = Tinify::Source.from_buffer("png file").preserve([:copyright, :location])
|
175
|
+
assert_equal "copyrighted file", source.to_buffer
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should include other options if set" do
|
179
|
+
stub_request(:get, "https://api:valid@api.tinify.com/some/location")
|
180
|
+
.with(
|
181
|
+
body: '{"resize":{"width":400},"preserve":["copyright","location"]}')
|
182
|
+
.to_return(
|
183
|
+
status: 200,
|
184
|
+
body: "copyrighted resized file")
|
185
|
+
|
186
|
+
source = Tinify::Source.from_buffer("png file").resize(width: 400).preserve(:copyright, :location)
|
187
|
+
assert_equal "copyrighted resized file", source.to_buffer
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
143
191
|
describe "resize" do
|
192
|
+
before do
|
193
|
+
stub_request(:post, "https://api:valid@api.tinify.com/shrink")
|
194
|
+
.to_return(
|
195
|
+
status: 201,
|
196
|
+
headers: { Location: "https://api.tinify.com/some/location" },
|
197
|
+
body: '{}')
|
198
|
+
|
199
|
+
stub_request(:get, "https://api:valid@api.tinify.com/some/location")
|
200
|
+
.with(
|
201
|
+
body: '{"resize":{"width":400}}')
|
202
|
+
.to_return(
|
203
|
+
status: 200,
|
204
|
+
body: "small file")
|
205
|
+
end
|
206
|
+
|
144
207
|
it "should return source" do
|
145
208
|
assert_kind_of Tinify::Source, Tinify::Source.from_buffer("png file").resize(width: 400)
|
146
209
|
end
|
@@ -151,6 +214,22 @@ describe Tinify::Source do
|
|
151
214
|
end
|
152
215
|
|
153
216
|
describe "store" do
|
217
|
+
before do
|
218
|
+
stub_request(:post, "https://api:valid@api.tinify.com/shrink")
|
219
|
+
.to_return(
|
220
|
+
status: 201,
|
221
|
+
headers: { Location: "https://api.tinify.com/some/location" },
|
222
|
+
body: '{}'
|
223
|
+
)
|
224
|
+
|
225
|
+
stub_request(:post, "https://api:valid@api.tinify.com/some/location")
|
226
|
+
.with(
|
227
|
+
body: '{"store":{"service":"s3"}}')
|
228
|
+
.to_return(
|
229
|
+
status: 200,
|
230
|
+
headers: { Location: "https://bucket.s3.amazonaws.com/example" })
|
231
|
+
end
|
232
|
+
|
154
233
|
it "should return result meta" do
|
155
234
|
assert_kind_of Tinify::ResultMeta, Tinify::Source.from_buffer("png file").store(service: "s3")
|
156
235
|
end
|
@@ -160,19 +239,52 @@ describe Tinify::Source do
|
|
160
239
|
assert_equal "https://bucket.s3.amazonaws.com/example", result.location
|
161
240
|
end
|
162
241
|
|
163
|
-
it "should include
|
242
|
+
it "should include other options if set" do
|
243
|
+
stub_request(:post, "https://api:valid@api.tinify.com/some/location")
|
244
|
+
.with(
|
245
|
+
body: '{"resize":{"width":400},"store":{"service":"s3"}}')
|
246
|
+
.to_return(
|
247
|
+
status: 200,
|
248
|
+
headers: { Location: "https://bucket.s3.amazonaws.com/example" })
|
249
|
+
|
164
250
|
result = Tinify::Source.from_buffer("png file").resize(width: 400).store(service: "s3")
|
165
251
|
assert_equal "https://bucket.s3.amazonaws.com/example", result.location
|
166
252
|
end
|
167
253
|
end
|
168
254
|
|
169
255
|
describe "to_buffer" do
|
256
|
+
before do
|
257
|
+
stub_request(:post, "https://api:valid@api.tinify.com/shrink")
|
258
|
+
.to_return(
|
259
|
+
status: 201,
|
260
|
+
headers: { Location: "https://api.tinify.com/some/location" },
|
261
|
+
body: '{}')
|
262
|
+
|
263
|
+
stub_request(:get, "https://api:valid@api.tinify.com/some/location").to_return(
|
264
|
+
status: 200,
|
265
|
+
body: "compressed file"
|
266
|
+
)
|
267
|
+
end
|
268
|
+
|
170
269
|
it "should return image data" do
|
171
270
|
assert_equal "compressed file", Tinify::Source.from_buffer("png file").to_buffer
|
172
271
|
end
|
173
272
|
end
|
174
273
|
|
175
274
|
describe "to_file" do
|
275
|
+
before do
|
276
|
+
stub_request(:post, "https://api:valid@api.tinify.com/shrink")
|
277
|
+
.to_return(
|
278
|
+
status: 201,
|
279
|
+
headers: { Location: "https://api.tinify.com/some/location" },
|
280
|
+
body: '{}')
|
281
|
+
|
282
|
+
stub_request(:get, "https://api:valid@api.tinify.com/some/location").to_return(
|
283
|
+
status: 200,
|
284
|
+
body: "compressed file"
|
285
|
+
)
|
286
|
+
end
|
287
|
+
|
176
288
|
it "should store image data" do
|
177
289
|
begin
|
178
290
|
tmp = Tempfile.open("foo")
|
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.3.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: 2016-01
|
11
|
+
date: 2016-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httpclient
|