Skip to content

Commit ebf5887

Browse files
committed
refactor: streamline model object handling and enhance safe property access
1 parent f35d7eb commit ebf5887

File tree

1 file changed

+23
-58
lines changed

1 file changed

+23
-58
lines changed

lib/imagekit/helpers/helper.rb

Lines changed: 23 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ class Helper
2323
# @param options [Imagekit::Models::SrcOptions] Options for generating ImageKit URLs with transformations
2424
# @return [String] The built URL with transformations
2525
def build_url(options)
26-
# Convert to hash if it's a model object
27-
opts = options.respond_to?(:to_h) ? options.to_h : options.dup
26+
# Convert model to hash - all inputs are expected to be BaseModel objects
27+
opts = options.to_h
2828

2929
# Set defaults
30-
opts[:url_endpoint] ||= opts["url_endpoint"] || ""
31-
opts[:src] ||= opts["src"] || ""
32-
opts[:transformation_position] ||= opts["transformation_position"] || :query
30+
opts[:url_endpoint] ||= ""
31+
opts[:src] ||= ""
32+
opts[:transformation_position] ||= :query
3333

3434
return "" if opts[:src].nil? || opts[:src].empty?
3535

@@ -48,14 +48,14 @@ def build_url(options)
4848
end
4949

5050
# Add query parameters
51-
query_params = opts[:query_parameters] || opts["query_parameters"] || {}
51+
query_params = opts[:query_parameters] || {}
5252
existing_params = CGI.parse(url_obj.query || "")
5353
query_params.each do |key, value|
5454
existing_params[key.to_s] = [value.to_s]
5555
end
5656

5757
# Build transformation string
58-
transformation_string = build_transformation_string(opts[:transformation] || opts["transformation"])
58+
transformation_string = build_transformation_string(opts[:transformation])
5959

6060
add_as_query = TransformationUtils.add_as_query_parameter?(opts) || is_src_parameter_used_for_url
6161
transformation_placeholder = "PLEASEREPLACEJUSTBEFORESIGN"
@@ -136,18 +136,17 @@ def build_transformation_string(transformations)
136136
transformations.each do |transform|
137137
next unless transform
138138

139-
# Convert to hash if it's a model object
140-
current_transform = transform.respond_to?(:to_h) ? transform.to_h : transform
141-
next unless current_transform.is_a?(Hash)
139+
# Convert model to hash - all transformation inputs are expected to be BaseModel objects
140+
current_transform = transform.to_h
142141

143142
parsed_transform_step = []
144143

145144
current_transform.each do |key, value|
146145
next if value.nil? || value.to_s.empty?
147146

148147
# Handle overlay separately
149-
if key.to_s == "overlay" && (value.is_a?(Hash) || value.respond_to?(:to_h))
150-
# Pass model object or hash directly to process_overlay
148+
if key.to_s == "overlay" && value
149+
# Pass model object directly to process_overlay
151150
raw_string = process_overlay(value)
152151
if raw_string && !raw_string.strip.empty?
153152
parsed_transform_step << raw_string
@@ -325,35 +324,22 @@ def path_join(parts, separator = "/")
325324
def process_overlay(overlay)
326325
return "" unless overlay
327326

328-
# Get the overlay type - handle both model objects and hashes
329-
if overlay.respond_to?(:type)
330-
# Model object - access type directly
331-
type = overlay.type
332-
overlay_obj = overlay
333-
elsif overlay.is_a?(Hash)
334-
# Hash - look for type key
335-
type = overlay[:type] || overlay["type"]
336-
# Convert to symbol keys if needed
337-
overlay_obj = {}
338-
overlay.each { |k, v| overlay_obj[k.to_sym] = v }
339-
else
340-
return ""
341-
end
342-
327+
# All overlay inputs are expected to be BaseModel objects with type accessor
328+
type = overlay.type
343329
return "" unless type
344330

345331
# Determine overlay type based on explicit type field
346332
case type.to_s
347333
when "text"
348-
process_text_overlay(overlay_obj)
334+
process_text_overlay(overlay)
349335
when "image"
350-
process_image_overlay(overlay_obj)
336+
process_image_overlay(overlay)
351337
when "video"
352-
process_video_overlay(overlay_obj)
338+
process_video_overlay(overlay)
353339
when "subtitle"
354-
process_subtitle_overlay(overlay_obj)
340+
process_subtitle_overlay(overlay)
355341
when "solidColor"
356-
process_solid_color_overlay(overlay_obj)
342+
process_solid_color_overlay(overlay)
357343
else
358344
""
359345
end
@@ -485,33 +471,12 @@ def process_solid_color_overlay(overlay)
485471
parts.join(",")
486472
end
487473

488-
# Safe property access that handles both hashes and model objects
474+
# Safe property access for model objects
489475
def safe_get(obj, key)
490476
return nil unless obj
491477

492-
# For model objects, try method access first
493-
if obj.respond_to?(key.to_sym)
494-
begin
495-
return obj.send(key.to_sym)
496-
rescue StandardError
497-
# Fall through to hash access if method fails
498-
end
499-
end
500-
501-
# For hashes, try symbol first, then string
502-
if obj.respond_to?(:[])
503-
begin
504-
return obj[key.to_sym]
505-
rescue StandardError
506-
begin
507-
return obj[key.to_s]
508-
rescue StandardError
509-
return nil
510-
end
511-
end
512-
end
513-
514-
nil
478+
# All inputs are expected to be BaseModel objects with property accessors
479+
obj.respond_to?(key.to_sym) ? obj.send(key.to_sym) : nil
515480
end
516481

517482
# Add overlay properties like position, timing, transformations (matching Node.js)
@@ -520,7 +485,7 @@ def add_overlay_properties(parts, overlay)
520485
position = safe_get(overlay, :position)
521486
if position
522487
x = safe_get(position, :x)
523-
y = safe_get(position, :y) || safe_get(position, :y_)
488+
y = safe_get(position, :y_)
524489
focus = safe_get(position, :focus)
525490

526491
parts << "lx-#{x}" if x
@@ -532,7 +497,7 @@ def add_overlay_properties(parts, overlay)
532497
timing = safe_get(overlay, :timing)
533498
if timing
534499
start = safe_get(timing, :start)
535-
end_time = safe_get(timing, :end) || safe_get(timing, :end_)
500+
end_time = safe_get(timing, :end_)
536501
duration = safe_get(timing, :duration)
537502

538503
parts << "lso-#{start.to_i}" if start

0 commit comments

Comments
 (0)