contentful-management 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/CHANGELOG.md +11 -6
  2. data/RELEASE.md +8 -0
  3. data/examples/blog.rb +2 -2
  4. data/examples/content_types.rb +2 -2
  5. data/examples/create_space.rb +0 -1
  6. data/lib/contentful/management/asset.rb +7 -8
  7. data/lib/contentful/management/client.rb +7 -7
  8. data/lib/contentful/management/content_type.rb +13 -13
  9. data/lib/contentful/management/content_type_entry_methods_factory.rb +1 -3
  10. data/lib/contentful/management/dynamic_entry.rb +8 -8
  11. data/lib/contentful/management/entry.rb +15 -29
  12. data/lib/contentful/management/field.rb +1 -1
  13. data/lib/contentful/management/locale.rb +2 -2
  14. data/lib/contentful/management/request.rb +3 -3
  15. data/lib/contentful/management/resource.rb +5 -5
  16. data/lib/contentful/management/resource/array_like.rb +2 -2
  17. data/lib/contentful/management/resource/asset_fields.rb +3 -3
  18. data/lib/contentful/management/resource/fields.rb +0 -1
  19. data/lib/contentful/management/resource/system_properties.rb +9 -9
  20. data/lib/contentful/management/resource_builder.rb +8 -8
  21. data/lib/contentful/management/response.rb +1 -1
  22. data/lib/contentful/management/space_asset_methods_factory.rb +1 -3
  23. data/lib/contentful/management/space_association_methods_factory.rb +2 -4
  24. data/lib/contentful/management/space_content_type_methods_factory.rb +1 -3
  25. data/lib/contentful/management/space_entry_methods_factory.rb +2 -4
  26. data/lib/contentful/management/space_locale_methods_factory.rb +1 -3
  27. data/lib/contentful/management/space_webhook_methods_factory.rb +2 -4
  28. data/lib/contentful/management/version.rb +1 -1
  29. data/spec/fixtures/vcr_cassettes/entry/create.yml +412 -85
  30. data/spec/fixtures/vcr_cassettes/entry/create_with_symbols.yml +2 -3
  31. data/spec/lib/contentful/management/content_type_spec.rb +2 -2
  32. data/spec/lib/contentful/management/entry_spec.rb +94 -6
  33. metadata +5 -6
  34. data/spec/fixtures/vcr_cassettes/entry/create_test.yml +0 -150
@@ -1,9 +1,14 @@
1
- ### 0.2.0
1
+ # Change Log
2
+
3
+ ## 0.2.1
4
+ * Fix: Use array for symbols in entry fields
5
+
6
+ ## 0.2.0
2
7
  * Fix: create entry with multiple locales, skip attributes for not localized fields in content types
3
8
  * Fix: reload Assets
4
9
  * add optional gzip envoding
5
10
 
6
- ### 0.1.0
11
+ ## 0.1.0
7
12
  * Support for webhooks
8
13
  * add image url to asset
9
14
  * Cleaning code
@@ -12,7 +17,7 @@
12
17
  * Fix: rename asset.process_files to asset.process
13
18
 
14
19
 
15
- ### 0.0.3
20
+ ## 0.0.3
16
21
  * Fix: next_page feature
17
22
  * Fix: create entry with specific locale
18
23
  * Fix: service unavailable error (503)
@@ -20,10 +25,10 @@
20
25
  * Code cleanup
21
26
  * More documentation
22
27
 
23
- ### 0.0.2
28
+ ## 0.0.2
24
29
  * Fix: Convert an Entry to a DynamicEntry after being created.
25
30
 
26
- ### 0.0.1
31
+ ## 0.0.1
27
32
  * Fix: Headers not properly cleared between requests
28
33
  * Fix: Create entries with custom identifier
29
34
  * Code cleanup
@@ -31,5 +36,5 @@
31
36
  * Adding filter by content_type id
32
37
 
33
38
 
34
- ### 0.0.1-pre
39
+ ## 0.0.1-pre
35
40
  * alpha pre-release
@@ -0,0 +1,8 @@
1
+ # Releasing new versions:
2
+
3
+ Make sure all points are taken care of:
4
+
5
+ - [ ] Make sure tests are green.
6
+ - [ ] Update Changelog: http://keepachangelog.com/
7
+ - [ ] Bump version number: http://semver.org/
8
+ - [ ] Release gem and communicate updates
@@ -49,7 +49,7 @@ category_type.activate
49
49
 
50
50
  sleep 10 # prevent race conditions
51
51
 
52
- puts "creating categories"
52
+ puts 'creating categories'
53
53
  entries = []
54
54
  entries << category_type.entries.create(category_name: 'Misc', category_description: 'Misc stuff')
55
55
  entries << category_type.entries.create(category_name: 'Serious', category_description: 'Serious stuff')
@@ -78,7 +78,7 @@ asset.save
78
78
 
79
79
  entries.map(&:publish)
80
80
 
81
- puts "Press any key to destroy space"
81
+ puts 'Press any key to destroy space'
82
82
  gets
83
83
 
84
84
  blog_space.destroy
@@ -17,7 +17,7 @@ p space.content_types.all
17
17
  field = Contentful::Management::Field.new
18
18
  field.id = 'such_content'
19
19
  field.name = 'Such Content'
20
- field.type = 'Text' #content types maybe as symbol?
20
+ field.type = 'Text' # content types maybe as symbol?
21
21
 
22
22
  type2 = space.content_types.new
23
23
  type2.name = 'ContentType 2'
@@ -27,7 +27,7 @@ type2.save
27
27
  field2 = Contentful::Management::Field.new
28
28
  field2.id = 'wow_content'
29
29
  field2.name = 'Wow Content'
30
- field2.type = 'Location' #content types maybe as symbol?
30
+ field2.type = 'Location' # content types maybe as symbol?
31
31
 
32
32
  type2.update(name: 'whoat', fields: [field, field2])
33
33
 
@@ -15,7 +15,6 @@ my_space.update(name: 'MyNewSpace')
15
15
 
16
16
  dat_space = Contentful::Management::Space.find(my_space.id)
17
17
 
18
-
19
18
  dat_space.locales.create(name: 'English', code: 'en-US')
20
19
 
21
20
  locales = dat_space.locales.all
@@ -8,7 +8,6 @@ module Contentful
8
8
  # Resource class for Asset.
9
9
  # https://www.contentful.com/developers/documentation/content-management-api/#resources-assets
10
10
  class Asset
11
-
12
11
  include Contentful::Management::Resource
13
12
  extend Contentful::Management::Resource::AssetFields
14
13
  include Contentful::Management::Resource::Fields
@@ -47,7 +46,7 @@ module Contentful
47
46
  asset.description = attributes[:description] if attributes[:description]
48
47
  asset.file = attributes[:file] if attributes[:file]
49
48
 
50
- request = Request.new("/#{ space_id }/assets/#{ attributes[:id] || ''}", {fields: asset.fields_for_query})
49
+ request = Request.new("/#{ space_id }/assets/#{ attributes[:id] || ''}", fields: asset.fields_for_query)
51
50
  response = attributes[:id].nil? ? request.post : request.put
52
51
  result = ResourceBuilder.new(response, {}, {}).run
53
52
  result.locale = locale if locale
@@ -81,7 +80,7 @@ module Contentful
81
80
  # See README for details.
82
81
  def save
83
82
  if id.nil?
84
- new_instance = self.class.create(sys[:space].id, {fields: instance_variable_get(:@fields)})
83
+ new_instance = self.class.create(sys[:space].id, fields: instance_variable_get(:@fields))
85
84
  refresh_data(new_instance)
86
85
  else
87
86
  update(title: title, description: description, file: file)
@@ -172,11 +171,11 @@ module Contentful
172
171
  # See https://www.contentful.com/developers/documentation/content-delivery-api/#image-asset-resizing
173
172
  def image_url(options = {})
174
173
  query = {
175
- w: options[:w] || options[:width],
176
- h: options[:h] || options[:height],
177
- fm: options[:fm] || options[:format],
178
- q: options[:q] || options[:quality]
179
- }.reject { |k, v| v.nil? }
174
+ w: options[:w] || options[:width],
175
+ h: options[:h] || options[:height],
176
+ fm: options[:fm] || options[:format],
177
+ q: options[:q] || options[:quality]
178
+ }.reject { |_k, v| v.nil? }
180
179
 
181
180
  if query.empty?
182
181
  file.url
@@ -19,11 +19,11 @@ module Contentful
19
19
  attr_accessor :organization_id, :version, :zero_length, :content_type_id, :dynamic_entry_cache
20
20
 
21
21
  DEFAULT_CONFIGURATION = {
22
- api_url: 'api.contentful.com',
23
- api_version: '1',
24
- secure: true,
25
- default_locale: 'en-US',
26
- gzip_encoded: false
22
+ api_url: 'api.contentful.com',
23
+ api_version: '1',
24
+ secure: true,
25
+ default_locale: 'en-US',
26
+ gzip_encoded: false
27
27
  }
28
28
 
29
29
  def initialize(access_token = nil, configuration = {})
@@ -49,8 +49,8 @@ module Contentful
49
49
  @dynamic_entry_cache = Hash[
50
50
  content_types.map do |ct|
51
51
  [
52
- ct.id.to_sym,
53
- DynamicEntry.create(ct)
52
+ ct.id.to_sym,
53
+ DynamicEntry.create(ct)
54
54
  ]
55
55
  end
56
56
  ]
@@ -9,16 +9,16 @@ module Contentful
9
9
  # https://www.contentful.com/developers/documentation/content-management-api/#resources-content-types
10
10
  class ContentType
11
11
  FIELD_TYPES = [
12
- SYMBOL = 'Symbol',
13
- TEXT = 'Text',
14
- INTEGER = 'Integer',
15
- FLOAT = 'Number',
16
- DATE = 'Date',
17
- BOOLEAN = 'Boolean',
18
- LINK = 'Link',
19
- ARRAY = 'Array',
20
- OBJECT = 'Object',
21
- LOCATION = 'Location'
12
+ SYMBOL = 'Symbol',
13
+ TEXT = 'Text',
14
+ INTEGER = 'Integer',
15
+ FLOAT = 'Number',
16
+ DATE = 'Date',
17
+ BOOLEAN = 'Boolean',
18
+ LINK = 'Link',
19
+ ARRAY = 'Array',
20
+ OBJECT = 'Object',
21
+ LOCATION = 'Location'
22
22
  ]
23
23
 
24
24
  include Contentful::Management::Resource
@@ -96,9 +96,9 @@ module Contentful
96
96
  # Returns a Contentful::Management::ContentType.
97
97
  def self.create(space_id, attributes)
98
98
  fields = fields_to_nested_properties_hash(attributes[:fields] || [])
99
- request = Request.new("/#{ space_id }/content_types/#{ attributes[:id] || ''}", {name: attributes.fetch(:name),
100
- description: attributes[:description],
101
- fields: fields})
99
+ request = Request.new("/#{ space_id }/content_types/#{ attributes[:id] || ''}", name: attributes.fetch(:name),
100
+ description: attributes[:description],
101
+ fields: fields)
102
102
  response = attributes[:id].nil? ? request.post : request.put
103
103
  result = ResourceBuilder.new(response, {}, {}).run
104
104
  client.register_dynamic_entry(result.id, DynamicEntry.create(result)) if result.is_a?(self.class)
@@ -1,7 +1,6 @@
1
1
  module Contentful
2
2
  module Management
3
3
  class ContentTypeEntryMethodsFactory
4
-
5
4
  attr_reader :content_type
6
5
 
7
6
  def initialize(content_type)
@@ -22,7 +21,6 @@ module Contentful
22
21
  dynamic_entry.content_type = content_type
23
22
  dynamic_entry
24
23
  end
25
-
26
24
  end
27
25
  end
28
- end
26
+ end
@@ -7,14 +7,14 @@ module Contentful
7
7
  module Management
8
8
  class DynamicEntry < Contentful::Management::Entry
9
9
  KNOWN_TYPES = {
10
- 'String' => :string,
11
- 'Text' => :string,
12
- 'Symbol' => :string,
13
- 'Integer' => :integer,
14
- 'Float' => :float,
15
- 'Boolean' => :boolean,
16
- 'Date' => :date,
17
- 'Location' => Location
10
+ 'String' => :string,
11
+ 'Text' => :string,
12
+ 'Symbol' => :string,
13
+ 'Integer' => :integer,
14
+ 'Float' => :float,
15
+ 'Boolean' => :boolean,
16
+ 'Date' => :date,
17
+ 'Location' => Location
18
18
  }
19
19
 
20
20
  def self.create(content_type)
@@ -65,7 +65,7 @@ module Contentful
65
65
  def update(attributes)
66
66
  fields_for_update = Contentful::Management::Support.deep_hash_merge(fields_for_query, fields_from_attributes(attributes))
67
67
 
68
- request = Request.new("/#{ space.id }/entries/#{ self.id }", {fields: fields_for_update}, id = nil, version: sys[:version])
68
+ request = Request.new("/#{ space.id }/entries/#{ id }", {fields: fields_for_update}, id = nil, version: sys[:version])
69
69
  response = request.put
70
70
  result = ResourceBuilder.new(response, {}, {}).run
71
71
  refresh_data(result)
@@ -75,7 +75,7 @@ module Contentful
75
75
  # See README for details.
76
76
  def save
77
77
  if id.nil?
78
- new_instance = Contentful::Management::Entry.create(content_type, {fields: instance_variable_get(:@fields)})
78
+ new_instance = Contentful::Management::Entry.create(content_type, fields: instance_variable_get(:@fields))
79
79
  refresh_data(new_instance)
80
80
  else
81
81
  update({})
@@ -151,11 +151,10 @@ module Contentful
151
151
  # Parser for assets attributes from query.
152
152
  # Returns a hash of existing fields.
153
153
  def fields_for_query
154
- raw_fields = self.instance_variable_get(:@fields)
154
+ raw_fields = instance_variable_get(:@fields)
155
155
  fields_names = raw_fields.first[1].keys
156
156
  fields_names.each_with_object({}) do |field_name, results|
157
157
  results[field_name] = raw_fields.each_with_object({}) do |(locale, fields), field_results|
158
- # field_results[locale] = fields[field_name]
159
158
  field_results[locale] = parse_update_attribute(fields[field_name]) unless fields[field_name].nil?
160
159
  end
161
160
  end
@@ -176,13 +175,12 @@ module Contentful
176
175
  when ContentType::ARRAY then
177
176
  parse_fields_array(attribute)
178
177
  when ContentType::LOCATION then
179
- {lat: attribute.properties[:lat], lon: attribute.properties[:lon]}
178
+ {lat: attribute.properties[:lat], lon: attribute.properties[:lon]} if attribute
180
179
  else
181
180
  attribute
182
181
  end
183
182
  end
184
183
 
185
- # TODO refactor
186
184
  def parse_update_attribute(attribute)
187
185
  if attribute.is_a? Asset
188
186
  {sys: {type: 'Link', linkType: 'Asset', id: attribute.id}}
@@ -191,41 +189,29 @@ module Contentful
191
189
  elsif attribute.is_a? Location
192
190
  {lat: attribute.properties[:lat], lon: attribute.properties[:lon]}
193
191
  elsif attribute.is_a? ::Array
194
- parse_update_fields_array(attribute)
192
+ self.class.parse_fields_array(attribute)
195
193
  else
196
194
  attribute
197
195
  end
198
196
  end
199
197
 
200
- def parse_update_fields_array(attributes)
198
+ def self.parse_fields_array(attributes)
201
199
  type = attributes.first.class.to_s
202
200
  if type == 'String'
203
201
  attributes
204
202
  else
205
- attributes.each_with_object([]) do |attr, arr|
206
- arr << case type
207
- when /Entry/ then
208
- {sys: {type: 'Link', linkType: 'Entry', id: attr.id}}
209
- when /Asset/ then
210
- {sys: {type: 'Link', linkType: 'Asset', id: attr.id}}
211
- end
212
- end
203
+ parse_objects_array(attributes, type)
213
204
  end
214
205
  end
215
206
 
216
- def self.parse_fields_array(attributes)
217
- if attributes.is_a? ::Array
218
- type = attributes.first.class
219
- attributes.each_with_object([]) do |attr, arr|
220
- arr << case type.to_s
221
- when /Entry/ then
222
- {sys: {type: 'Link', linkType: 'Entry', id: attr.id}}
223
- when /Asset/ then
224
- {sys: {type: 'Link', linkType: 'Asset', id: attr.id}}
225
- end
226
- end
227
- else
228
- [attributes]
207
+ def self.parse_objects_array(attributes, type)
208
+ attributes.each_with_object([]) do |attr, arr|
209
+ arr << case type
210
+ when /Entry/ then
211
+ {sys: {type: 'Link', linkType: 'Entry', id: attr.id}}
212
+ when /Asset/ then
213
+ {sys: {type: 'Link', linkType: 'Asset', id: attr.id}}
214
+ end
229
215
  end
230
216
  end
231
217
 
@@ -17,7 +17,7 @@ module Contentful
17
17
 
18
18
  def deep_merge!(field)
19
19
  properties.merge!(field.properties.select { |name, _type| name != :items })
20
- items.properties.merge!(field.items.properties) if (items.respond_to?(:properties) && field.items.respond_to?(:properties))
20
+ items.properties.merge!(field.items.properties) if items.respond_to?(:properties) && field.items.respond_to?(:properties)
21
21
  end
22
22
 
23
23
  def properties_to_hash
@@ -18,7 +18,7 @@ module Contentful
18
18
  # Gets a collection of locales.
19
19
  # Takes an id of a space.
20
20
  # Returns a Contentful::Management::Array of Contentful::Management::Locale.
21
- def self.all(space_id = nil, parameters = {})
21
+ def self.all(space_id = nil, _parameters = {})
22
22
  request = Request.new("/#{ space_id }/locales")
23
23
  response = request.get
24
24
  result = ResourceBuilder.new(response, {'Locale' => Locale}, {})
@@ -44,7 +44,7 @@ module Contentful
44
44
  # :publish
45
45
  # Returns a Contentful::Management::Locale.
46
46
  def self.create(space_id, attributes)
47
- request = Request.new("/#{ space_id }/locales", {'name' => attributes.fetch(:name), 'code' => attributes.fetch(:code)})
47
+ request = Request.new("/#{ space_id }/locales", 'name' => attributes.fetch(:name), 'code' => attributes.fetch(:code))
48
48
  response = request.post
49
49
  result = ResourceBuilder.new(response, {'Locale' => Locale}, {})
50
50
  result.run
@@ -71,9 +71,9 @@ module Contentful
71
71
  Hash[
72
72
  query.map do |key, value|
73
73
  [
74
- key.to_sym,
75
- value
76
- # value.is_a?(::Array) ? value.join(',') : value
74
+ key.to_sym,
75
+ value
76
+ # value.is_a?(::Array) ? value.join(',') : value
77
77
  ]
78
78
  end
79
79
  ]
@@ -15,11 +15,11 @@ module Contentful
15
15
  # by the client by default
16
16
  module Resource
17
17
  COERCIONS = {
18
- string: ->(value) { value.to_s },
19
- integer: ->(value) { value.to_i },
20
- float: ->(value) { value.to_f },
21
- boolean: ->(value) { !!value },
22
- date: ->(value) { !value.nil? ? DateTime.parse(value) : nil }
18
+ string: ->(value) { value.to_s },
19
+ integer: ->(value) { value.to_i },
20
+ float: ->(value) { value.to_f },
21
+ boolean: ->(value) { !!value },
22
+ date: ->(value) { !value.nil? ? DateTime.parse(value) : nil }
23
23
  }
24
24
 
25
25
  attr_reader :properties, :request, :client, :default_locale
@@ -17,7 +17,7 @@ module Contentful
17
17
  items.each(&block)
18
18
  end
19
19
 
20
- alias each each_item
20
+ alias_method :each, :each_item
21
21
 
22
22
  # Delegates to items#empty?
23
23
  def empty?
@@ -29,7 +29,7 @@ module Contentful
29
29
  items.size
30
30
  end
31
31
 
32
- alias length size
32
+ alias_method :length, :size
33
33
  end
34
34
  end
35
35
  end
@@ -5,9 +5,9 @@ module Contentful
5
5
  # Special fields for Asset.
6
6
  def fields_coercions
7
7
  {
8
- title: :hash,
9
- description: :hash,
10
- file: Contentful::Management::File
8
+ title: :hash,
9
+ description: :hash,
10
+ file: Contentful::Management::File
11
11
  }
12
12
  end
13
13
  end
@@ -5,7 +5,6 @@ module Contentful
5
5
  module Management
6
6
  module Resource
7
7
  module Fields
8
-
9
8
  def fields(wanted_locale = default_locale)
10
9
  requested_locale = locale || wanted_locale
11
10
  @fields[requested_locale] = {} unless @fields[requested_locale]