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, #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.
-
#locale ⇒ Object
Returns currently supported local or default locale.
-
#process_files ⇒ Object
This method is used only when an asset is created.
-
#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.
21 22 23 24 25 26 |
# File 'lib/contentful/management/asset.rb', line 21 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.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/contentful/management/asset.rb', line 41 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.process_files if result.is_a? self 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.
31 32 33 34 35 36 |
# File 'lib/contentful/management/asset.rb', line 31 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.
125 126 127 128 129 130 |
# File 'lib/contentful/management/asset.rb', line 125 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.
149 150 151 |
# File 'lib/contentful/management/asset.rb', line 149 def archived? !sys[:archivedAt].nil? end |
#destroy ⇒ Object
Destroys an asset. Returns true if succeed.
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/contentful/management/asset.rb', line 94 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.
159 160 161 162 163 164 165 |
# File 'lib/contentful/management/asset.rb', line 159 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] = field_name == :file ? (fields[field_name] ? fields[field_name].properties : nil) : fields[field_name] end end end |
#locale ⇒ Object
Returns currently supported local or default locale.
154 155 156 |
# File 'lib/contentful/management/asset.rb', line 154 def locale sys && sys[:locale] ? sys[:locale] : default_locale end |
#process_files ⇒ Object
This method is used only when an asset is created. Processes the uploaded file.
59 60 61 62 63 64 65 66 |
# File 'lib/contentful/management/asset.rb', line 59 def process_files 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.
107 108 109 110 111 112 |
# File 'lib/contentful/management/asset.rb', line 107 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.
143 144 145 |
# File 'lib/contentful/management/asset.rb', line 143 def published? !sys[:publishedAt].nil? 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.
83 84 85 86 87 88 89 90 |
# File 'lib/contentful/management/asset.rb', line 83 def save if id.nil? new_instance = self.class.create(self.sys[:space].id, {fields: instance_variable_get(:@fields)}) refresh_data(new_instance) else update(title: title, description: description, file: file) end end |
#unarchive ⇒ Object
Unarchvie an asset. Returns a Contentful::Management::Asset.
134 135 136 137 138 139 |
# File 'lib/contentful/management/asset.rb', line 134 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.
116 117 118 119 120 121 |
# File 'lib/contentful/management/asset.rb', line 116 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.
71 72 73 74 75 76 77 78 79 |
# File 'lib/contentful/management/asset.rb', line 71 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 |