Skip to content
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased

* Added `Tinify::Format` constants for the media types `convert` accepts, including JPEG XL (`image/jxl`).

## 1.8.0

* Update development dependencies.
Expand Down
1 change: 1 addition & 0 deletions lib/tinify.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "tinify/version"
require "tinify/error"
require "tinify/format"

require "tinify/client"
require "tinify/result_meta"
Expand Down
36 changes: 36 additions & 0 deletions lib/tinify/format.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Tinify
# Media types that images can be converted to.
#
# Use these constants with Source#convert:
#
# Tinify.from_file("input.png")
# .convert(type: Tinify::Format::JXL)
# .to_file("output.jxl")
#
# Multiple types may be supplied, in which case the API returns the smallest
# result:
#
# convert(type: [Tinify::Format::JXL, Tinify::Format::WEBP])
module Format
# WebP.
WEBP = "image/webp".freeze

# PNG.
PNG = "image/png".freeze

# JPEG.
JPEG = "image/jpeg".freeze

# JPEG, an alias of JPEG.
JPG = "image/jpg".freeze

# AVIF.
AVIF = "image/avif".freeze

# JPEG XL.
JXL = "image/jxl".freeze

# Wildcard, returns the smallest of the supported types.
ANY = "*/*".freeze
end
end
56 changes: 56 additions & 0 deletions test/tinify_format_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require File.expand_path("../helper", __FILE__)

describe Tinify::Format do
describe "constants" do
it "should expose the supported media types" do
assert_equal "image/webp", Tinify::Format::WEBP
assert_equal "image/png", Tinify::Format::PNG
assert_equal "image/jpeg", Tinify::Format::JPEG
assert_equal "image/jpg", Tinify::Format::JPG
assert_equal "image/avif", Tinify::Format::AVIF
assert_equal "image/jxl", Tinify::Format::JXL
assert_equal "*/*", Tinify::Format::ANY
end
end

describe "convert" do
before do
Tinify.key = "valid"

stub_request(:post, "https://api.tinify.com/shrink")
.with(basic_auth: ['api', 'valid'])
.to_return(
status: 201,
headers: { Location: "https://api.tinify.com/some/location" },
body: '{}')
end

it "should serialize a single format" do
stub_request(:post, "https://api.tinify.com/some/location")
.with(
basic_auth: ['api', 'valid'],
body: '{"convert":{"type":"image/jxl"}}')
.to_return(
status: 200,
body: "converted file")

source = Tinify::Source.from_buffer("png file")
.convert(type: Tinify::Format::JXL)
assert_equal "converted file", source.to_buffer
end

it "should serialize multiple formats" do
stub_request(:post, "https://api.tinify.com/some/location")
.with(
basic_auth: ['api', 'valid'],
body: '{"convert":{"type":["image/jxl","image/webp"]}}')
.to_return(
status: 200,
body: "converted file")

source = Tinify::Source.from_buffer("png file")
.convert(type: [Tinify::Format::JXL, Tinify::Format::WEBP])
assert_equal "converted file", source.to_buffer
end
end
end