class Contentful::Management::Asset

Resource class for Asset. @see _ www.contentful.com/developers/documentation/content-management-api/#resources-assets

Public Class Methods

client_association_class() click to toggle source

@private

# File lib/contentful/management/asset.rb, line 28
def self.client_association_class
  ClientAssetMethodsFactory
end
create_attributes(client, attributes) click to toggle source

@private

# File lib/contentful/management/asset.rb, line 38
def self.create_attributes(client, attributes)
  fields = attributes[:fields] || {}
  locale = attributes[:locale] || client.default_locale
  fields[:title] = { locale => attributes[:title] } if attributes[:title]
  fields[:description] = { locale => attributes[:description] } if attributes[:description]
  fields[:file] = { locale => attributes[:file].properties } if attributes[:file]

  { fields: fields, metadata: attributes[:_metadata] }
end
pre_process_params(parameters) click to toggle source

@private

# File lib/contentful/management/asset.rb, line 33
def self.pre_process_params(parameters)
  Support.normalize_select!(parameters)
end

Public Instance Methods

after_create(attributes) click to toggle source

@private

# File lib/contentful/management/asset.rb, line 49
def after_create(attributes)
  self.locale = attributes[:locale] || client.default_locale
end
fields_for_query() click to toggle source

Parser for assets attributes, creates appropriate form of request.

# 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
get_value_from(fields, field_name) click to toggle source

@private

# File lib/contentful/management/asset.rb, line 87
def get_value_from(fields, field_name)
  if field_name == :file
    fields[field_name].properties if fields[field_name]
  else
    fields[field_name]
  end
end
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 @see _ www.contentful.com/developers/documentation/content-delivery-api/#image-asset-resizing

@return [String] Image API URL

# 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
locale() click to toggle source

Returns currently supported locale or default locale. @return [String] current_locale

# File lib/contentful/management/asset.rb, line 73
def locale
  sys && sys[:locale] ? sys[:locale] : default_locale
end
process_file() click to toggle source

Processing an Asset file

@return [Contentful::Management::Asset]

# 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

Protected Instance Methods

process_url(locale_code) click to toggle source
# File lib/contentful/management/asset.rb, line 118
def process_url(locale_code)
  "spaces/#{space.id}/environments/#{environment_id}/assets/#{id}/files/#{locale_code}/process"
end
query_attributes(attributes) click to toggle source
# File lib/contentful/management/asset.rb, line 122
def query_attributes(attributes)
  self.title = attributes[:title] || title
  self.description = attributes[:description] || description
  self.file = attributes[:file] || file

  { fields: fields_for_query, metadata: attributes[:_metadata] }
end