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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MzlkN2ZlMGI2OTc3YzFhZmY1M2JmMjVkNDY5ZjJmZWY4ZmZlMTJlZg==
4
+ OTg3Y2VkOGIxZWJjNjVjNzIzOGU0ZWM4NjQ1MDE0ZGEyNzUyOGM4ZQ==
5
5
  data.tar.gz: !binary |-
6
- NWNhZDgxMDE0NmU3MjhkNWZmNmVmYzJkNTkyYjE4MGFiM2RiODdlMA==
6
+ NzJiYjliMmNhYTQ1ZGJjN2FjMDdmNTNiYTcxNzIyZmYyZDU2MDM0Yw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NjdmNTM5YzIzY2Y0YWIwM2ZiMTIwZGFhOGIwNzA2MTMyYTEyYzc5NDc4NDk2
10
- OWE4ODBhNDVkNTA4ZGMyODZjOGU4ZjNmOWE1ZDczMTM2YjM3NDYzNzhjMmI4
11
- N2IyYTA2MDY2YzM3YzhjYTRjZjBlYmM5MDM0ZmVmZDY3NzkyZjA=
9
+ ODI3MmJmMDhlYTg2MGE1YzVlOWYzYjRkNjAwMDRiNjMzNjlkZWJhODgzM2Rk
10
+ NDE1Njk3ODc2OWZkODlhNjNiNGYyMjE2N2I4MTUxMzNmODRhYTk5ODVlOWRm
11
+ YTc0MTVkM2NiZDQ1NjNhMDRmMTFlM2FlNGU5YjliZWQwODRiMTg=
12
12
  data.tar.gz: !binary |-
13
- ODhkYTA3NDg2YTIwZWJkYjYyMzQ1ZTM1YjIxN2E0NzdkYzMxMGFhY2MyMmU1
14
- NWNhZWU3ZDUzZmQxMTZjNjgzZDUzMmYzOGM1MGYyMWRlMzA5MjhjODhiMjYz
15
- YzI0NTJhYWM3ZDdjZDA4MGI2ZGY5ZTZkNzMzNjg2ZmYxZGRhN2E=
13
+ MzIxZTJlZWQwMDZhMzU4ZDkyODA3N2I5M2JiNGEwZTM4ODI0NjcxYTcxMWZj
14
+ ZTBjYjc5ZGJlYjg5MmU5MGZlZDVlYmI4YmViYjMxY2QzYWQyNWFlMWZmY2I3
15
+ YjY0N2Q3ZmU0YTQzNjk1ZTQ3NWI1YTEyOTMwNTgyYzM1MDdkNmI=
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tinify (1.2.0)
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.0.1)
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
@@ -1,6 +1,6 @@
1
1
  The MIT License
2
2
 
3
- Copyright (c) 2013-2015 Voormedia
3
+ Copyright (c) 2013-2016 Voormedia
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -10,8 +10,8 @@ require "thread"
10
10
 
11
11
  module Tinify
12
12
  class << self
13
- attr_accessor :key
14
- attr_accessor :app_identifier
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 @key
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(@key, @app_identifier).freeze
51
+ @client ||= Client.new(key, app_identifier).freeze
52
52
  end
53
53
  end
54
54
  end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Tinify
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
Binary file
@@ -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
- assert_operator file.size, :>, 0
18
- assert_operator file.size, :<, 1500
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
- assert_operator file.size, :>, 0
27
- assert_operator file.size, :<, 1500
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("resized.png") do |file|
48
+ Tempfile.open("optimized.png", encoding: "binary") do |file|
33
49
  optimized.resize(method: "fit", width: 50, height: 20).to_file(file.path)
34
- assert_operator file.size, :>, 0
35
- assert_operator file.size, :<, 800
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
@@ -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").to_return(
11
- status: 401,
12
- body: '{"error":"Unauthorized","message":"Credentials are invalid"}'
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").to_return(
117
- status: 200,
118
- body: "compressed file"
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 resize options if set" do
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.2.0
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-29 00:00:00.000000000 Z
11
+ date: 2016-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httpclient