Class: Contentful::Management::Asset
- Inherits:
-
Object
- Object
- Contentful::Management::Asset
- Extended by:
- Resource::AssetFields
- Defined in:
- lib/contentful/management/asset.rb
Overview
Resource class for Asset. www.contentful.com/developers/documentation/content-management-api/#resources-assets
Constant Summary
Constants included from Resource::SystemProperties
Resource::SystemProperties::SYS_COERCIONS
Constants included from Resource
Instance Attribute Summary
Attributes included from Resource::SystemProperties
Attributes included from Resource
#client, #default_locale, #properties, #raw_object, #request
Class Method Summary collapse
-
.all(space_id, query = {}) ⇒ Object
Gets a collection of assets.
-
.create(space_id, attributes) ⇒ Object
Creates an asset.
-
.find(space_id, asset_id) ⇒ Object
Gets a specific asset.
Instance Method Summary collapse
-
#archive ⇒ Object
Archive an asset.
-
#archived? ⇒ Boolean
Checks if an asset is archvied.
-
#destroy ⇒ Object
Destroys an asset.
-
#fields_for_query ⇒ Object
Parser for assets attributes, creates appropriate form of request.
- #get_value_from(fields, field_name) ⇒ Object
-
#image_url(options = {}) ⇒ Object
Returns the image url of an asset Allows you to pass in the following options for image resizing: :width :height :format :quality See www.contentful.com/developers/documentation/content-delivery-api/#image-asset-resizing.
-
#locale ⇒ Object
Returns currently supported local or default locale.
-
#process_file ⇒ Object
Processing an Asset file.
-
#publish ⇒ Object
Publishes an asset.
-
#published? ⇒ Boolean
Checks if an asset is published.
-
#save ⇒ Object
If an asset is a new object gets created in the Contentful, otherwise the existing asset gets updated.
-
#unarchive ⇒ Object
Unarchvie an asset.
-
#unpublish ⇒ Object
Unpublishes an asset.
-
#update(attributes) ⇒ Object
Updates an asset.
Methods included from Resource::AssetFields
Methods included from Resource::Refresher
Methods included from Resource::SystemProperties
included, #initialize, #inspect
Methods included from Resource::Fields
#fields, included, #initialize, #inspect
Methods included from Resource
#array?, #fields, #initialize, #inspect, #nested_locale_fields?, #sys
Class Method Details
.all(space_id, query = {}) ⇒ Object
Gets a collection of assets. Takes an id of space and an optional hash of query options Returns a Contentful::Management::Array of Contentful::Management::Asset.
19 20 21 22 23 24 25 26 27 |
# File 'lib/contentful/management/asset.rb', line 19 def self.all(space_id, query = {}) request = Request.new( "/#{ space_id }/assets", query ) response = request.get result = ResourceBuilder.new(response, {}, {}) result.run end |
.create(space_id, attributes) ⇒ Object
Creates an asset. Takes a space id and hash with attributes (title, description, file) Returns a Contentful::Management::Asset.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/contentful/management/asset.rb', line 42 def self.create(space_id, attributes) locale = attributes[:locale] asset = new asset.instance_variable_set(:@fields, attributes[:fields] || {}) asset.locale = attributes[:locale] if attributes[:locale] asset.title = attributes[:title] if attributes[:title] asset.description = attributes[:description] if attributes[:description] asset.file = attributes[:file] if attributes[:file] request = Request.new( "/#{ space_id }/assets/#{ attributes[:id]}", fields: asset.fields_for_query ) response = attributes[:id].nil? ? request.post : request.put result = ResourceBuilder.new(response, {}, {}).run result.locale = locale if locale result end |
.find(space_id, asset_id) ⇒ Object
Gets a specific asset. Takes an id of space and asset. Returns a Contentful::Management::Asset.
32 33 34 35 36 37 |
# File 'lib/contentful/management/asset.rb', line 32 def self.find(space_id, asset_id) request = Request.new("/#{ space_id }/assets/#{ asset_id }") response = request.get result = ResourceBuilder.new(response, {}, {}) result.run end |
Instance Method Details
#archive ⇒ Object
Archive an asset. Returns a Contentful::Management::Asset.
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/contentful/management/asset.rb', line 148 def archive request = Request.new( "/#{ space.id }/assets/#{ id }/archived", {}, id = nil, version: sys[:version] ) response = request.put result = ResourceBuilder.new(response, {}, {}).run refresh_data(result) end |
#archived? ⇒ Boolean
Checks if an asset is archvied. Returns true if archived.
182 183 184 |
# File 'lib/contentful/management/asset.rb', line 182 def archived? sys[:archivedAt] ? true : false end |
#destroy ⇒ Object
Destroys an asset. Returns true if succeed.
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/contentful/management/asset.rb', line 107 def destroy request = Request.new("/#{ space.id }/assets/#{ id }") response = request.delete if response.status == :no_content return true else result = ResourceBuilder.new(response, {}, {}) result.run end end |
#fields_for_query ⇒ Object
Parser for assets attributes, creates appropriate form of request.
192 193 194 195 196 197 198 |
# File 'lib/contentful/management/asset.rb', line 192 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 |
#get_value_from(fields, field_name) ⇒ Object
200 201 202 203 204 205 206 |
# File 'lib/contentful/management/asset.rb', line 200 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 = {}) ⇒ Object
Returns the image url of an asset Allows you to pass in the following options for image resizing:
:width
:height
:format
:quality
See www.contentful.com/developers/documentation/content-delivery-api/#image-asset-resizing
215 216 217 218 219 220 221 222 223 224 |
# File 'lib/contentful/management/asset.rb', line 215 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 ⇒ Object
Returns currently supported local or default locale.
187 188 189 |
# File 'lib/contentful/management/asset.rb', line 187 def locale sys && sys[:locale] ? sys[:locale] : default_locale end |
#process_file ⇒ Object
Processing an Asset file
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/contentful/management/asset.rb', line 62 def process_file instance_variable_get(:@fields).keys.each do |locale| request = Request.new( "/#{ space.id }/assets/#{ id }/files/#{ locale }/process", {}, id = nil, version: sys[:version] ) request.put end sys[:version] += 1 self end |
#publish ⇒ Object
Publishes an asset. Returns a Contentful::Management::Asset.
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/contentful/management/asset.rb', line 120 def publish request = Request.new( "/#{ space.id }/assets/#{ id }/published", {}, id = nil, version: sys[:version] ) response = request.put result = ResourceBuilder.new(response, {}, {}).run refresh_data(result) end |
#published? ⇒ Boolean
Checks if an asset is published. Returns true if published.
176 177 178 |
# File 'lib/contentful/management/asset.rb', line 176 def published? sys[:publishedAt] ? true : false end |
#save ⇒ Object
If an asset is a new object gets created in the Contentful, otherwise the existing asset gets updated. See README for details.
96 97 98 99 100 101 102 103 |
# File 'lib/contentful/management/asset.rb', line 96 def save if id update(title: title, description: description, file: file) else new_instance = self.class.create(sys[:space].id, fields: instance_variable_get(:@fields)) refresh_data(new_instance) end end |
#unarchive ⇒ Object
Unarchvie an asset. Returns a Contentful::Management::Asset.
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/contentful/management/asset.rb', line 162 def unarchive request = Request.new( "/#{ space.id }/assets/#{ id }/archived", {}, id = nil, version: sys[:version] ) response = request.delete result = ResourceBuilder.new(response, {}, {}).run refresh_data(result) end |
#unpublish ⇒ Object
Unpublishes an asset. Returns a Contentful::Management::Asset.
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/contentful/management/asset.rb', line 134 def unpublish request = Request.new( "/#{ space.id }/assets/#{ id }/published", {}, id = nil, version: sys[:version] ) response = request.delete result = ResourceBuilder.new(response, {}, {}).run refresh_data(result) end |
#update(attributes) ⇒ Object
Updates an asset. Takes hash with attributes (title, description, file) Returns a Contentful::Management::Asset.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/contentful/management/asset.rb', line 79 def update(attributes) self.title = attributes[:title] if attributes[:title] self.description = attributes[:description] if attributes[:description] self.file = attributes[:file] if attributes[:file] request = Request.new( "/#{ space.id }/assets/#{ id }", {fields: fields_for_query}, id = nil, version: sys[:version] ) response = request.put result = ResourceBuilder.new(response, {}, {}).run refresh_data(result) end |