From 2ec42981b773706dc2b9d387de34ee503c16b42a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jovan=20Mari=C4=87?= Date: Tue, 23 Sep 2025 11:25:03 +0200 Subject: [PATCH 1/2] deps(gems): update --- Gemfile.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 187a9ea..6c534e1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -9,19 +9,19 @@ GEM specs: addressable (2.8.7) public_suffix (>= 2.0.2, < 7.0) - base64 (0.2.0) - bigdecimal (3.1.9) - bigdecimal (3.1.9-java) + base64 (0.3.0) + bigdecimal (3.2.3) + bigdecimal (3.2.3-java) crack (1.0.0) bigdecimal rexml - hashdiff (1.1.2) + hashdiff (1.2.1) httpclient (2.9.0) mutex_m minitest (5.25.5) mutex_m (0.3.0) - public_suffix (6.0.1) - rake (13.2.1) + public_suffix (6.0.2) + rake (13.3.0) rexml (3.4.4) webmock (3.25.1) addressable (>= 2.8.0) From 9343f79ef67f5a4454172440d8d21025c0ac5252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jovan=20Mari=C4=87?= Date: Tue, 23 Sep 2025 11:27:41 +0200 Subject: [PATCH 2/2] fix(source): `:result` uses post when body is given --- lib/tinify/source.rb | 7 +++++- test/tinify_source_test.rb | 49 ++++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/lib/tinify/source.rb b/lib/tinify/source.rb index 19d2a33..f2e192a 100644 --- a/lib/tinify/source.rb +++ b/lib/tinify/source.rb @@ -43,7 +43,12 @@ def store(options) end def result - response = Tinify.client.request(:get, @url, @commands) + response = if @commands&.empty? + Tinify.client.request(:get, @url) + else + Tinify.client.request(:post, @url, @commands) + end + Result.new(response.headers, response.body).freeze end diff --git a/test/tinify_source_test.rb b/test/tinify_source_test.rb index c65f312..8a0f5e7 100644 --- a/test/tinify_source_test.rb +++ b/test/tinify_source_test.rb @@ -153,8 +153,35 @@ body: "compressed file") end - it "should return result" do - assert_kind_of Tinify::Result, Tinify::Source.from_buffer("png file").result + it 'has a `Tinify::Result`' do + assert_kind_of(Tinify::Result, + Tinify::Source.from_buffer("png file").result) + end + + it 'has result data' do + assert_equal('compressed file', + Tinify::Source.from_buffer("png file").result.data) + end + + it 'is a :get request' do + Tinify::Source.from_buffer("png file").result + + assert_requested(:get, "https://api.tinify.com/some/location", + times: 1) + end + + it 'is a :post request' do + stub_request(:post, "https://api.tinify.com/some/location") + .with(basic_auth: ['api', 'valid'], + body: '{"preserve":["copyright","location"]}') + .to_return(status: 200, body: "copyrighted file") + + Tinify::Source.from_buffer("png file") + .preserve("copyright", "location") + .result + + assert_requested(:post, "https://api.tinify.com/some/location", + times: 1) end end @@ -167,7 +194,7 @@ headers: { Location: "https://api.tinify.com/some/location" }, body: '{}') - stub_request(:get, "https://api.tinify.com/some/location") + stub_request(:post, "https://api.tinify.com/some/location") .with( basic_auth: ['api', 'valid'], body: '{"preserve":["copyright","location"]}') @@ -192,7 +219,7 @@ end it "should include other options if set" do - stub_request(:get, "https://api.tinify.com/some/location") + stub_request(:post, "https://api.tinify.com/some/location") .with( basic_auth: ['api', 'valid'], body: '{"resize":{"width":400},"preserve":["copyright","location"]}') @@ -214,7 +241,7 @@ headers: { Location: "https://api.tinify.com/some/location" }, body: '{}') - stub_request(:get, "https://api.tinify.com/some/location") + stub_request(:post, "https://api.tinify.com/some/location") .with( basic_auth: ['api', 'valid'], body: '{"resize":{"width":400}}') @@ -241,7 +268,7 @@ headers: { Location: "https://api.tinify.com/some/location" }, body: '{}') - stub_request(:get, "https://api.tinify.com/some/location") + stub_request(:post, "https://api.tinify.com/some/location") .with( basic_auth: ['api', 'valid'], body: '{"convert":{"type":["image/webp"]}}') @@ -268,7 +295,7 @@ headers: { Location: "https://api.tinify.com/some/location" }, body: '{}') - stub_request(:get, "https://api.tinify.com/some/location") + stub_request(:post, "https://api.tinify.com/some/location") .with( basic_auth: ['api', 'valid'], body: '{"transform":{"color":"black"}}') @@ -286,11 +313,9 @@ end it "should include other options if set" do - - stub_request(:get, "https://api.tinify.com/some/location"). - with(:body => '{"convert":{"type":["image/webp"]},"transform":{"color":"black"}}', - ). - to_return(:status => 200, :body => "trans-formed-and-coded", :headers => {}) + stub_request(:post, "https://api.tinify.com/some/location") + .with(:body => '{"convert":{"type":["image/webp"]},"transform":{"color":"black"}}') + .to_return(:status => 200, :body => "trans-formed-and-coded", :headers => {}) result = Tinify::Source.from_buffer("png file").convert(type: ["image/webp"]).transform(color: "black") assert_equal "trans-formed-and-coded", result.to_buffer