Skip to content

Commit

Permalink
Format hints are now at the end of the BobRoss Query
Browse files Browse the repository at this point in the history
  • Loading branch information
malomalo committed Sep 19, 2023
1 parent 08de8ab commit af81390
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
39 changes: 21 additions & 18 deletions lib/bob_ross.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,57 +149,60 @@ def transformations
end

def encode_transformations(options = {})
string = []
pre_transform_options = []
transform_options = []
post_transform_options = []

options.each do |key, value|
next if value.nil?
case key
when :background
string << 'B' + value.downcase
transform_options << 'B' + value.downcase
when :crop
string << 'C' + value.downcase
transform_options << 'C' + value.downcase
when :expires
string << 'E' + value.to_i.to_s(16)
pre_transform_options << 'E' + value.to_i.to_s(16)
when :grayscale
string << 'G'
transform_options << 'G'
when :interlace
string << 'I'
post_transform_options << 'I'
when :lossless
string << 'L'
post_transform_options << 'L'
when :optimize
string << 'O'
post_transform_options << 'O'
when :padding
string << if value.is_a?(Array)
transform_options << if value.is_a?(Array)
'P' + value.join(',')
else
'P' + value
end
when :resize
string << 'S' + value.downcase
transform_options << 'S' + value.downcase
when :transparent
string << 'T'
post_transform_options << 'T'
when :watermark
if value.is_a?(Integer)
string << 'W' + value.to_s + 'se'
transform_options << 'W' + value.to_s + 'se'
elsif value.is_a?(Hash)
string << 'W' + (value[:id] || 0).to_s + (value[:position] || 'se') + value[:offset].to_s
transform_options << 'W' + (value[:id] || 0).to_s + (value[:position] || 'se') + value[:offset].to_s
elsif value.is_a?(String)
string << 'W' + value
transform_options << 'W' + value
elsif value
string << 'W0se'
transform_options << 'W0se'
end
# when :quality
# string << "Q#{value}"
# post_transform_options << "Q#{value}"
else
@plugins.values.find do |plugin|
if encode = plugin.encode_transformation(key, value)
string << encode
transform_options << encode
true
end
end
end
end

string.join('')
pre_transform_options.join('') + transform_options.join('') + post_transform_options.join('')
end

# Delegates all uncauge class method calls to the singleton
Expand Down
8 changes: 6 additions & 2 deletions test/bob_ross_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def setup
test "encode_transformations" do
time = 1449100194

assert_equal "E#{time.to_s(16)}OILTS500x500^GW0seP1,2,3,4weeddccaa", BobRoss.encode_transformations({
assert_equal "E#{time.to_s(16)}S500x500^GW0seP1,2,3,4weeddccaaOILT", BobRoss.encode_transformations({
expires: time,
optimize: true,
interlace: true,
Expand Down Expand Up @@ -44,7 +44,7 @@ def setup
}
})

assert_equal "https://example.com/H41482f0113cc9843f0aeaa10631936644a164059OIS500x500%5EBeeddccaaE#{time.to_s(16)}GLTW0se/hash/image.png", BobRoss.url('hash')
assert_equal "https://example.com/H41482f0113cc9843f0aeaa10631936644a164059E#{time.to_s(16)}S500x500%5EBeeddccaaGW0seOILT/hash/image.png", BobRoss.url('hash')
end

test "path" do
Expand Down Expand Up @@ -104,6 +104,10 @@ def setup
assert_equal '/Hfae60e9eb00a16c19ff01aef0cadfe6709f024e6Baabbcc/hash/my+Filename%26.png', BobRoss.path('hash', background: 'aabbcc', filename: 'my Filename&', format: :png, hmac: {key: 'secret', attributes: [:transformations, :hash]})
assert_equal '/He993a4e6169a3effcfc7f2ea39ec56cc81a41591Baabbcc/hash/my+Filename%26.png', BobRoss.path('hash', background: 'aabbcc', filename: 'my Filename&', format: :png, hmac: {key: 'secret', attributes: [:transformations, :hash, :format]})
end

test "format hints go at the end of the hash" do
assert_equal '/S500x500ILOT/hash/my+Filename%26', BobRoss.path('hash', interlace: true, lossless: true, optimize: true, transparent: true, resize: '500x500', filename: 'my Filename&')
end

test "url" do
BobRoss.configure(host: 'http://example.com')
Expand Down

0 comments on commit af81390

Please sign in to comment.