class Contentful::Asset

Resource class for Asset. www.contentful.com/developers/documentation/content-delivery-api/#assets

Public Class Methods

new(*) click to toggle source
Calls superclass method
# File lib/contentful/asset.rb, line 33
def initialize(*)
  super
  create_files!
  define_asset_methods!
end

Public Instance Methods

image_url(options = {}) click to toggle source

Generates a URL for the Contentful Image API

@param [Hash] options @option options [Integer] :width @option options [Integer] :height @option options [String] :format @option options [String] :quality @option options [String] :focus @option options [String] :fit @option options [String] :fl File Layering - ‘progressive’ @option options [String] :background @see _ www.contentful.com/developers/documentation/content-delivery-api/#image-asset-resizing

@return [String] Image API URL

# File lib/contentful/asset.rb, line 53
def image_url(options = {})
  query = build_query(options)

  if query.empty?
    file.url
  else
    "#{file.url}?#{URI.encode_www_form(query)}"
  end
end
Also aliased as: url
inspect() click to toggle source

@private

# File lib/contentful/asset.rb, line 29
def inspect
  "<#{repr_name} id='#{sys[:id]}' url='#{url}'>"
end
marshal_dump() click to toggle source

@private

Calls superclass method
# File lib/contentful/asset.rb, line 12
def marshal_dump
  super.merge(raw: raw)
end
marshal_load(raw_object) click to toggle source

@private

Calls superclass method
# File lib/contentful/asset.rb, line 17
def marshal_load(raw_object)
  super(raw_object)
  create_files!
  define_asset_methods!
end
url(options = {})
Alias for: image_url

Private Instance Methods

build_query(options) click to toggle source
# File lib/contentful/asset.rb, line 67
def build_query(options)
  {
    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],
    bg: options[:bg] || options[:background],
    r: options[:r] || options[:radius],
    fit: options[:fit],
    fl: options[:fl]
  }.reject { |_k, v| v.nil? }
end
create_files!() click to toggle source
# File lib/contentful/asset.rb, line 81
def create_files!
  file_json = raw.fetch('fields', {}).fetch('file', nil)
  return if file_json.nil?

  is_localized = file_json.keys.none? { |f| %w[fileName contentType details url].include? f }
  if is_localized
    locales.each do |locale|
      @fields[locale][:file] = ::Contentful::File.new(file_json[locale.to_s] || {}, @configuration)
    end
  else
    @fields[internal_resource_locale][:file] = ::Contentful::File.new(file_json, @configuration)
  end
end
define_asset_methods!() click to toggle source
# File lib/contentful/asset.rb, line 95
def define_asset_methods!
  define_singleton_method :title do
    fields.fetch(:title, nil)
  end

  define_singleton_method :description do
    fields.fetch(:description, nil)
  end

  define_singleton_method :file do |wanted_locale = nil|
    fields(wanted_locale)[:file]
  end
end