tinify 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZjI0YTA0ZjFkZDdlYTlkNTVkMTY1MjQ4ZjAyZjMyNjk5NmVhNDhmNQ==
4
+ MzlkN2ZlMGI2OTc3YzFhZmY1M2JmMjVkNDY5ZjJmZWY4ZmZlMTJlZg==
5
5
  data.tar.gz: !binary |-
6
- NTdhNzRjYjQ3ZjhhNDc2ZjlmZTI0YjFmY2U2ZWJmNjlmZGZhNTliZg==
6
+ NWNhZDgxMDE0NmU3MjhkNWZmNmVmYzJkNTkyYjE4MGFiM2RiODdlMA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MWJhOTQxYzUxMjBiOTI3MzQ4ZTJkNjBjZjI2MmFhMTkzYmUwMTE3NDY0M2Jl
10
- MGU3MWQ0M2MzYmRlMzkxODJjNGE1NmRiMzZhZDE3OTUxOTZkYzVkZTZiZDcz
11
- NWRkMjVmMzczYTlhYjFhZTQ0MTNkNTIxZmEyZDBjMjM0ZjU0YmE=
9
+ NjdmNTM5YzIzY2Y0YWIwM2ZiMTIwZGFhOGIwNzA2MTMyYTEyYzc5NDc4NDk2
10
+ OWE4ODBhNDVkNTA4ZGMyODZjOGU4ZjNmOWE1ZDczMTM2YjM3NDYzNzhjMmI4
11
+ N2IyYTA2MDY2YzM3YzhjYTRjZjBlYmM5MDM0ZmVmZDY3NzkyZjA=
12
12
  data.tar.gz: !binary |-
13
- YTJlMDhlOWJiODc0N2Y4ZmY4MWIyOWEwM2M1ZmRiNDhjOGYwYjNkN2VkZjY4
14
- ZmY2NDkzNjM1ZjIyZjYyMmIyMGI2MDkzMDg4ZWIxZWJjNDBhY2ExNzI5OWMx
15
- NDEzNmU2YTZjZjQwZWU0MTdiODZkMjc5ODY5NGU3NDc2OGIyNmQ=
13
+ ODhkYTA3NDg2YTIwZWJkYjYyMzQ1ZTM1YjIxN2E0NzdkYzMxMGFhY2MyMmU1
14
+ NWNhZWU3ZDUzZmQxMTZjNjgzZDUzMmYzOGM1MGYyMWRlMzA5MjhjODhiMjYz
15
+ YzI0NTJhYWM3ZDdjZDA4MGI2ZGY5ZTZkNzMzNjg2ZmYxZGRhN2E=
@@ -5,13 +5,21 @@ rvm:
5
5
  - 2.0
6
6
  - 2.1
7
7
  - 2.2
8
+ - 2.3.0
8
9
  - ruby-head
9
10
  - jruby-19mode
10
11
  - jruby-20mode
11
12
  - rbx-2
13
+ env:
14
+ global:
15
+ - secure: u2JnTDMbqgf0kP5vDywFP+lL0ON135Q4a4jXTS1J4K8Kp7qFe/yzyrtYSmDJux31U3Ob/U/ptwYLlFUYuwjwMgTb4ow35jms8d9RvLb6vIBH7Yw9Sf6eq3iJxHGNOjM2pTL9N5DHx4TTO2ov+vQ1PT/QY5P70LVrSKurgKFDjgIZKwnYbFSZtusHX0qTPPQ3FL+NDdcuDmxLxiGt294o+9pFrUAQUKVV4NAk+HUnLjpqKWFqD9pMEw/DYrX4g9RAmN5YWt0sE+CCMFEaKh6Ghrfcfxov9yDEcKDz2ELXNiot9526wwoNONA/zT9hNQXfxhIXfV/67JsJ9cmbZIgP9FLWd/ziRs/yjoTJRaSloifXtAXAft2PMiNx7dnB2C9FFnDzJuTTkJdQfdSEkcXkn0a1Hy5wUI6Ur9Vdjn/Jc5yAHF6Uc78ekgkXBIVxc/J9J9EjophlaCsZHQbgM2H+mADbRIAA/qV1WD9XTISxANP6N19Pt6xFW+wGDnfBBDqbzCO6vK9M+9fU2LW4N8177rtCEG7XkUVSllbJvq841MPqOi2k++B9LD2xAu6Db3ubV5KFL/wg1QBoyP5QOd1uAZ+goQFdvxOZ/7mN/45ytKYYammQlngZFUi638dAE6f4j+NUtrczk4QQJmDuFxXoP+KBtgiD1nrtmenaxOEp/gk=
12
16
  matrix:
13
17
  allow_failures:
14
18
  - rvm: ruby-head
19
+ include:
20
+ - rvm: 2.2
21
+ env: INTEGRATION_TESTS=true
22
+ script: "if [ \"$TRAVIS_PULL_REQUEST\" == \"false\" ]; then rake integration; fi"
15
23
  notifications:
16
24
  email: false
17
25
  slack:
@@ -25,3 +33,4 @@ deploy:
25
33
  tags: true
26
34
  repo: tinify/tinify-ruby
27
35
  ruby: 2.2
36
+ condition: "$INTEGRATION_TESTS != true"
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tinify (1.1.0)
4
+ tinify (1.2.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.6.0.1)
13
+ httpclient (2.7.0.1)
14
14
  minitest (5.5.1)
15
15
  rake (10.4.2)
16
16
  safe_yaml (1.0.4)
data/README.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  # Tinify API client for Ruby
4
4
 
5
+ Ruby client for the Tinify API, used for [TinyPNG](https://tinypng.com) and [TinyJPG](https://tinyjpg.com). Tinify compresses your images intelligently. Read more at [http://tinify.com](http://tinify.com).
6
+
7
+ ## Documentation
8
+
9
+ [Go to the documentation for the Ruby client](https://tinypng.com/developers/reference/ruby).
10
+
5
11
  ## Installation
6
12
 
7
13
  Install the API client:
@@ -32,6 +38,13 @@ bundle install
32
38
  rake
33
39
  ```
34
40
 
41
+ ### Integration tests
42
+
43
+ ```
44
+ bundle install
45
+ TINIFY_KEY=$YOUR_API_KEY rake integration
46
+ ```
47
+
35
48
  ## License
36
49
 
37
50
  This software is licensed under the MIT License. [View the license](LICENSE).
data/Rakefile CHANGED
@@ -5,4 +5,8 @@ Rake::TestTask.new do |t|
5
5
  t.test_files = FileList["test/*_test.rb"]
6
6
  end
7
7
 
8
+ Rake::TestTask.new(:integration) do |t|
9
+ t.test_files = FileList["test/integration.rb"]
10
+ end
11
+
8
12
  task default: :test
@@ -32,6 +32,10 @@ module Tinify
32
32
  Source.from_buffer(string)
33
33
  end
34
34
 
35
+ def from_url(string)
36
+ Source.from_url(string)
37
+ end
38
+
35
39
  def validate!
36
40
  client.request(:post, "/shrink")
37
41
  rescue ClientError
@@ -20,9 +20,13 @@ module Tinify
20
20
  end
21
21
 
22
22
  def request(method, url, body = nil, header = {})
23
- if Hash === body && !body.empty?
24
- body = JSON.generate(body)
25
- header["Content-Type"] = "application/json"
23
+ if Hash === body
24
+ if body.empty?
25
+ body = nil
26
+ else
27
+ body = JSON.generate(body)
28
+ header["Content-Type"] = "application/json"
29
+ end
26
30
  end
27
31
 
28
32
  begin
@@ -13,7 +13,7 @@ module Tinify
13
13
  alias_method :to_buffer, :data
14
14
 
15
15
  def size
16
- @meta["Content-Length"].to_i
16
+ @meta["Content-Length"].to_i.nonzero?
17
17
  end
18
18
 
19
19
  def media_type
@@ -5,11 +5,11 @@ module Tinify
5
5
  end
6
6
 
7
7
  def width
8
- @meta["Image-Width"].to_i
8
+ @meta["Image-Width"].to_i.nonzero?
9
9
  end
10
10
 
11
11
  def height
12
- @meta["Image-Height"].to_i
12
+ @meta["Image-Height"].to_i.nonzero?
13
13
  end
14
14
 
15
15
  def location
@@ -9,6 +9,11 @@ module Tinify
9
9
  response = Tinify.client.request(:post, "/shrink", string)
10
10
  new(response.headers["Location"]).freeze
11
11
  end
12
+
13
+ def from_url(url)
14
+ response = Tinify.client.request(:post, "/shrink", source: { url: url })
15
+ new(response.headers["Location"]).freeze
16
+ end
12
17
  end
13
18
 
14
19
  def initialize(url, commands = {})
@@ -1,3 +1,3 @@
1
1
  module Tinify
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -10,18 +10,18 @@ module TestHelpers
10
10
  end
11
11
 
12
12
  def assert_raise_with_message(message)
13
- err = nil
14
- begin
15
- yield
16
- rescue => err
17
- end
18
- if message.is_a?(Regexp)
19
- assert_match(message, err.message)
20
- else
21
- assert_equal(message, err.message)
22
- end
23
- end
24
- end
13
+ err = nil
14
+ begin
15
+ yield
16
+ rescue => err
17
+ end
18
+ if message.is_a?(Regexp)
19
+ assert_match(message, err.message)
20
+ else
21
+ assert_equal(message, err.message)
22
+ end
23
+ end
24
+ end
25
25
 
26
26
  class MiniTest::Spec
27
27
  include TestHelpers
@@ -0,0 +1,38 @@
1
+ abort "Set the TINIFY_KEY environment variable." unless ENV["TINIFY_KEY"]
2
+
3
+ require "bundler/setup"
4
+ require "tinify"
5
+
6
+ require "minitest/autorun"
7
+
8
+ describe "client integration" do
9
+ Tinify.key = ENV["TINIFY_KEY"]
10
+
11
+ unoptimized_path = File.expand_path("../examples/voormedia.png", __FILE__)
12
+ optimized = Tinify.from_file(unoptimized_path)
13
+
14
+ it "should compress from file" do
15
+ Tempfile.open("optimized.png") do |file|
16
+ optimized.to_file(file.path)
17
+ assert_operator file.size, :>, 0
18
+ assert_operator file.size, :<, 1500
19
+ end
20
+ end
21
+
22
+ it "should compress from url" do
23
+ source = Tinify.from_url("https://raw.githubusercontent.com/tinify/tinify-ruby/master/test/examples/voormedia.png")
24
+ Tempfile.open("optimized.png") do |file|
25
+ source.to_file(file.path)
26
+ assert_operator file.size, :>, 0
27
+ assert_operator file.size, :<, 1500
28
+ end
29
+ end
30
+
31
+ it "should resize" do
32
+ Tempfile.open("resized.png") do |file|
33
+ optimized.resize(method: "fit", width: 50, height: 20).to_file(file.path)
34
+ assert_operator file.size, :>, 0
35
+ assert_operator file.size, :<, 800
36
+ end
37
+ end
38
+ end
@@ -9,9 +9,16 @@ describe Tinify::Client do
9
9
  describe "when valid" do
10
10
  before do
11
11
  stub_request(:get, "https://api:key@api.tinify.com").to_return(
12
- status: 200,
12
+ status: 201,
13
13
  headers: { "Compression-Count" => "12" }
14
14
  )
15
+ stub_request(:get, "https://api:key@api.tinify.com/shrink").to_return(
16
+ status: 201,
17
+ headers: {
18
+ "Compression-Count" => "12",
19
+ "Location" => "https://api.tinify.com/output/3spbi1cd7rs812lb.png"
20
+ }
21
+ )
15
22
  end
16
23
 
17
24
  it "should issue request" do
@@ -20,11 +27,32 @@ describe Tinify::Client do
20
27
  headers: { "Authorization" => "Basic " + ["api:key"].pack("m").chomp }
21
28
  end
22
29
 
30
+ it "should issue request to endpoint" do
31
+ subject.request(:get, "/shrink", {})
32
+ assert_requested :get, "https://api:key@api.tinify.com/shrink"
33
+ end
34
+
35
+ it "should issue request with method" do
36
+ subject.request(:get, "/shrink", {})
37
+ assert_requested :get, "https://api:key@api.tinify.com/shrink"
38
+ end
39
+
40
+ it "should return response" do
41
+ response = subject.request(:get, "/shrink", {})
42
+ assert_equal "https://api.tinify.com/output/3spbi1cd7rs812lb.png", response.headers["Location"]
43
+ end
44
+
23
45
  it "should issue request without body when options are empty" do
24
46
  subject.request(:get, "/", {})
25
47
  assert_requested :get, "https://api:key@api.tinify.com", body: nil
26
48
  end
27
49
 
50
+ it "should issue request without content type when options are empty" do
51
+ subject.request(:get, "/", {})
52
+ assert_not_requested :get, "https://api:key@api.tinify.com",
53
+ headers: { "Content-Type" => "application/x-www-form-urlencoded" }
54
+ end
55
+
28
56
  it "should issue request with json body" do
29
57
  subject.request(:get, "/", { hello: "world" })
30
58
  assert_requested :get, "https://api:key@api.tinify.com",
@@ -28,4 +28,28 @@ describe Tinify::ResultMeta do
28
28
  end
29
29
  end
30
30
  end
31
+
32
+ describe "without metadata" do
33
+ subject do
34
+ Tinify::ResultMeta.new({})
35
+ end
36
+
37
+ describe "width" do
38
+ it "should return nil" do
39
+ assert_nil subject.width
40
+ end
41
+ end
42
+
43
+ describe "height" do
44
+ it "should return nil" do
45
+ assert_nil subject.height
46
+ end
47
+ end
48
+
49
+ describe "location" do
50
+ it "should return nil" do
51
+ assert_nil subject.location
52
+ end
53
+ end
54
+ end
31
55
  end
@@ -8,6 +8,7 @@ describe Tinify::Result do
8
8
  "Image-Height" => "60",
9
9
  "Content-Length" => "450",
10
10
  "Content-Type" => "image/png",
11
+ "Location" => "https://api.tinify.com/output/13205af0a0ea.png"
11
12
  }, "image data")
12
13
  end
13
14
 
@@ -25,7 +26,7 @@ describe Tinify::Result do
25
26
 
26
27
  describe "location" do
27
28
  it "should return nil" do
28
- assert_nil subject.location
29
+ assert_equal "https://api.tinify.com/output/13205af0a0ea.png", subject.location
29
30
  end
30
31
  end
31
32
 
@@ -47,4 +48,46 @@ describe Tinify::Result do
47
48
  end
48
49
  end
49
50
  end
51
+
52
+ describe "without meta and data" do
53
+ subject do
54
+ Tinify::Result.new({}, nil)
55
+ end
56
+
57
+ describe "width" do
58
+ it "should return nil" do
59
+ assert_nil subject.width
60
+ end
61
+ end
62
+
63
+ describe "height" do
64
+ it "should return nil" do
65
+ assert_nil subject.height
66
+ end
67
+ end
68
+
69
+ describe "location" do
70
+ it "should return nil" do
71
+ assert_nil subject.location
72
+ end
73
+ end
74
+
75
+ describe "size" do
76
+ it "should return nil" do
77
+ assert_nil subject.size
78
+ end
79
+ end
80
+
81
+ describe "content_type" do
82
+ it "should return nil" do
83
+ assert_nil subject.content_type
84
+ end
85
+ end
86
+
87
+ describe "to_buffer" do
88
+ it "should return nil" do
89
+ assert_nil subject.to_buffer
90
+ end
91
+ end
92
+ end
50
93
  end
@@ -28,17 +28,26 @@ describe Tinify::Source do
28
28
  end
29
29
  end
30
30
  end
31
+
32
+ describe "from_url" do
33
+ it "should raise account error" do
34
+ assert_raises Tinify::AccountError do
35
+ Tinify::Source.from_url("http://example.com/test.jpg")
36
+ end
37
+ end
38
+ end
31
39
  end
32
40
 
33
41
  describe "with valid api key" do
34
42
  before do
35
43
  Tinify.key = "valid"
36
44
 
37
- stub_request(:post, "https://api:valid@api.tinify.com/shrink").to_return(
38
- status: 201,
39
- headers: { Location: "https://api.tinify.com/some/location" },
40
- body: '{}'
41
- )
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
+ )
42
51
 
43
52
  stub_request(:get, "https://api:valid@api.tinify.com/some/location").to_return(
44
53
  status: 200,
@@ -58,6 +67,13 @@ describe Tinify::Source do
58
67
  status: 200,
59
68
  headers: { Location: "https://bucket.s3.amazonaws.com/example" }
60
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
+ )
61
77
  end
62
78
 
63
79
  describe "from_file" do
@@ -80,6 +96,44 @@ describe Tinify::Source do
80
96
  end
81
97
  end
82
98
 
99
+ describe "from_url" do
100
+ before do
101
+ stub_request(:post, "https://api:valid@api.tinify.com/shrink")
102
+ .with(body: '{"source":{"url":"http://example.com/test.jpg"}}')
103
+ .to_return(
104
+ status: 201,
105
+ headers: { Location: "https://api.tinify.com/some/location" },
106
+ body: '{}'
107
+ )
108
+
109
+ stub_request(:post, "https://api:valid@api.tinify.com/shrink")
110
+ .with(body: '{"source":{"url":"file://wrong"}}')
111
+ .to_return(
112
+ status: 400,
113
+ body: '{"error":"Source not found","message":"Cannot parse URL"}'
114
+ )
115
+
116
+ stub_request(:get, "https://api:valid@api.tinify.com/some/location").to_return(
117
+ status: 200,
118
+ body: "compressed file"
119
+ )
120
+ end
121
+
122
+ it "should return source" do
123
+ assert_kind_of Tinify::Source, Tinify::Source.from_url("http://example.com/test.jpg")
124
+ end
125
+
126
+ it "should return source with data" do
127
+ assert_equal "compressed file", Tinify::Source.from_url("http://example.com/test.jpg").to_buffer
128
+ end
129
+
130
+ it "should raise error if request is not ok" do
131
+ assert_raises Tinify::ClientError do
132
+ Tinify::Source.from_url("file://wrong")
133
+ end
134
+ end
135
+ end
136
+
83
137
  describe "result" do
84
138
  it "should return result" do
85
139
  assert_kind_of Tinify::Result, Tinify::Source.from_buffer("png file").result
@@ -105,6 +159,11 @@ describe Tinify::Source do
105
159
  result = Tinify::Source.from_buffer("png file").store(service: "s3")
106
160
  assert_equal "https://bucket.s3.amazonaws.com/example", result.location
107
161
  end
162
+
163
+ it "should include resize options if set" do
164
+ result = Tinify::Source.from_buffer("png file").resize(width: 400).store(service: "s3")
165
+ assert_equal "https://bucket.s3.amazonaws.com/example", result.location
166
+ end
108
167
  end
109
168
 
110
169
  describe "to_buffer" do
@@ -116,4 +116,20 @@ describe Tinify do
116
116
  assert_kind_of Tinify::Source, Tinify.from_file(dummy_file)
117
117
  end
118
118
  end
119
+
120
+ describe "from_url" do
121
+ before do
122
+ Tinify.key = "valid"
123
+
124
+ stub_request(:post, "https://api:valid@api.tinify.com/shrink").to_return(
125
+ status: 201,
126
+ headers: { Location: "https://api.tinify.com/some/location" },
127
+ body: '{}'
128
+ )
129
+ end
130
+
131
+ it "should return source" do
132
+ assert_kind_of Tinify::Source, Tinify.from_url("http://example.com/test.jpg")
133
+ end
134
+ end
119
135
  end
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.1.0
4
+ version: 1.2.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: 2015-08-28 00:00:00.000000000 Z
11
+ date: 2016-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httpclient
@@ -103,7 +103,9 @@ files:
103
103
  - lib/tinify/source.rb
104
104
  - lib/tinify/version.rb
105
105
  - test/examples/dummy.png
106
+ - test/examples/voormedia.png
106
107
  - test/helper.rb
108
+ - test/integration.rb
107
109
  - test/tinify_client_test.rb
108
110
  - test/tinify_result_meta_test.rb
109
111
  - test/tinify_result_test.rb
@@ -136,7 +138,9 @@ specification_version: 4
136
138
  summary: Ruby client for the Tinify API.
137
139
  test_files:
138
140
  - test/examples/dummy.png
141
+ - test/examples/voormedia.png
139
142
  - test/helper.rb
143
+ - test/integration.rb
140
144
  - test/tinify_client_test.rb
141
145
  - test/tinify_result_meta_test.rb
142
146
  - test/tinify_result_test.rb