Class: Api::Image

Inherits:
ActiveResource::Base
  • Object
show all
Includes:
Models::Imageable
Defined in:
app/models/api/image.rb

Overview

An activeresource wrapper to load up image asset from an API endpoint

Constant Summary

Constants included from Models::Imageable

Models::Imageable::STANDARD_SIZES, Models::Imageable::STANDARD_THUMBNAIL_SIZE, Models::Imageable::VALID_IMAGE_URL_OPTIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Imageable

#aspect_ratio, #human_size, #ik_file_name, #ik_file_name_with_extension, #ik_get_file_details, #ik_get_metadata, #ik_get_metadata_by_url, #ik_raw_url, #ik_url, #image_info, #image_url, #info, #presets_hash, #sourceset, suggested_sources_for_select, #thumbnail_url, #to_s

Class Method Details

._cr_findObject

Wrap cached_resource's find with per-key mutex coalescing.
alias_method captures cached_resource's patched find at class load time.



27
# File 'app/models/api/image.rb', line 27

alias_method :_cr_find, :find

.find(*args, **options) ⇒ Object



29
30
31
32
33
# File 'app/models/api/image.rb', line 29

def find(*args, **options)
  key = args.first.to_s
  lock = FETCH_LOCKS.compute_if_absent(key) { Mutex.new }
  lock.synchronize { _cr_find(*args, **options) }
end

Instance Method Details

#ik_pathObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/api/image.rb', line 36

def ik_path
  # ImageKit 4.0 returns snake_case keys: file_path instead of filePath
  # Handle both hash access and method access for API responses
  path = if asset.respond_to?(:attributes) && asset.attributes.is_a?(Hash)
           # ActiveResource objects store data in attributes hash
           asset.attributes['file_path'] || asset.attributes['filePath']
         elsif asset.respond_to?(:file_path)
           asset.file_path || asset.filePath
         elsif asset.is_a?(Hash)
           asset['file_path'] || asset['filePath'] || asset[:file_path] || asset[:filePath]
         end
  
  # Only fall back to slug-based path if we couldn't get a path from asset
  # Use slug without extension since ImageKit handles format dynamically
  path.presence || "/img/#{slug}"
end