Class: Oembed::ImageProvider

Inherits:
Object
  • Object
show all
Defined in:
app/services/oembed/image_provider.rb

Defined Under Namespace

Classes: ImageNotFoundError

Constant Summary collapse

VALID_IMAGE_OPTIONS =

All valid options that can be passed to image_url
See Models::Imageable::VALID_IMAGE_URL_OPTIONS

%i[
  encode_format size borderx bordery size_modifier percentage
  width height crop_x crop_y crop_w crop_h crop_mode crop_last rotate relative
  hostname cache webp download optimize thumbnail background progressive_jpeg blur
  quality focus zoom radius border border_color
].freeze
VALID_DISPLAY_OPTIONS =

Display options for the rendered tag

%i[
  caption lazyload include_srcset sizes class alt link link_target
  wrapper_tag
].freeze
DEFAULT_MAX_WIDTH =

Default max width to prevent enormous images from being inserted
The srcset will still provide smaller sizes for appropriate viewports

2000

Instance Method Summary collapse

Instance Method Details

#get(options = {}) ⇒ Hash

Get rendered image HTML

Parameters:

  • options (Hash) (defaults to: {})

    Options hash

Options Hash (options):

  • :image_id (Integer)

    Image ID (required)

  • :caption (String)

    Optional caption text

  • :width (Integer)

    Target width

  • :height (Integer)

    Target height

  • :crop_x (Integer)

    Crop X offset

  • :crop_y (Integer)

    Crop Y offset

  • :crop_w (Integer)

    Crop width

  • :crop_h (Integer)

    Crop height

  • :crop_mode (String)

    Crop mode (pad_resize, force, etc.)

  • :rotate (Integer)

    Rotation degrees

  • :blur (Integer)

    Blur amount (1-100)

  • :background (String)

    Background color for padding

  • :encode_format (String)

    Output format (jpeg, png, webp)

  • :lazyload (Boolean)

    Enable lazy loading (default: true)

  • :include_srcset (Boolean)

    Include responsive srcset (default: true)

  • :link (String)

    URL to wrap image in link

  • :link_target (String)

    Link target (_blank, etc.)

Returns:

  • (Hash)

    oEmbed-like response with :html, :type, :provider_name, etc.

Raises:



59
60
61
62
63
64
65
66
67
# File 'app/services/oembed/image_provider.rb', line 59

def get(options = {})
  image_id = options[:image_id]
  raise ImageNotFoundError, 'No image ID provided' if image_id.blank?

  image = Image.find_by(id: image_id)
  raise ImageNotFoundError, "Image not found: #{image_id}" unless image

  build_response(image, options)
end