Class: Contentful::Management::Asset

Inherits:
Object
  • Object
show all
Extended by:
Resource::AllPublished, Resource::AssetFields
Includes:
Resource, Resource::Archiver, Resource::Fields, 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

#client, #default_locale, #properties, #raw_object, #request

Instance Method Summary collapse

Methods included from Resource::AssetFields

fields_coercions

Methods included from Resource::AllPublished

all_published

Methods included from Resource::Publisher

#publish, #published?, #unpublish

Methods included from Resource::Archiver

#archive, #archived?, #unarchive

Methods included from Resource::Refresher

#reload

Methods included from Resource::Fields

#fields, included

Methods included from Resource

#array?, #destroy, #fields, #nested_locale_fields?, #resource?, #sys, #update

Instance Method Details

#fields_for_queryObject

Parser for assets attributes, creates appropriate form of request.



87
88
89
90
91
92
93
# File 'lib/contentful/management/asset.rb', line 87

def fields_for_query
  self.class.fields_coercions.keys.each_with_object({}) do |field_name, results|
    results[field_name] = @fields.each_with_object({}) do |(locale, fields), field_results|
      field_results[locale] = 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


114
115
116
117
118
119
120
121
122
123
# File 'lib/contentful/management/asset.rb', line 114

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 local or default locale.

Returns:

  • (String)

    current_locale



82
83
84
# File 'lib/contentful/management/asset.rb', line 82

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

#process_fileContentful::Management::Asset

Processing an Asset file



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/contentful/management/asset.rb', line 52

def process_file
  instance_variable_get(:@fields).keys.each do |locale|
    request = Request.new(
      client,
      "/#{space.id}/assets/#{id}/files/#{locale}/process",
      {},
      nil,
      version: sys[:version]
    )
    request.put
  end
  sys[:version] += 1
  self
end

#saveContentful::Management::Asset

If an asset is a new object gets created in the Contentful, otherwise the existing asset gets updated.

Returns:

See Also:

  • https://github.com/contentful/contentful-management.rb for details.


71
72
73
74
75
76
77
78
# File 'lib/contentful/management/asset.rb', line 71

def save
  if id
    update(title: title, description: description, file: file)
  else
    new_instance = self.class.create(client, sys[:space].id, fields: instance_variable_get(:@fields))
    refresh_data(new_instance)
  end
end