Class: Contentful::Management::Asset
- Inherits:
-
Object
- Object
- Contentful::Management::Asset
- 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.
Instance Attribute Summary
Attributes included from Resource::SystemProperties
Attributes included from Resource
#client, #default_locale, #properties, #raw_object, #request
Instance Method Summary collapse
-
#fields_for_query ⇒ Object
Parser for assets attributes, creates appropriate form of request.
-
#image_url(options = {}) ⇒ String
Generates a URL for the Contentful Image API.
-
#locale ⇒ String
Returns currently supported local or default locale.
-
#process_file ⇒ Contentful::Management::Asset
Processing an Asset file.
-
#save ⇒ Contentful::Management::Asset
If an asset is a new object gets created in the Contentful, otherwise the existing asset gets updated.
Methods included from Resource::AssetFields
Methods included from Resource::AllPublished
Methods included from Resource::Publisher
#publish, #published?, #unpublish
Methods included from Resource::Archiver
#archive, #archived?, #unarchive
Methods included from Resource::Refresher
Methods included from Resource::Fields
Methods included from Resource
#array?, #destroy, #fields, #nested_locale_fields?, #resource?, #sys, #update
Instance Method Details
#fields_for_query ⇒ Object
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
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/contentful/management/asset.rb', line 114 def image_url( = {}) query = { w: [:w] || [:width], h: [:h] || [:height], fm: [:fm] || [:format], q: [:q] || [:quality] }.select { |_k, value| value } query.empty? ? file.url : "#{file.url}?#{URI.encode_www_form(query)}" end |
#locale ⇒ String
Returns currently supported local or default locale.
82 83 84 |
# File 'lib/contentful/management/asset.rb', line 82 def locale sys && sys[:locale] ? sys[:locale] : default_locale end |
#process_file ⇒ Contentful::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 |
#save ⇒ Contentful::Management::Asset
If an asset is a new object gets created in the Contentful, otherwise the existing asset gets updated.
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 |