Class: Contentful::Asset

Inherits:
FieldsResource show all
Defined in:
lib/contentful/asset.rb

Overview

Instance Attribute Summary

Attributes inherited from BaseResource

#default_locale, #raw, #sys

Instance Method Summary collapse

Methods inherited from FieldsResource

#fields, #fields_with_locales, #locales

Methods inherited from BaseResource

#==, #reload

Constructor Details

#initializeAsset

Returns a new instance of Asset



25
26
27
28
29
# File 'lib/contentful/asset.rb', line 25

def initialize(*)
  super
  create_files!
  define_asset_methods!
end

Instance Method Details

#image_url(options = {}) ⇒ String Also known as: url

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


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/contentful/asset.rb', line 44

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