Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion lib/tinify/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
49 changes: 37 additions & 12 deletions test/tinify_source_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"]}')
Expand All @@ -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"]}')
Expand All @@ -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}}')
Expand All @@ -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"]}}')
Expand All @@ -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"}}')
Expand All @@ -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
Expand Down