Class: Contentful::Management::Entry

Inherits:
Object
  • Object
show all
Extended by:
Resource::EntryFields
Includes:
Resource, Resource::Fields, Resource::Refresher, Resource::SystemProperties
Defined in:
lib/contentful/management/entry.rb

Overview

Direct Known Subclasses

DynamicEntry

Constant Summary

Constants included from Resource::SystemProperties

Resource::SystemProperties::SYS_COERCIONS

Constants included from Resource

Resource::COERCIONS

Instance Attribute Summary collapse

Attributes included from Resource::SystemProperties

#sys

Attributes included from Resource

#client, #default_locale, #properties, #raw_object, #request

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resource::EntryFields

fields_coercions

Methods included from Resource::Fields

#fields, included, #initialize, #inspect

Methods included from Resource::Refresher

#refresh_data, #reload

Methods included from Resource::SystemProperties

included, #initialize, #inspect

Methods included from Resource

#array?, #fields, #initialize, #inspect, #nested_locale_fields?, #sys

Instance Attribute Details

#content_typeObject

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

#archiveObject

Archives an entry. Returns a Contentful::Management::Entry.



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/contentful/management/entry.rb', line 127

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.

Returns:

  • (Boolean)


174
175
176
# File 'lib/contentful/management/entry.rb', line 174

def archived?
  sys[:archivedAt] ? true : false
end

#destroyObject

Destroys an entry. Returns true if succeed.



155
156
157
158
159
160
161
162
163
164
# File 'lib/contentful/management/entry.rb', line 155

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_for_queryObject

Parser for assets attributes from query. Returns a hash of existing fields.



185
186
187
188
189
190
191
192
193
# File 'lib/contentful/management/entry.rb', line 185

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]) if fields[field_name]
    end
  end
end

#fields_from_attributes(attributes) ⇒ Object



195
196
197
198
199
# File 'lib/contentful/management/entry.rb', line 195

def fields_from_attributes(attributes)
  attributes.each do |id, value|
    attributes[id] = {locale => parse_update_attribute(value)}
  end
end

#localeObject

Returns the currently supported local.



179
180
181
# File 'lib/contentful/management/entry.rb', line 179

def locale
  sys[:locale] || default_locale
end

#publishObject

Publishes an entry. Returns a Contentful::Management::Entry.



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/contentful/management/entry.rb', line 99

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.

Returns:

  • (Boolean)


168
169
170
# File 'lib/contentful/management/entry.rb', line 168

def published?
  sys[:publishedAt] ? true : false
end

#saveObject

If an entry is a new object gets created in the Contentful, otherwise the existing entry gets updated. See README for details.



88
89
90
91
92
93
94
95
# File 'lib/contentful/management/entry.rb', line 88

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

#unarchiveObject

Unarchives an entry. Returns a Contentful::Management::Entry.



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/contentful/management/entry.rb', line 141

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

#unpublishObject

Unpublishes an entry. Returns a Contentful::Management::Entry.



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/contentful/management/entry.rb', line 113

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.



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/contentful/management/entry.rb', line 72

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