Class: Contentful::Management::Entry
- Inherits:
-
Object
- Object
- Contentful::Management::Entry
- Extended by:
- Resource::EntryFields
- Defined in:
- lib/contentful/management/entry.rb
Overview
Resource class for Entry. www.contentful.com/developers/documentation/content-management-api/#resources-entries
Direct Known Subclasses
Constant Summary
Constants included from Resource::SystemProperties
Resource::SystemProperties::SYS_COERCIONS
Constants included from Resource
Instance Attribute Summary collapse
-
#content_type ⇒ Object
Returns the value of attribute content_type.
Attributes included from Resource::SystemProperties
Attributes included from Resource
#client, #default_locale, #properties, #raw_object, #request
Class Method Summary collapse
-
.all(space_id, parameters = {}) ⇒ Object
Gets a collection of entries.
-
.create(content_type, attributes) ⇒ Object
Creates an entry.
-
.find(space_id, entry_id) ⇒ Object
Gets a specific entry.
Instance Method Summary collapse
-
#archive ⇒ Object
Archives an entry.
-
#archived? ⇒ Boolean
Checks if an entry is archived.
-
#destroy ⇒ Object
Destroys an entry.
- #fields(wanted_locale = default_locale) ⇒ Object
-
#fields_for_query ⇒ Object
Parser for assets attributes from query.
- #fields_from_attributes(attributes) ⇒ Object
-
#locale ⇒ Object
Returns the currently supported local.
-
#publish ⇒ Object
Publishes an entry.
-
#published? ⇒ Boolean
Checks if an entry is published.
-
#save ⇒ Object
If an entry is a new object gets created in the Contentful, otherwise the existing entry gets updated.
-
#unarchive ⇒ Object
Unarchives an entry.
-
#unpublish ⇒ Object
Unpublishes an entry.
-
#update(attributes) ⇒ Object
Updates an entry.
Methods included from Resource::EntryFields
Methods included from Resource::Fields
included, #initialize, #inspect
Methods included from Resource::Refresher
Methods included from Resource::SystemProperties
included, #initialize, #inspect
Methods included from Resource
#array?, #initialize, #inspect, #nested_locale_fields?, #sys
Instance Attribute Details
#content_type ⇒ Object
Returns the value of attribute content_type.
16 17 18 |
# File 'lib/contentful/management/entry.rb', line 16 def content_type @content_type end |
Class Method Details
.all(space_id, parameters = {}) ⇒ Object
Gets a collection of entries. Takes an id of space and hash of parameters with optional content_type_id. Returns a Contentful::Management::Array of Contentful::Management::Entry.
21 22 23 24 25 26 27 28 29 |
# File 'lib/contentful/management/entry.rb', line 21 def self.all(space_id, parameters = {}) request = Request.new( "/#{ space_id }/entries", parameters ) response = request.get result = ResourceBuilder.new(response, {}, {}) result.run end |
.create(content_type, attributes) ⇒ Object
Creates an entry. Takes a content type object and hash with attributes of content type. Returns a Contentful::Management::Entry.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/contentful/management/entry.rb', line 44 def self.create(content_type, attributes) custom_id = attributes[:id] locale = attributes[:locale] fields_for_create = if attributes[:fields] # create from initialized dynamic entry via save tmp_entry = new tmp_entry.instance_variable_set(:@fields, attributes.delete(:fields) || {}) Contentful::Management::Support.deep_hash_merge(tmp_entry.fields_for_query, tmp_entry.fields_from_attributes(attributes)) else fields_with_locale content_type, attributes end request = Request.new( "/#{ content_type.sys[:space].id }/entries/#{ custom_id }", {fields: fields_for_create}, nil, content_type_id: content_type.id ) response = custom_id.nil? ? request.post : request.put result = ResourceBuilder.new(response, {}, {}) client.register_dynamic_entry(content_type.id, DynamicEntry.create(content_type)) entry = result.run entry.locale = locale if locale entry end |
.find(space_id, entry_id) ⇒ Object
Gets a specific entry. Takes an id of space and entry. Returns a Contentful::Management::Entry.
34 35 36 37 38 39 |
# File 'lib/contentful/management/entry.rb', line 34 def self.find(space_id, entry_id) request = Request.new("/#{ space_id }/entries/#{ entry_id }") response = request.get result = ResourceBuilder.new(response, {}, {}) result.run end |
Instance Method Details
#archive ⇒ Object
Archives an entry. Returns a Contentful::Management::Entry.
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/contentful/management/entry.rb', line 135 def archive request = Request.new( "/#{ space.id }/entries/#{ id }/archived", {}, id = nil, version: sys[:version] ) response = request.put result = ResourceBuilder.new(response, {}, {}).run refresh_data(result) end |
#archived? ⇒ Boolean
Checks if an entry is archived. Returns true if published.
182 183 184 |
# File 'lib/contentful/management/entry.rb', line 182 def archived? sys[:archivedAt] ? true : false end |
#destroy ⇒ Object
Destroys an entry. Returns true if succeed.
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/contentful/management/entry.rb', line 163 def destroy request = Request.new("/#{ space.id }/entries/#{ id }") response = request.delete if response.status == :no_content return true else result = ResourceBuilder.new(response, {}, {}) result.run end end |
#fields(wanted_locale = default_locale) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/contentful/management/entry.rb', line 69 def fields(wanted_locale = default_locale) requested_locale = locale || wanted_locale @fields[requested_locale] = {} unless @fields[requested_locale] default_fields = @fields[default_locale] || {} default_fields.merge(@fields[requested_locale]) end |
#fields_for_query ⇒ Object
Parser for assets attributes from query. Returns a hash of existing fields.
193 194 195 196 197 198 199 200 201 |
# File 'lib/contentful/management/entry.rb', line 193 def fields_for_query raw_fields = instance_variable_get(:@fields) fields_names = raw_fields.first[1].keys fields_names.each_with_object({}) do |field_name, results| results[field_name] = raw_fields.each_with_object({}) do |(locale, fields), field_results| field_results[locale] = parse_update_attribute(fields[field_name]) end end end |
#fields_from_attributes(attributes) ⇒ Object
203 204 205 206 207 |
# File 'lib/contentful/management/entry.rb', line 203 def fields_from_attributes(attributes) attributes.each do |id, value| attributes[id] = {locale => parse_update_attribute(value)} end end |
#locale ⇒ Object
Returns the currently supported local.
187 188 189 |
# File 'lib/contentful/management/entry.rb', line 187 def locale sys[:locale] || default_locale end |
#publish ⇒ Object
Publishes an entry. Returns a Contentful::Management::Entry.
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/contentful/management/entry.rb', line 107 def publish request = Request.new( "/#{ space.id }/entries/#{ id }/published", {}, id = nil, version: sys[:version] ) response = request.put result = ResourceBuilder.new(response, {}, {}).run refresh_data(result) end |
#published? ⇒ Boolean
Checks if an entry is published. Returns true if published.
176 177 178 |
# File 'lib/contentful/management/entry.rb', line 176 def published? sys[:publishedAt] ? true : false end |
#save ⇒ Object
If an entry is a new object gets created in the Contentful, otherwise the existing entry gets updated. See README for details.
96 97 98 99 100 101 102 103 |
# File 'lib/contentful/management/entry.rb', line 96 def save if id update({}) else new_instance = Contentful::Management::Entry.create(content_type, fields: instance_variable_get(:@fields)) refresh_data(new_instance) end end |
#unarchive ⇒ Object
Unarchives an entry. Returns a Contentful::Management::Entry.
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/contentful/management/entry.rb', line 149 def unarchive request = Request.new( "/#{ space.id }/entries/#{ id }/archived", {}, id = nil, version: sys[:version] ) response = request.delete result = ResourceBuilder.new(response, {}, {}).run refresh_data(result) end |
#unpublish ⇒ Object
Unpublishes an entry. Returns a Contentful::Management::Entry.
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/contentful/management/entry.rb', line 121 def unpublish request = Request.new( "/#{ space.id }/entries/#{ id }/published", {}, id = nil, version: sys[:version] ) response = request.delete result = ResourceBuilder.new(response, {}, {}).run refresh_data(result) end |
#update(attributes) ⇒ Object
Updates an entry. Takes an optional hash with attributes of content type. Returns a Contentful::Management::Entry.
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/contentful/management/entry.rb', line 80 def update(attributes) fields_for_update = Contentful::Management::Support.deep_hash_merge(fields_for_query, fields_from_attributes(attributes)) request = Request.new( "/#{ space.id }/entries/#{ id }", {fields: fields_for_update}, id = nil, version: sys[:version] ) response = request.put result = ResourceBuilder.new(response, {}, {}).run refresh_data(result) end |