Class: Contentful::Asset

Inherits:
Object
  • Object
show all
Includes:
Resource, Resource::AssetFields, Resource::SystemProperties
Defined in:
lib/contentful/asset.rb

Overview

Constant Summary

Constants included from Resource::AssetFields

Resource::AssetFields::FIELDS_COERCIONS

Instance Attribute Summary

Attributes included from Resource::SystemProperties

#sys

Attributes included from Resource

#client, #default_locale, #properties, #raw, #request

Instance Method Summary collapse

Methods included from Resource::AssetFields

#fields

Methods included from Resource

#array?, #fields, #localized?, #reload, #sys

Instance Method Details

#image_url(options = {}) ⇒ String

Generates a URL for the Contentful Image API

Parameters:

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

Options Hash (options):

  • :width (Integer)
  • :height (Integer)
  • :format (String)
  • :quality (String)

Returns:

  • (String)

    Image API URL

See Also:

  • https://www.contentful.com/developers/documentation/content-delivery-api/#image-asset-resizing


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/contentful/asset.rb', line 22

def image_url(options = {})
  query = {
    w: options[:w] || options[:width],
    h: options[:h] || options[:height],
    fm: options[:fm] || options[:format],
    q: options[:q] || options[:quality],
    f: options[:f] || options[:focus],
    fit: options[:fit]
  }.reject { |_k, v| v.nil? }

  if query.empty?
    file.url
  else
    "#{file.url}?#{URI.encode_www_form(query)}"
  end
end