contentful-management 0.9.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/.rubocop_todo.yml +2 -2
  4. data/CHANGELOG.md +27 -3
  5. data/README.md +34 -7
  6. data/lib/contentful/management/api_key.rb +4 -43
  7. data/lib/contentful/management/array.rb +1 -2
  8. data/lib/contentful/management/asset.rb +29 -191
  9. data/lib/contentful/management/client.rb +96 -7
  10. data/lib/contentful/management/client_api_key_methods_factory.rb +15 -0
  11. data/lib/contentful/management/client_asset_methods_factory.rb +13 -0
  12. data/lib/contentful/management/client_association_all_published_method_factory.rb +21 -0
  13. data/lib/contentful/management/client_association_methods_factory.rb +54 -0
  14. data/lib/contentful/management/client_content_type_methods_factory.rb +13 -0
  15. data/lib/contentful/management/client_entry_methods_factory.rb +21 -0
  16. data/lib/contentful/management/client_locale_methods_factory.rb +15 -0
  17. data/lib/contentful/management/client_space_methods_factory.rb +58 -0
  18. data/lib/contentful/management/client_webhook_methods_factory.rb +15 -0
  19. data/lib/contentful/management/content_type.rb +30 -151
  20. data/lib/contentful/management/content_type_entry_methods_factory.rb +9 -3
  21. data/lib/contentful/management/dynamic_entry.rb +20 -39
  22. data/lib/contentful/management/entry.rb +47 -182
  23. data/lib/contentful/management/http_client.rb +41 -8
  24. data/lib/contentful/management/locale.rb +7 -76
  25. data/lib/contentful/management/request.rb +3 -3
  26. data/lib/contentful/management/resource.rb +105 -7
  27. data/lib/contentful/management/resource/all_published.rb +24 -0
  28. data/lib/contentful/management/resource/archiver.rb +37 -0
  29. data/lib/contentful/management/resource/field_aware.rb +49 -29
  30. data/lib/contentful/management/resource/publisher.rb +37 -0
  31. data/lib/contentful/management/resource/refresher.rb +1 -1
  32. data/lib/contentful/management/resource_builder.rb +8 -4
  33. data/lib/contentful/management/resource_requester.rb +96 -0
  34. data/lib/contentful/management/space.rb +40 -43
  35. data/lib/contentful/management/space_association_all_published_method_factory.rb +1 -1
  36. data/lib/contentful/management/space_association_methods_factory.rb +6 -3
  37. data/lib/contentful/management/space_entry_methods_factory.rb +1 -1
  38. data/lib/contentful/management/version.rb +1 -1
  39. data/lib/contentful/management/webhook.rb +9 -82
  40. data/spec/fixtures/vcr_cassettes/asset/create.yml +1 -1
  41. data/spec/fixtures/vcr_cassettes/asset/create_with_locale.yml +1 -1
  42. data/spec/fixtures/vcr_cassettes/asset/process.yml +1 -1
  43. data/spec/fixtures/vcr_cassettes/asset/publish_after_create.yml +1 -1
  44. data/spec/fixtures/vcr_cassettes/content_type/create.yml +1 -1
  45. data/spec/fixtures/vcr_cassettes/content_type/create_with_Array_field.yml +1 -1
  46. data/spec/fixtures/vcr_cassettes/content_type/create_with_Boolean_field.yml +1 -1
  47. data/spec/fixtures/vcr_cassettes/content_type/create_with_Date_field.yml +1 -1
  48. data/spec/fixtures/vcr_cassettes/content_type/create_with_Integer_field.yml +1 -1
  49. data/spec/fixtures/vcr_cassettes/content_type/create_with_Link_field.yml +1 -1
  50. data/spec/fixtures/vcr_cassettes/content_type/create_with_Location_field.yml +1 -1
  51. data/spec/fixtures/vcr_cassettes/content_type/create_with_Number_field.yml +1 -1
  52. data/spec/fixtures/vcr_cassettes/content_type/create_with_Object_field.yml +1 -1
  53. data/spec/fixtures/vcr_cassettes/content_type/create_with_Symbol_field.yml +1 -1
  54. data/spec/fixtures/vcr_cassettes/content_type/create_with_Text_field.yml +1 -1
  55. data/spec/fixtures/vcr_cassettes/content_type/entry/create.yml +1 -1
  56. data/spec/fixtures/vcr_cassettes/content_type/entry/create_only_with_localized_fields.yml +1 -1
  57. data/spec/fixtures/vcr_cassettes/content_type/entry/create_to_single_locale_only_with_localized_fields.yml +1 -1
  58. data/spec/fixtures/vcr_cassettes/content_type/entry/create_with_camel_case_id_to_multiple_locales.yml +1 -1
  59. data/spec/fixtures/vcr_cassettes/content_type/entry/create_with_entries.yml +1 -1
  60. data/spec/fixtures/vcr_cassettes/content_type/entry/create_with_entries_for_multiple_locales.yml +1 -1
  61. data/spec/fixtures/vcr_cassettes/content_type/entry/create_with_multiple_locales.yml +1 -1
  62. data/spec/fixtures/vcr_cassettes/content_type/fields/create_array_types.yml +1 -1
  63. data/spec/fixtures/vcr_cassettes/content_type/save_new.yml +1 -1
  64. data/spec/fixtures/vcr_cassettes/delete_request.yml +38 -0
  65. data/spec/fixtures/vcr_cassettes/entry/create.yml +1 -1
  66. data/spec/fixtures/vcr_cassettes/entry/create_with_asset.yml +1 -1
  67. data/spec/fixtures/vcr_cassettes/entry/create_with_assets.yml +1 -1
  68. data/spec/fixtures/vcr_cassettes/entry/create_with_entries.yml +1 -1
  69. data/spec/fixtures/vcr_cassettes/entry/create_with_entry.yml +1 -1
  70. data/spec/fixtures/vcr_cassettes/entry/create_with_just_id.yml +1 -1
  71. data/spec/fixtures/vcr_cassettes/entry/create_with_location.yml +1 -1
  72. data/spec/fixtures/vcr_cassettes/entry/create_with_specified_locale.yml +1 -1
  73. data/spec/fixtures/vcr_cassettes/entry/create_with_symbols.yml +1 -1
  74. data/spec/fixtures/vcr_cassettes/entry/too_many_requests.yml +1 -1
  75. data/spec/fixtures/vcr_cassettes/entry/update_bool_field.yml +1 -1
  76. data/spec/fixtures/vcr_cassettes/post_request.yml +64 -0
  77. data/spec/fixtures/vcr_cassettes/proxy_request.yml +562 -0
  78. data/spec/fixtures/vcr_cassettes/put_request.yml +40 -0
  79. data/spec/fixtures/vcr_cassettes/space/asset/create.yml +1 -1
  80. data/spec/fixtures/vcr_cassettes/space/asset/create_with_multiple_locales.yml +1 -1
  81. data/spec/fixtures/vcr_cassettes/space/content_type/create.yml +1 -1
  82. data/spec/fixtures/vcr_cassettes/space/create_with_client_default_locale.yml +197 -36
  83. data/spec/fixtures/vcr_cassettes/space/webhook/create.yml +1 -1
  84. data/spec/lib/contentful/management/api_key_spec.rb +7 -1
  85. data/spec/lib/contentful/management/array_spec.rb +1 -1
  86. data/spec/lib/contentful/management/asset_spec.rb +52 -32
  87. data/spec/lib/contentful/management/client_spec.rb +38 -0
  88. data/spec/lib/contentful/management/content_type_spec.rb +37 -22
  89. data/spec/lib/contentful/management/entry_spec.rb +68 -57
  90. data/spec/lib/contentful/management/locale_spec.rb +7 -1
  91. data/spec/lib/contentful/management/space_spec.rb +9 -3
  92. data/spec/lib/contentful/management/webhook_spec.rb +11 -5
  93. metadata +23 -2
@@ -1,3 +1,5 @@
1
+ require_relative 'resource_requester'
2
+
1
3
  module Contentful
2
4
  module Management
3
5
  # Wrapper for Entry manipulation for a specific Content Type
@@ -21,7 +23,7 @@ module Contentful
21
23
  #
22
24
  # @return [Contentful::Management::Array<Contentful::Management::Entry>]
23
25
  def all(params = {})
24
- Entry.all(content_type.space.id, params.merge(content_type: content_type.id))
26
+ Entry.all(content_type.client, content_type.space.id, params.merge(content_type: content_type.id))
25
27
  end
26
28
 
27
29
  # Creates an entry for a content type.
@@ -30,14 +32,18 @@ module Contentful
30
32
  #
31
33
  # @return [Contentful::Management::Entry]
32
34
  def create(attributes)
33
- Entry.create(content_type, attributes)
35
+ attributes[:content_type] = content_type
36
+ Entry.create(content_type.client, content_type.space.id, attributes)
34
37
  end
35
38
 
36
39
  # Instantiates an empty entry for a content type.
37
40
  #
38
41
  # @return [Contentful::Management::Entry]
39
42
  def new
40
- dynamic_entry_class = content_type.client.register_dynamic_entry(content_type.id, DynamicEntry.create(content_type))
43
+ dynamic_entry_class = content_type.client.register_dynamic_entry(
44
+ content_type.id,
45
+ DynamicEntry.create(content_type, content_type.client)
46
+ )
41
47
  dynamic_entry = dynamic_entry_class.new
42
48
  dynamic_entry.content_type = content_type
43
49
  dynamic_entry
@@ -19,48 +19,22 @@ module Contentful
19
19
  }
20
20
 
21
21
  # @private
22
- def self.create(content_type)
23
- unless content_type.is_a? ContentType
24
- content_type = ContentType.new(content_type)
25
- end
26
-
27
- fields_coercions = Hash[
28
- content_type.fields.map do |field|
29
- [field.id.to_sym, KNOWN_TYPES[field.type]]
22
+ def self.define_singleton_properties(entry_class, content_type, client)
23
+ entry_class.class_eval do
24
+ define_singleton_method :content_type do
25
+ content_type
30
26
  end
31
- ]
32
27
 
33
- Class.new DynamicEntry do
34
- content_type.fields.each do |field|
35
- accessor_name = Support.snakify(field.id)
36
- define_method accessor_name do
37
- fields[field.id.to_sym]
38
- end
39
- define_method "#{accessor_name}_with_locales" do
40
- fields_for_query[field.id.to_sym]
41
- end
42
- define_method "#{accessor_name}=" do |value|
43
- if localized_or_default_locale(field, locale)
44
- @fields[locale] ||= {}
45
- @fields[locale][field.id.to_sym] = value
46
- end
47
- end
48
- define_method "#{accessor_name}_with_locales=" do |values|
49
- values.each do |locale, value|
50
- if localized_or_default_locale(field, locale)
51
- @fields[locale] ||= {}
52
- @fields[locale][field.id.to_sym] = value
53
- end
54
- end
55
- end
28
+ define_singleton_method :client do
29
+ client
56
30
  end
57
31
 
58
32
  define_singleton_method :fields_coercions do
59
- fields_coercions
60
- end
61
-
62
- define_singleton_method :content_type do
63
- content_type
33
+ Hash[
34
+ content_type.fields.map do |field|
35
+ [field.id.to_sym, KNOWN_TYPES[field.type]]
36
+ end
37
+ ]
64
38
  end
65
39
 
66
40
  define_singleton_method :to_s do
@@ -74,8 +48,15 @@ module Contentful
74
48
  end
75
49
 
76
50
  # @private
77
- def localized_or_default_locale(field, locale)
78
- field.localized || default_locale == locale
51
+ def self.create(content_type, client)
52
+ unless content_type.is_a? ContentType
53
+ content_type = ContentType.new(content_type)
54
+ end
55
+
56
+ Class.new DynamicEntry do
57
+ DynamicEntry.define_singleton_properties(self, content_type, client)
58
+ FieldAware.create_fields_for_content_type(self, :class)
59
+ end
79
60
  end
80
61
  end
81
62
  end
@@ -1,7 +1,12 @@
1
1
  require_relative 'resource'
2
+ require_relative 'resource_requester'
3
+ require_relative 'client_entry_methods_factory'
2
4
  require_relative 'resource/entry_fields'
3
5
  require_relative 'resource/fields'
4
6
  require_relative 'resource/field_aware'
7
+ require_relative 'resource/all_published'
8
+ require_relative 'resource/archiver'
9
+ require_relative 'resource/publisher'
5
10
 
6
11
  module Contentful
7
12
  module Management
@@ -12,74 +17,26 @@ module Contentful
12
17
  include Contentful::Management::Resource::SystemProperties
13
18
  include Contentful::Management::Resource::Refresher
14
19
  extend Contentful::Management::Resource::EntryFields
20
+ extend Contentful::Management::Resource::AllPublished
15
21
  include Contentful::Management::Resource::Fields
22
+ include Contentful::Management::Resource::Archiver
23
+ include Contentful::Management::Resource::Publisher
16
24
 
17
25
  attr_accessor :content_type
18
26
 
19
- # Gets a collection of entries.
20
- #
21
- # @param [String] space_id
22
- # @param [Hash] parameters
23
- # @see _ For complete option list: http://docs.contentfulcda.apiary.io/#reference/search-parameters
24
- # @option parameters [String] 'sys.id' Entry ID
25
- # @option parameters [String] :content_type
26
- # @option parameters [Integer] :limit
27
- # @option parameters [Integer] :skip
28
- #
29
- # @return [Contentful::Management::Array<Contentful::Management::Entry>]
30
- def self.all(space_id, parameters = {})
31
- request = Request.new(
32
- "/#{space_id}/entries",
33
- parameters
34
- )
35
- response = request.get
36
- result = ResourceBuilder.new(response, {}, {})
37
- result.run
38
- end
39
-
40
- # Gets a collection of published entries.
41
- #
42
- # @param [String] space_id
43
- # @param [Hash] parameters
44
- # @see _ For complete option list: http://docs.contentfulcda.apiary.io/#reference/search-parameters
45
- # @option parameters [String] 'sys.id' Entry ID
46
- # @option parameters [String] :content_type
47
- # @option parameters [Integer] :limit
48
- # @option parameters [Integer] :skip
49
- #
50
- # @return [Contentful::Management::Array<Contentful::Management::Entry>]
51
- def self.all_published(space_id, parameters = {})
52
- request = Request.new(
53
- "/#{space_id}/public/entries",
54
- parameters
55
- )
56
- response = request.get
57
- result = ResourceBuilder.new(response, {}, {})
58
- result.run
27
+ # @private
28
+ def self.endpoint
29
+ 'entries'
59
30
  end
60
31
 
61
- # Gets a specific entry.
62
- #
63
- # @param [String] space_id
64
- # @param [String] entry_id
65
- #
66
- # @return [Contentful::Management::Entry]
67
- def self.find(space_id, entry_id)
68
- request = Request.new("/#{space_id}/entries/#{entry_id}")
69
- response = request.get
70
- result = ResourceBuilder.new(response, {}, {})
71
- result.run
32
+ # @private
33
+ def self.client_association_class
34
+ ClientEntryMethodsFactory
72
35
  end
73
36
 
74
- # Creates an entry.
75
- #
76
- # @param [Contentful::Management::ContentType] content_type
77
- # @param [Hash] attributes extracted from Content Type fields
78
- #
79
- # @return [Contentful::Management::Entry]
80
- def self.create(content_type, attributes)
81
- custom_id = attributes[:id]
82
- locale = attributes[:locale]
37
+ # @private
38
+ def self.create_attributes(client, attributes)
39
+ content_type = attributes[:content_type]
83
40
  fields_for_create = if attributes[:fields] # create from initialized dynamic entry via save
84
41
  tmp_entry = new
85
42
  tmp_entry.instance_variable_set(:@fields, attributes.delete(:fields) || {})
@@ -88,21 +45,25 @@ module Contentful
88
45
  tmp_entry.fields_from_attributes(attributes)
89
46
  )
90
47
  else
91
- fields_with_locale content_type, attributes
48
+ fields_with_locale content_type, attributes.clone
92
49
  end
93
50
 
94
- request = Request.new(
95
- "/#{content_type.sys[:space].id}/entries/#{custom_id}",
96
- { fields: fields_for_create },
97
- nil,
98
- content_type_id: content_type.id
99
- )
100
- response = custom_id.nil? ? request.post : request.put
101
- result = ResourceBuilder.new(response, {}, {})
102
- client.register_dynamic_entry(content_type.id, DynamicEntry.create(content_type))
103
- entry = result.run
104
- entry.locale = locale if locale
105
- entry
51
+ client.register_dynamic_entry(content_type.id, DynamicEntry.create(content_type, client))
52
+
53
+ { fields: fields_for_create }
54
+ end
55
+
56
+ # @private
57
+ def self.create_headers(_client, attributes)
58
+ content_type = attributes[:content_type]
59
+ content_type_id = content_type.respond_to?(:id) ? content_type.id : content_type[:id]
60
+
61
+ { content_type_id: content_type_id }
62
+ end
63
+
64
+ # @private
65
+ def after_create(attributes)
66
+ self.locale = attributes[:locale] || client.default_locale
106
67
  end
107
68
 
108
69
  # Gets Hash of fields for the current locale
@@ -118,25 +79,6 @@ module Contentful
118
79
  default_fields.merge(@fields[requested_locale])
119
80
  end
120
81
 
121
- # Updates an entry.
122
- #
123
- # @param [Hash] attributes extracted from Content Type fields
124
- #
125
- # @return [Contentful::Management::Entry]
126
- def update(attributes)
127
- fields_for_update = Contentful::Management::Support.deep_hash_merge(fields_for_query, fields_from_attributes(attributes))
128
-
129
- request = Request.new(
130
- "/#{space.id}/entries/#{id}",
131
- { fields: fields_for_update },
132
- nil,
133
- version: sys[:version]
134
- )
135
- response = request.put
136
- result = ResourceBuilder.new(response, {}, {}).run
137
- refresh_data(result)
138
- end
139
-
140
82
  # If an entry is a new object gets created in the Contentful, otherwise the existing entry gets updated.
141
83
  # @see _ README for details.
142
84
  #
@@ -145,99 +87,16 @@ module Contentful
145
87
  if id
146
88
  update({})
147
89
  else
148
- new_instance = Contentful::Management::Entry.create(content_type, fields: instance_variable_get(:@fields))
90
+ new_instance = Contentful::Management::Entry.create(
91
+ client,
92
+ content_type.space.id,
93
+ content_type: content_type,
94
+ fields: instance_variable_get(:@fields)
95
+ )
149
96
  refresh_data(new_instance)
150
97
  end
151
98
  end
152
99
 
153
- # Publishes an entry.
154
- #
155
- # @return [Contentful::Management::Entry]
156
- def publish
157
- request = Request.new(
158
- "/#{space.id}/entries/#{id}/published",
159
- {},
160
- nil,
161
- version: sys[:version]
162
- )
163
- response = request.put
164
- result = ResourceBuilder.new(response, {}, {}).run
165
- refresh_data(result)
166
- end
167
-
168
- # Unpublishes an entry.
169
- #
170
- # @return [Contentful::Management::Entry]
171
- def unpublish
172
- request = Request.new(
173
- "/#{space.id}/entries/#{id}/published",
174
- {},
175
- nil,
176
- version: sys[:version]
177
- )
178
- response = request.delete
179
- result = ResourceBuilder.new(response, {}, {}).run
180
- refresh_data(result)
181
- end
182
-
183
- # Archives an entry.
184
- #
185
- # @return [Contentful::Management::Entry]
186
- def archive
187
- request = Request.new(
188
- "/#{space.id}/entries/#{id}/archived",
189
- {},
190
- nil,
191
- version: sys[:version]
192
- )
193
- response = request.put
194
- result = ResourceBuilder.new(response, {}, {}).run
195
- refresh_data(result)
196
- end
197
-
198
- # Unarchives an entry.
199
- #
200
- # @return [Contentful::Management::Entry]
201
- def unarchive
202
- request = Request.new(
203
- "/#{space.id}/entries/#{id}/archived",
204
- {},
205
- nil,
206
- version: sys[:version]
207
- )
208
- response = request.delete
209
- result = ResourceBuilder.new(response, {}, {}).run
210
- refresh_data(result)
211
- end
212
-
213
- # Destroys an entry.
214
- #
215
- # @return [true, Contentful::Management::Error] success
216
- def destroy
217
- request = Request.new("/#{space.id}/entries/#{id}")
218
- response = request.delete
219
- if response.status == :no_content
220
- return true
221
- else
222
- result = ResourceBuilder.new(response, {}, {})
223
- result.run
224
- end
225
- end
226
-
227
- # Checks if an entry is published.
228
- #
229
- # @return [Boolean]
230
- def published?
231
- sys[:publishedAt] ? true : false
232
- end
233
-
234
- # Checks if an entry is archived.
235
- #
236
- # @return [Boolean]
237
- def archived?
238
- sys[:archivedAt] ? true : false
239
- end
240
-
241
100
  # Returns the currently supported local.
242
101
  #
243
102
  # @return [String] current_locale
@@ -272,6 +131,12 @@ module Contentful
272
131
  end
273
132
  end
274
133
 
134
+ protected
135
+
136
+ def query_attributes(attributes)
137
+ { fields: Contentful::Management::Support.deep_hash_merge(fields_for_query, fields_from_attributes(attributes)) }
138
+ end
139
+
275
140
  private
276
141
 
277
142
  def self.parse_attribute_with_field(attribute, field)
@@ -315,7 +180,7 @@ module Contentful
315
180
  end
316
181
 
317
182
  def fetch_content_type
318
- @content_type ||= ::Contentful::Management::ContentType.find(space.id, sys[:contentType].id)
183
+ @content_type ||= ::Contentful::Management::ContentType.find(client, space.id, sys[:contentType].id)
319
184
  end
320
185
 
321
186
  def self.hash_with_link_object(type, attribute)
@@ -9,8 +9,8 @@ module Contentful
9
9
  # @param [Hash] headers
10
10
  #
11
11
  # @return [HTTP::Response]
12
- def get_http(url, query, headers = {})
13
- HTTP[headers].get(url, params: query)
12
+ def get_http(url, query, headers = {}, proxy = {})
13
+ http_send(:get, url, { params: query }, headers, proxy)
14
14
  end
15
15
 
16
16
  # Post Request
@@ -20,8 +20,8 @@ module Contentful
20
20
  # @param [Hash] headers
21
21
  #
22
22
  # @return [HTTP::Response]
23
- def post_http(url, params, headers = {})
24
- HTTP[headers].post(url, json: params)
23
+ def post_http(url, params, headers = {}, proxy = {})
24
+ http_send(:post, url, { json: params }, headers, proxy)
25
25
  end
26
26
 
27
27
  # Delete Request
@@ -31,8 +31,8 @@ module Contentful
31
31
  # @param [Hash] headers
32
32
  #
33
33
  # @return [HTTP::Response]
34
- def delete_http(url, params, headers = {})
35
- HTTP[headers].delete(url, params: params)
34
+ def delete_http(url, params, headers = {}, proxy = {})
35
+ http_send(:delete, url, { params: params }, headers, proxy)
36
36
  end
37
37
 
38
38
  # Put Request
@@ -42,8 +42,41 @@ module Contentful
42
42
  # @param [Hash] headers
43
43
  #
44
44
  # @return [HTTP::Response]
45
- def put_http(url, params, headers = {})
46
- HTTP[headers].put(url, json: params)
45
+ def put_http(url, params, headers = {}, proxy = {})
46
+ http_send(:put, url, { json: params }, headers, proxy)
47
+ end
48
+
49
+ # Proxy Helper
50
+ #
51
+ # @param [Symbol] type
52
+ # @param [String] url
53
+ # @param [Hash] params
54
+ # @param [Hash] headers
55
+ # @param [Hash] proxy
56
+ #
57
+ # @return [HTTP::Response]
58
+ def proxy_send(type, url, params, headers, proxy)
59
+ HTTP[headers].via(
60
+ proxy[:host],
61
+ proxy[:port],
62
+ proxy[:username],
63
+ proxy[:password]
64
+ ).public_send(type, url, params)
65
+ end
66
+
67
+ # HTTP Helper
68
+ # Abtracts the Proxy/No-Proxy logic
69
+ #
70
+ # @param [Symbol] type
71
+ # @param [String] url
72
+ # @param [Hash] params
73
+ # @param [Hash] headers
74
+ # @param [Hash] proxy
75
+ #
76
+ # @return [HTTP::Response]
77
+ def http_send(type, url, params, headers, proxy)
78
+ return proxy_send(type, url, params, headers, proxy) unless proxy[:host].nil?
79
+ HTTP[headers].public_send(type, url, params)
47
80
  end
48
81
  end
49
82
  end