contentful-management 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. data/.reek +20 -0
  2. data/.rubocop.yml +25 -1
  3. data/CHANGELOG.md +9 -0
  4. data/README.md +5 -0
  5. data/examples/content_types.rb +1 -3
  6. data/examples/create_space.rb +0 -2
  7. data/lib/contentful/management/asset.rb +25 -4
  8. data/lib/contentful/management/client.rb +1 -2
  9. data/lib/contentful/management/content_type.rb +3 -21
  10. data/lib/contentful/management/content_type_entry_methods_factory.rb +28 -0
  11. data/lib/contentful/management/entry.rb +1 -3
  12. data/lib/contentful/management/error.rb +7 -7
  13. data/lib/contentful/management/file.rb +1 -1
  14. data/lib/contentful/management/locale.rb +7 -7
  15. data/lib/contentful/management/request.rb +1 -2
  16. data/lib/contentful/management/resource.rb +5 -6
  17. data/lib/contentful/management/resource/array_like.rb +0 -1
  18. data/lib/contentful/management/resource/asset_fields.rb +2 -3
  19. data/lib/contentful/management/resource/entry_fields.rb +1 -3
  20. data/lib/contentful/management/resource/fields.rb +1 -1
  21. data/lib/contentful/management/resource/refresher.rb +0 -1
  22. data/lib/contentful/management/resource/system_properties.rb +2 -2
  23. data/lib/contentful/management/resource_builder.rb +9 -9
  24. data/lib/contentful/management/response.rb +5 -5
  25. data/lib/contentful/management/space.rb +18 -76
  26. data/lib/contentful/management/space_asset_methods_factory.rb +11 -0
  27. data/lib/contentful/management/space_association_methods_factory.rb +38 -0
  28. data/lib/contentful/management/space_content_type_methods_factory.rb +11 -0
  29. data/lib/contentful/management/space_entry_methods_factory.rb +19 -0
  30. data/lib/contentful/management/space_locale_methods_factory.rb +15 -0
  31. data/lib/contentful/management/space_webhook_methods_factory.rb +19 -0
  32. data/lib/contentful/management/support.rb +3 -5
  33. data/lib/contentful/management/version.rb +1 -1
  34. data/lib/contentful/management/webhook.rb +77 -0
  35. data/spec/fixtures/vcr_cassettes/asset/process.yml +150 -0
  36. data/spec/fixtures/vcr_cassettes/entry/search_filter/equality_operator.yml +476 -0
  37. data/spec/fixtures/vcr_cassettes/entry/search_filter/exclusion_operator.yml +594 -0
  38. data/spec/fixtures/vcr_cassettes/entry/search_filter/full_search.yml +535 -0
  39. data/spec/fixtures/vcr_cassettes/entry/search_filter/full_search_match_operator.yml +535 -0
  40. data/spec/fixtures/vcr_cassettes/entry/search_filter/including_linked_entries.yml +476 -0
  41. data/spec/fixtures/vcr_cassettes/entry/search_filter/inclusion_operator.yml +535 -0
  42. data/spec/fixtures/vcr_cassettes/entry/search_filter/inequality_operator.yml +698 -0
  43. data/spec/fixtures/vcr_cassettes/entry/search_filter/location_search_near_operator.yml +749 -0
  44. data/spec/fixtures/vcr_cassettes/entry/search_filter/location_search_within_operator.yml +749 -0
  45. data/spec/fixtures/vcr_cassettes/entry/search_filter/matching_array_fields.yml +535 -0
  46. data/spec/fixtures/vcr_cassettes/entry/search_filter/order_sys_createdAt.yml +724 -0
  47. data/spec/fixtures/vcr_cassettes/entry/search_filter/order_sys_updatedAt.yml +724 -0
  48. data/spec/fixtures/vcr_cassettes/entry/search_filter/range_operators_greater_than_or_equal.yml +1169 -0
  49. data/spec/fixtures/vcr_cassettes/entry/search_filter/range_operators_less.yml +524 -0
  50. data/spec/fixtures/vcr_cassettes/entry/search_filter/reverse_order_sys_updatedAt.yml +724 -0
  51. data/spec/fixtures/vcr_cassettes/space/webhook/all.yml +534 -0
  52. data/spec/fixtures/vcr_cassettes/space/webhook/find.yml +492 -0
  53. data/spec/fixtures/vcr_cassettes/webhook/all.yml +133 -0
  54. data/spec/fixtures/vcr_cassettes/webhook/create.yml +89 -0
  55. data/spec/fixtures/vcr_cassettes/webhook/create_with_taken_url.yml +72 -0
  56. data/spec/fixtures/vcr_cassettes/webhook/destroy.yml +142 -0
  57. data/spec/fixtures/vcr_cassettes/webhook/find.yml +91 -0
  58. data/spec/fixtures/vcr_cassettes/webhook/find_not_found.yml +67 -0
  59. data/spec/fixtures/vcr_cassettes/webhook/update.yml +179 -0
  60. data/spec/lib/contentful/management/asset_spec.rb +45 -3
  61. data/spec/lib/contentful/management/content_type_spec.rb +30 -26
  62. data/spec/lib/contentful/management/entry_spec.rb +160 -2
  63. data/spec/lib/contentful/management/locale_spec.rb +1 -1
  64. data/spec/lib/contentful/management/space_spec.rb +21 -1
  65. data/spec/lib/contentful/management/webhook_spec.rb +84 -0
  66. data/spec/support/vcr.rb +1 -1
  67. metadata +64 -4
data/.reek CHANGED
@@ -1,3 +1,23 @@
1
1
  ---
2
2
  IrresponsibleModule:
3
3
  enabled: false
4
+ TooManyStatements:
5
+ exclude:
6
+ - initialize
7
+ - entries
8
+ - assets
9
+ - content_types
10
+ max_statements: 11
11
+ NestedIterators:
12
+ enabled: false
13
+ PrimaDonnaMethod:
14
+ enabled: false
15
+ FeatureEnvy:
16
+ enabled: true
17
+ exclude:
18
+ - initialize
19
+ NilCheck:
20
+ enabled: true
21
+ exclude:
22
+ - save
23
+ - create
data/.rubocop.yml CHANGED
@@ -13,4 +13,28 @@ AllCops:
13
13
  - bin/cma-console
14
14
 
15
15
  LineLength:
16
- Max: 120
16
+ Max: 135
17
+ Style/MethodLength:
18
+ Max: 15
19
+
20
+ Style/SpaceInsideBrackets:
21
+ Enabled: false
22
+
23
+ Style/CyclomaticComplexity:
24
+ Max: 8
25
+
26
+ Style/SpaceInsideHashLiteralBraces:
27
+ EnforcedStyle: no_space
28
+ EnforcedStyleForEmptyBraces: no_space
29
+
30
+ Style/CaseIndentation:
31
+ Enabled: false
32
+
33
+ Lint/UselessAssignment:
34
+ Enabled: false
35
+
36
+ Style/IndentationWidth:
37
+ Enabled: false
38
+
39
+ Style/GuardClause:
40
+ Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ### 0.1.0
2
+ * Support for webhooks
3
+ * add image url to asset
4
+ * Cleaning code
5
+ * Fix: remove implicit processing of assets.
6
+ * Fix: Gem is modifying nil #17
7
+ * Fix: rename asset.process_files to asset.process
8
+
9
+
1
10
  ### 0.0.3
2
11
  * Fix: next_page feature
3
12
  * Fix: create entry with specific locale
data/README.md CHANGED
@@ -244,6 +244,11 @@ asset.file_with_locales= {'en-US' => en_file, 'pl' => pl_file}
244
244
  asset.save
245
245
  ```
246
246
 
247
+ Process an Asset file after create:
248
+ ```ruby
249
+ asset.process_file
250
+ ```
251
+
247
252
  Updating an asset:
248
253
 
249
254
  - default locale
@@ -5,7 +5,6 @@ organization = 'organization id'
5
5
 
6
6
  require 'contentful/management'
7
7
 
8
-
9
8
  client = Contentful::Management::Client.new(access_token)
10
9
  space = Contentful::Management::Space.create(name: 'MySpace', organization_id: organization)
11
10
 
@@ -30,7 +29,6 @@ field2.id = 'wow_content'
30
29
  field2.name = 'Wow Content'
31
30
  field2.type = 'Location' #content types maybe as symbol?
32
31
 
33
-
34
32
  type2.update(name: 'whoat', fields: [field, field2])
35
33
 
36
34
  type2.fields.add(field2)
@@ -41,5 +39,5 @@ type2.deactivate
41
39
 
42
40
  type1.destroy
43
41
 
44
- #clean up afterwards!
42
+ # clean up afterwards!
45
43
  space.destroy
@@ -23,7 +23,6 @@ locales = dat_space.locales.all
23
23
 
24
24
  dat_space.destroy
25
25
 
26
-
27
26
  # XXX: This does not set the organization_id when a space should be created
28
27
  #
29
28
  your_space = Contentful::Management::Space.new
@@ -31,5 +30,4 @@ your_space.organization = organization
31
30
  your_space.name = 'YourSpace'
32
31
  your_space.save
33
32
 
34
-
35
33
  your_space.destroy
@@ -50,13 +50,12 @@ module Contentful
50
50
  request = Request.new("/#{ space_id }/assets/#{ attributes[:id] || ''}", {fields: asset.fields_for_query})
51
51
  response = attributes[:id].nil? ? request.post : request.put
52
52
  result = ResourceBuilder.new(response, {}, {}).run
53
- result.process_files if result.is_a? self
54
53
  result.locale = locale if locale
55
54
  result
56
55
  end
57
56
 
58
- # This method is used only when an asset is created. Processes the uploaded file.
59
- def process_files
57
+ # Processing an Asset file
58
+ def process_file
60
59
  instance_variable_get(:@fields).keys.each do |locale|
61
60
  request = Request.new("/#{ space.id }/assets/#{ id }/files/#{ locale }/process", {}, id = nil, version: sys[:version])
62
61
  request.put
@@ -82,7 +81,7 @@ module Contentful
82
81
  # See README for details.
83
82
  def save
84
83
  if id.nil?
85
- new_instance = self.class.create(self.sys[:space].id, {fields: instance_variable_get(:@fields)})
84
+ new_instance = self.class.create(sys[:space].id, {fields: instance_variable_get(:@fields)})
86
85
  refresh_data(new_instance)
87
86
  else
88
87
  update(title: title, description: description, file: file)
@@ -163,6 +162,28 @@ module Contentful
163
162
  end
164
163
  end
165
164
  end
165
+
166
+ # Returns the image url of an asset
167
+ # Allows you to pass in the following options for image resizing:
168
+ # :width
169
+ # :height
170
+ # :format
171
+ # :quality
172
+ # See https://www.contentful.com/developers/documentation/content-delivery-api/#image-asset-resizing
173
+ def image_url(options = {})
174
+ 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? }
180
+
181
+ if query.empty?
182
+ file.url
183
+ else
184
+ "#{file.url}?#{URI.encode_www_form(query)}"
185
+ end
186
+ end
166
187
  end
167
188
  end
168
189
  end
@@ -22,7 +22,7 @@ module Contentful
22
22
  api_url: 'api.contentful.com',
23
23
  api_version: '1',
24
24
  secure: true,
25
- default_locale: 'en-US',
25
+ default_locale: 'en-US'
26
26
  }
27
27
 
28
28
  def initialize(access_token = nil, configuration = {})
@@ -32,7 +32,6 @@ module Contentful
32
32
  Thread.current[:client] = self
33
33
  end
34
34
 
35
-
36
35
  def update_dynamic_entry_cache_for_spaces!(spaces)
37
36
  spaces.each do |space|
38
37
  update_dynamic_entry_cache_for_space!(space)
@@ -1,6 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  require_relative 'resource'
3
3
  require_relative 'field'
4
+ require_relative 'content_type_entry_methods_factory'
4
5
 
5
6
  module Contentful
6
7
  module Management
@@ -184,26 +185,7 @@ module Contentful
184
185
  # Returns a Contentful::Management::Entry.
185
186
  # See README for details.
186
187
  def entries
187
- entries = nil
188
-
189
- entries.instance_exec(self) do |content_type|
190
-
191
- define_singleton_method(:all) do
192
- Contentful::Management::Entry.all(content_type.space.id, content_type: content_type.id)
193
- end
194
-
195
- define_singleton_method(:create) do |params|
196
- Entry.create(content_type, params)
197
- end
198
-
199
- define_singleton_method(:new) do
200
- dynamic_entry_class = content_type.client.register_dynamic_entry(content_type.id, DynamicEntry.create(content_type))
201
- dynamic_entry = dynamic_entry_class.new
202
- dynamic_entry.content_type = content_type
203
- dynamic_entry
204
- end
205
- end
206
- entries
188
+ Contentful::Management::ContentTypeEntryMethodsFactory.new(self)
207
189
  end
208
190
 
209
191
  private
@@ -215,4 +197,4 @@ module Contentful
215
197
  end
216
198
  end
217
199
  end
218
- end
200
+ end
@@ -0,0 +1,28 @@
1
+ module Contentful
2
+ module Management
3
+ class ContentTypeEntryMethodsFactory
4
+
5
+ attr_reader :content_type
6
+
7
+ def initialize(content_type)
8
+ @content_type = content_type
9
+ end
10
+
11
+ def all(params = {})
12
+ Entry.all(content_type.space.id, params.merge(content_type: content_type.id))
13
+ end
14
+
15
+ def create(attributes)
16
+ Entry.create(content_type, attributes)
17
+ end
18
+
19
+ def new
20
+ dynamic_entry_class = content_type.client.register_dynamic_entry(content_type.id, DynamicEntry.create(content_type))
21
+ dynamic_entry = dynamic_entry_class.new
22
+ dynamic_entry.content_type = content_type
23
+ dynamic_entry
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -8,7 +8,6 @@ module Contentful
8
8
  # Resource class for Entry.
9
9
  # https://www.contentful.com/developers/documentation/content-management-api/#resources-entries
10
10
  class Entry
11
-
12
11
  include Contentful::Management::Resource
13
12
  include Contentful::Management::Resource::SystemProperties
14
13
  include Contentful::Management::Resource::Refresher
@@ -43,7 +42,7 @@ module Contentful
43
42
  def self.create(content_type, attributes)
44
43
  custom_id = attributes[:id] || ''
45
44
  locale = attributes[:locale]
46
- fields_for_create = if attributes[:fields] #create from initialized dynamic entry via save
45
+ fields_for_create = if attributes[:fields] # create from initialized dynamic entry via save
47
46
  tmp_entry = new
48
47
  tmp_entry.instance_variable_set(:@fields, attributes.delete(:fields) || {})
49
48
  Contentful::Management::Support.deep_hash_merge(tmp_entry.fields_for_query, tmp_entry.fields_from_attributes(attributes))
@@ -52,7 +51,6 @@ module Contentful
52
51
  end
53
52
 
54
53
  request = Request.new("/#{ content_type.sys[:space].id }/entries/#{ custom_id }", {fields: fields_for_create}, nil, content_type_id: content_type.id)
55
-
56
54
  response = custom_id.empty? ? request.post : request.put
57
55
  result = ResourceBuilder.new(response, {}, {})
58
56
  client.register_dynamic_entry(content_type.id, DynamicEntry.create(content_type))
@@ -32,31 +32,31 @@ module Contentful
32
32
  end
33
33
 
34
34
  # 404
35
- class NotFound < Error;
35
+ class NotFound < Error
36
36
  end
37
37
 
38
38
  # 400
39
- class BadRequest < Error;
39
+ class BadRequest < Error
40
40
  end
41
41
 
42
42
  # 403
43
- class AccessDenied < Error;
43
+ class AccessDenied < Error
44
44
  end
45
45
 
46
46
  # 401
47
- class Unauthorized < Error;
47
+ class Unauthorized < Error
48
48
  end
49
49
 
50
50
  # 500
51
- class ServerError < Error;
51
+ class ServerError < Error
52
52
  end
53
53
 
54
54
  # Raised when response is no valid json
55
- class UnparsableJson < Error;
55
+ class UnparsableJson < Error
56
56
  end
57
57
 
58
58
  # Raised when response is not parsable as a Contentful::Resource
59
- class UnparsableResource < Error;
59
+ class UnparsableResource < Error
60
60
  end
61
61
  end
62
62
  end
@@ -3,7 +3,7 @@ require_relative 'resource'
3
3
 
4
4
  module Contentful
5
5
  module Management
6
- # An Asset's file schema
6
+ # An Asset's file schema
7
7
  class File
8
8
  include Contentful::Management::Resource
9
9
 
@@ -18,10 +18,10 @@ 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)
21
+ def self.all(space_id = nil, parameters = {})
22
22
  request = Request.new("/#{ space_id }/locales")
23
23
  response = request.get
24
- result = ResourceBuilder.new(response, { 'Locale' => Locale }, {})
24
+ result = ResourceBuilder.new(response, {'Locale' => Locale}, {})
25
25
  result.run
26
26
  end
27
27
 
@@ -31,7 +31,7 @@ module Contentful
31
31
  def self.find(space_id, locale_id)
32
32
  request = Request.new("/#{ space_id }/locales/#{ locale_id }")
33
33
  response = request.get
34
- result = ResourceBuilder.new(response, { 'Locale' => Locale }, {})
34
+ result = ResourceBuilder.new(response, {'Locale' => Locale}, {})
35
35
  result.run
36
36
  end
37
37
 
@@ -44,9 +44,9 @@ 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
- result = ResourceBuilder.new(response, { 'Locale' => Locale }, {})
49
+ result = ResourceBuilder.new(response, {'Locale' => Locale}, {})
50
50
  result.run
51
51
  end
52
52
 
@@ -54,9 +54,9 @@ module Contentful
54
54
  # Takes a hash with attributes.
55
55
  # Returns a Contentful::Management::Locale.
56
56
  def update(attributes)
57
- request = Request.new("/#{ space.id }/locales/#{ id }", { 'name' => attributes.fetch(:name) }, id = nil, version: sys[:version])
57
+ request = Request.new("/#{ space.id }/locales/#{ id }", {'name' => attributes.fetch(:name)}, id = nil, version: sys[:version])
58
58
  response = request.put
59
- result = ResourceBuilder.new(response, { 'Locale' => Locale }, {})
59
+ result = ResourceBuilder.new(response, {'Locale' => Locale}, {})
60
60
  refresh_data(result.run)
61
61
  end
62
62
  end
@@ -65,7 +65,6 @@ module Contentful
65
65
  self.class.new(@endpoint, @query, @initial_id, @header)
66
66
  end
67
67
 
68
-
69
68
  private
70
69
 
71
70
  def normalize_query(query)
@@ -74,7 +73,7 @@ module Contentful
74
73
  [
75
74
  key.to_sym,
76
75
  value
77
- # value.is_a?(::Array) ? value.join(',') : value
76
+ # value.is_a?(::Array) ? value.join(',') : value
78
77
  ]
79
78
  end
80
79
  ]
@@ -15,11 +15,11 @@ module Contentful
15
15
  # by the client by default
16
16
  module Resource
17
17
  COERCIONS = {
18
- string: ->(v) { v.to_s },
19
- integer: ->(v) { v.to_i },
20
- float: ->(v) { v.to_f },
21
- boolean: ->(v) { !!v },
22
- date: ->(v) { !v.nil? ? DateTime.parse(v) : 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
@@ -160,7 +160,6 @@ module Contentful
160
160
  def self.included(base)
161
161
  base.extend(ClassMethods)
162
162
  end
163
-
164
163
  end
165
164
  end
166
165
  end
@@ -30,7 +30,6 @@ module Contentful
30
30
  end
31
31
 
32
32
  alias length size
33
-
34
33
  end
35
34
  end
36
35
  end
@@ -2,7 +2,7 @@ module Contentful
2
2
  module Management
3
3
  module Resource
4
4
  module AssetFields
5
-
5
+ # Special fields for Asset.
6
6
  def fields_coercions
7
7
  {
8
8
  title: :hash,
@@ -10,8 +10,7 @@ module Contentful
10
10
  file: Contentful::Management::File
11
11
  }
12
12
  end
13
-
14
13
  end
15
14
  end
16
15
  end
17
- end
16
+ end