Class: Contentful::Management::Asset

Inherits:
Object
  • Object
show all
Extended by:
Resource::AssetFields
Includes:
Resource, Resource::Archiver, Resource::EnvironmentAware, Resource::Fields, Resource::Metadata, Resource::Publisher, Resource::Refresher, Resource::SystemProperties
Defined in:
lib/contentful/management/asset.rb

Overview

Resource class for Asset.

See Also:

  • https://www.contentful.com/developers/documentation/content-management-api/#resources-assets

Instance Attribute Summary

Attributes included from Resource::SystemProperties

#sys

Attributes included from Resource::Metadata

#_metadata

Attributes included from Resource

#client, #properties, #raw_object, #request

Instance Method Summary collapse

Methods included from Resource::AssetFields

fields_coercions

Methods included from Resource::EnvironmentAware

#environment_id

Methods included from Resource::Publisher

#publish, #published?, #unpublish, #updated?

Methods included from Resource::Refresher

#reload

Methods included from Resource::Archiver

#archive, #archived?, #unarchive

Methods included from Resource::Fields

#fields, included

Methods included from Resource

#array?, #default_locale, #destroy, #environment_id, #fields, #nested_locale_fields?, #resource?, #save, #sys, #update

Instance Method Details

#fields_for_queryObject

Parser for assets attributes, creates appropriate form of request.



78
79
80
81
82
83
84
# File 'lib/contentful/management/asset.rb', line 78

def fields_for_query
  self.class.fields_coercions.keys.each_with_object({}) do |field_name, results|
    results[field_name] = @fields.transform_values do |fields|
      get_value_from(fields, field_name)
    end
  end
end

#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


105
106
107
108
109
110
111
112
113
114
# File 'lib/contentful/management/asset.rb', line 105

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]
  }.select { |_k, value| value }

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

#localeString

Returns currently supported locale or default locale.

Returns:

  • (String)

    current_locale



73
74
75
# File 'lib/contentful/management/asset.rb', line 73

def locale
  sys && sys[:locale] ? sys[:locale] : default_locale
end

#process_fileContentful::Management::Asset

Processing an Asset file



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/contentful/management/asset.rb', line 56

def process_file
  instance_variable_get(:@fields).each_key do |locale|
    request = Request.new(
      client,
      process_url(locale),
      {},
      nil,
      version: sys[:version]
    )
    request.put
  end
  sys[:version] += 1
  self
end