Skip to content

Commit f7a8f2d

Browse files
committed
feat(tests): add support for plain hashes in various helper methods and tests
1 parent 4d258e9 commit f7a8f2d

File tree

5 files changed

+131
-5
lines changed

5 files changed

+131
-5
lines changed

lib/imagekit/helpers/helper.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,12 @@ def path_join(parts, separator = "/")
448448
def process_overlay(overlay)
449449
return "" unless overlay
450450

451-
# All overlay inputs are expected to be BaseModel objects with type accessor
452-
type = overlay.type
451+
# Support both BaseModel objects and plain hashes
452+
type = if overlay.is_a?(Hash)
453+
overlay[:type] || overlay["type"]
454+
else
455+
overlay.type
456+
end
453457
return "" unless type
454458

455459
# Determine overlay type based on explicit type field
@@ -595,12 +599,18 @@ def process_solid_color_overlay(overlay)
595599
parts.join(",")
596600
end
597601

598-
# Safe property access for model objects
602+
# Safe property access for model objects and hashes
599603
def safe_get(obj, key)
600604
return nil unless obj
601605

602-
# All inputs are expected to be BaseModel objects with property accessors
603-
obj.respond_to?(key.to_sym) ? obj.send(key.to_sym) : nil
606+
# Support both BaseModel objects and plain hashes
607+
if obj.is_a?(Hash)
608+
obj[key.to_sym] || obj[key.to_s]
609+
elsif obj.respond_to?(key.to_sym)
610+
obj.send(key.to_sym)
611+
else
612+
nil
613+
end
604614
end
605615

606616
# Add overlay properties like position, timing, transformations (matching Node.js)

test/imagekit/custom-tests/responsive_image_attributes_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,36 @@ def test_using_query_parameters_and_transformation_position
184184
assert_equal(expected, result.to_h)
185185
end
186186

187+
# Hash-based API test - verify that plain hashes work for responsive image attributes
188+
def test_should_work_with_plain_hashes_for_responsive_image_attributes
189+
# Using plain hashes instead of model objects
190+
result = @client.helper.get_responsive_image_attributes(
191+
{
192+
src: "sample.jpg",
193+
url_endpoint: "https://ik.imagekit.io/demo",
194+
width: 450,
195+
transformation: [
196+
{
197+
height: 300
198+
},
199+
{
200+
ai_remove_background: true
201+
}
202+
]
203+
}
204+
)
205+
206+
# Multiple caller transformations should be combined appropriately
207+
expected = {
208+
src: "https://ik.imagekit.io/demo/sample.jpg?tr=h-300:e-bgremove:w-1080,c-at_max",
209+
src_set: "https://ik.imagekit.io/demo/sample.jpg?tr=h-300:e-bgremove:w-640,c-at_max 1x, https://ik.imagekit.io/demo/sample.jpg?tr=h-300:e-bgremove:w-1080,c-at_max 2x",
210+
sizes: nil,
211+
width: 450
212+
}
213+
214+
assert_equal(expected, result.to_h)
215+
end
216+
187217
def test_fallback_when_no_usable_vw_tokens
188218
result = @client.helper.get_responsive_image_attributes(
189219
src: "sample.jpg",

test/imagekit/custom-tests/url-generation/advanced_url_generation_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,4 +450,30 @@ def test_should_generate_the_correct_url_with_many_transformations_including_vid
450450
expected = "https://ik.imagekit.io/test_url_endpoint/test_path.jpg?tr=h-300,w-400,ar-4-3,q-40,c-force,cm-extract,fo-left,f-jpeg,r-50,bg-A94D34,b-5-A94D34,rt-90,bl-10,n-some_name,pr-true,lo-true,t-5,md-true,cp-true,di-folder@@file.jpg,dpr-3,x-10,y-20,xc-30,yc-40,fl-h,o-0.8,z-2,vc-h264,ac-aac,so-5,eo-15,du-10,sr-1440_1080,e-grayscale,e-upscale,e-retouch,e-genvar,e-dropshadow,e-changebg-prompt-car,e-edit-prompt-make it vintage,e-bgremove,e-contrast,e-shadow-bl-15_st-40_x-10_y-N5,e-sharpen-10,e-usm-2-2-0.8-0.024,e-gradient-from-red_to-white,orig-true,pg-2_4,h-200,w-300,l-image,i-logo.png,l-end"
451451
assert_equal(expected, url)
452452
end
453+
454+
# Hash-based API test - verify that plain hashes work for both transformations and options
455+
# Developers can use hashes instead of model objects for a more concise, Ruby-idiomatic syntax
456+
def test_should_work_with_plain_hashes_for_transformations_and_options
457+
# Using plain hashes instead of Transformation.new and SrcOptions.new
458+
url = @client.helper.build_url(
459+
{
460+
src: "/test_path1.jpg",
461+
url_endpoint: "https://ik.imagekit.io/test_url_endpoint",
462+
transformation_position: :query,
463+
transformation: [
464+
{
465+
width: 300,
466+
height: 200,
467+
quality: 85,
468+
border: "5_FF0000",
469+
ai_remove_background: true,
470+
ai_drop_shadow: true
471+
}
472+
]
473+
}
474+
)
475+
476+
expected = "https://ik.imagekit.io/test_url_endpoint/test_path1.jpg?tr=w-300,h-200,q-85,b-5_FF0000,e-bgremove,e-dropshadow"
477+
assert_equal(expected, url)
478+
end
453479
end

test/imagekit/custom-tests/url-generation/overlay_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,4 +724,40 @@ def test_should_properly_encode_overlay_text_when_transformations_are_in_query_p
724724
expected = "https://ik.imagekit.io/demo/sample.jpg?tr=l-text,i-Minimal%20Text,l-end"
725725
assert_equal(expected, url)
726726
end
727+
728+
# Hash-based API test - verify that plain hashes work for overlays
729+
def test_should_work_with_plain_hashes_for_text_overlay
730+
# Using plain hashes for everything including overlay
731+
url = @client.helper.build_url(
732+
{
733+
src: "/base-image.jpg",
734+
url_endpoint: "https://ik.imagekit.io/test_url_endpoint",
735+
transformation_position: :path,
736+
transformation: [
737+
{
738+
width: 300,
739+
height: 200,
740+
overlay: {
741+
type: :text,
742+
text: "Hello World",
743+
position: {
744+
x: "10",
745+
y_: "20",
746+
focus: :center
747+
},
748+
transformation: [
749+
{
750+
font_size: 20,
751+
font_color: "FF0000"
752+
}
753+
]
754+
}
755+
}
756+
]
757+
}
758+
)
759+
760+
expected = "https://ik.imagekit.io/test_url_endpoint/tr:w-300,h-200,l-text,i-Hello%20World,lx-10,ly-20,lfo-center,fs-20,co-FF0000,l-end/base-image.jpg"
761+
assert_equal(expected, url)
762+
end
727763
end

test/imagekit/custom-tests/url-generation/signing_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,28 @@ def test_should_generate_signed_url_with_transformations_in_path_position_and_qu
209209
expected = "https://ik.imagekit.io/demo/tr:w-300,h-200/sdk-testing-files/future-search.png?version=2.0&ik-s=dd1ee8f83d019bc59fd57a5fc4674a11eb8a3496"
210210
assert_equal(expected, url)
211211
end
212+
213+
# Hash-based API test - verify that plain hashes work for signed URLs
214+
def test_should_work_with_plain_hashes_for_signed_url_with_transformations
215+
# Using plain hashes instead of model objects
216+
url = @client.helper.build_url(
217+
{
218+
src: "sdk-testing-files/future-search.png",
219+
url_endpoint: "https://ik.imagekit.io/demo/",
220+
transformation: [
221+
{
222+
width: 300,
223+
height: 200
224+
}
225+
],
226+
query_parameters: {
227+
"version" => "2.0"
228+
},
229+
signed: true
230+
}
231+
)
232+
233+
expected = "https://ik.imagekit.io/demo/sdk-testing-files/future-search.png?version=2.0&tr=w-300,h-200&ik-s=601d97a7834b7554f4dabf0d3fc3a219ceeb6b31"
234+
assert_equal(expected, url)
235+
end
212236
end

0 commit comments

Comments
 (0)