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)
  • :focus (String)
  • :fit (String)
  • :fl (String)

    File Layering - ‘progressive’

Returns:

  • (String)

    Image API URL

See Also:

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


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/contentful/asset.rb', line 37

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],
    fl: options[:fl]
  }.reject { |_k, v| v.nil? }

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