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
@@ -7,7 +7,7 @@ module Contentful
7
7
  # Updates the current version of the object to the version on the system
8
8
  def reload
9
9
  self_class = self.class
10
- resource = self.is_a?(Space) ? self_class.find(id) : self_class.find(space.id, id)
10
+ resource = self.is_a?(Space) ? self_class.find(client, id) : self_class.find(client, space.id, id)
11
11
  refresh_data(resource) if resource.is_a? self_class
12
12
  end
13
13
 
@@ -8,6 +8,8 @@ require_relative 'space'
8
8
  require_relative 'content_type'
9
9
  require_relative 'asset'
10
10
  require_relative 'webhook'
11
+ require_relative 'api_key'
12
+ require_relative 'locale'
11
13
 
12
14
  module Contentful
13
15
  module Management
@@ -23,7 +25,9 @@ module Contentful
23
25
  'Asset' => Contentful::Management::Asset,
24
26
  'Array' => :array_or_sync_page,
25
27
  'Link' => Contentful::Management::Link,
26
- 'WebhookDefinition' => Contentful::Management::Webhook
28
+ 'WebhookDefinition' => Contentful::Management::Webhook,
29
+ 'ApiKey' => Contentful::Management::ApiKey,
30
+ 'Locale' => Contentful::Management::Locale
27
31
  }
28
32
  # Default Entry Mapping
29
33
  # @see _ README for more information on Entry Mapping
@@ -31,13 +35,13 @@ module Contentful
31
35
 
32
36
  attr_reader :client, :response, :resource_mapping, :entry_mapping, :resource
33
37
 
34
- def initialize(response, resource_mapping = {}, entry_mapping = {})
38
+ def initialize(response, client, resource_mapping = {}, entry_mapping = {})
35
39
  @response = response
36
- @client = Contentful::Management::Client.shared_instance
40
+ @client = client
37
41
  @included_resources = {}
38
42
  @known_resources = Hash.new { |hash, key| hash[key] = {} }
39
43
  @nested_locales = true
40
- @default_locale = (client.configuration || Contentful::Client::DEFAULT_CONFIGURATION)[:default_locale]
44
+ @default_locale = (client.configuration || Contentful::Management::Client::DEFAULT_CONFIGURATION)[:default_locale]
41
45
  @resource_mapping = default_resource_mapping.merge(resource_mapping)
42
46
  @entry_mapping = default_entry_mapping.merge(entry_mapping)
43
47
  end
@@ -0,0 +1,96 @@
1
+ module Contentful
2
+ module Management
3
+ # Generic Resource Request Class
4
+ # @private
5
+ class ResourceRequester
6
+ attr_reader :client, :resource_class
7
+
8
+ def initialize(client, resource_class)
9
+ @client = client
10
+ @resource_class = resource_class
11
+ end
12
+
13
+ def all(endpoint_options = {}, query = {})
14
+ get(endpoint_options, query)
15
+ end
16
+
17
+ def find(endpoint_options = {})
18
+ get(endpoint_options)
19
+ end
20
+
21
+ def create(endpoint_options = {}, attributes = {})
22
+ custom_id = attributes[:id]
23
+ request = Request.new(
24
+ client,
25
+ resource_class.build_endpoint(endpoint_options),
26
+ resource_class.create_attributes(client, attributes),
27
+ nil,
28
+ resource_class.create_headers(client, attributes)
29
+ )
30
+ response = custom_id.nil? ? request.post : request.put
31
+ resource = ResourceBuilder.new(response, client).run
32
+ resource.after_create(attributes) if resource_class?(resource)
33
+ resource
34
+ end
35
+
36
+ def update(object, endpoint_options = {}, attributes = {}, headers = {})
37
+ object.refresh_data(put(endpoint_options, attributes, headers))
38
+ end
39
+
40
+ def destroy(endpoint_options = {})
41
+ delete(endpoint_options)
42
+ end
43
+
44
+ def archive(object, endpoint_options = {}, headers = {})
45
+ update(object, endpoint_options, {}, headers)
46
+ end
47
+ alias_method :publish, :archive
48
+
49
+ def unarchive(object, endpoint_options = {}, headers = {})
50
+ object.refresh_data(delete(endpoint_options, {}, headers))
51
+ end
52
+ alias_method :unpublish, :unarchive
53
+
54
+ private
55
+
56
+ def resource_class?(object)
57
+ object.resource?
58
+ rescue
59
+ false
60
+ end
61
+
62
+ def get(endpoint_options = {}, query = {})
63
+ request = Request.new(
64
+ client,
65
+ resource_class.build_endpoint(endpoint_options),
66
+ query
67
+ )
68
+ ResourceBuilder.new(request.get, client).run
69
+ end
70
+
71
+ def put(endpoint_options = {}, attributes = {}, headers = {})
72
+ request = Request.new(
73
+ client,
74
+ resource_class.build_endpoint(endpoint_options),
75
+ attributes,
76
+ nil,
77
+ headers
78
+ )
79
+ ResourceBuilder.new(request.put, client).run
80
+ end
81
+
82
+ def delete(endpoint_options = {}, attributes = {}, headers = {})
83
+ request = Request.new(
84
+ client,
85
+ resource_class.build_endpoint(endpoint_options),
86
+ attributes,
87
+ nil,
88
+ headers
89
+ )
90
+ response = request.delete
91
+ return true if response.status == :no_content
92
+ ResourceBuilder.new(response, client).run
93
+ end
94
+ end
95
+ end
96
+ end
@@ -27,52 +27,58 @@ module Contentful
27
27
 
28
28
  attr_accessor :found_locale
29
29
 
30
- # Gets a collection of spaces.
30
+ # @private
31
+ def self.build_endpoint(endpoint_options)
32
+ return "/#{endpoint_options[:space_id]}" if endpoint_options.key?(:space_id)
33
+ ''
34
+ end
35
+
36
+ # Gets all Spaces
37
+ #
38
+ # @param [Contentful::Management::Client] client
39
+ #
31
40
  # @return [Contentful::Management::Array<Contentful::Management::Space>]
32
- def self.all
33
- request = Request.new('')
34
- response = request.get
35
- result = ResourceBuilder.new(response, {}, {})
36
- spaces = result.run
37
- client.update_dynamic_entry_cache_for_spaces!(spaces)
38
- spaces
41
+ def self.all(client)
42
+ ClientSpaceMethodsFactory.new(client).all
39
43
  end
40
44
 
41
45
  # Gets a specific space.
42
46
  #
47
+ # @param [Contentful::Management::Client] client
43
48
  # @param [String] space_id
44
49
  #
45
50
  # @return [Contentful::Management::Space]
46
- def self.find(space_id)
47
- request = Request.new("/#{space_id}")
48
- response = request.get
49
- result = ResourceBuilder.new(response, {}, {})
50
- space = result.run
51
- client.update_dynamic_entry_cache_for_space!(space) if space.is_a? Space
52
- space
51
+ def self.find(client, space_id)
52
+ ClientSpaceMethodsFactory.new(client).find(space_id)
53
+ end
54
+
55
+ # @private
56
+ def self.create_attributes(client, attributes)
57
+ default_locale = attributes[:default_locale] || client.default_locale
58
+ { 'name' => attributes.fetch(:name), defaultLocale: default_locale }
59
+ end
60
+
61
+ # @private
62
+ def self.create_headers(_client, attributes)
63
+ { organization_id: attributes[:organization_id] }
64
+ end
65
+
66
+ # @private
67
+ def after_create(attributes)
68
+ self.found_locale = attributes[:default_locale] || client.default_locale
53
69
  end
54
70
 
55
71
  # Create a space.
56
72
  #
73
+ # @param [Contentful::Management::Client] client
57
74
  # @param [Hash] attributes
58
75
  # @option attributes [String] :name
59
76
  # @option attributes [String] :default_locale
60
77
  # @option attributes [String] :organization_id Required if user has more than one organization
61
78
  #
62
79
  # @return [Contentful::Management::Space]
63
- def self.create(attributes)
64
- default_locale = attributes[:default_locale] || client.default_locale
65
- request = Request.new(
66
- '',
67
- { 'name' => attributes.fetch(:name), defaultLocale: default_locale },
68
- nil,
69
- organization_id: attributes[:organization_id]
70
- )
71
- response = request.post
72
- result = ResourceBuilder.new(response, {}, {})
73
- space = result.run
74
- space.found_locale = default_locale if space.is_a? Space
75
- space
80
+ def self.create(client, attributes)
81
+ ResourceRequester.new(client, self).create({}, attributes)
76
82
  end
77
83
 
78
84
  # Updates a space.
@@ -83,16 +89,13 @@ module Contentful
83
89
  #
84
90
  # @return [Contentful::Management::Space]
85
91
  def update(attributes)
86
- request = Request.new(
87
- "/#{id}",
92
+ ResourceRequester.new(client, self.class).update(
93
+ self,
94
+ { space_id: id },
88
95
  { 'name' => attributes.fetch(:name) },
89
- nil,
90
96
  version: sys[:version],
91
97
  organization_id: attributes[:organization_id]
92
98
  )
93
- response = request.put
94
- result = ResourceBuilder.new(response, {}, {})
95
- refresh_data(result.run)
96
99
  end
97
100
 
98
101
  # If a space is new, an object gets created in the Contentful, otherwise the existing space gets updated.
@@ -103,7 +106,7 @@ module Contentful
103
106
  if id
104
107
  update(name: name, organization_id: organization)
105
108
  else
106
- new_instance = self.class.create(name: name, organization_id: organization)
109
+ new_instance = self.class.create(client, name: name, organization_id: organization)
107
110
  refresh_data(new_instance)
108
111
  end
109
112
  end
@@ -112,13 +115,7 @@ module Contentful
112
115
  #
113
116
  # @return [true, Contentful::Management::Error] success
114
117
  def destroy
115
- request = Request.new("/#{id}")
116
- response = request.delete
117
- if response.status == :no_content
118
- true
119
- else
120
- ResourceBuilder.new(response, {}, {}).run
121
- end
118
+ ResourceRequester.new(client, self.class).destroy(space_id: id)
122
119
  end
123
120
 
124
121
  # Allows manipulation of content types in context of the current space
@@ -187,7 +184,7 @@ module Contentful
187
184
  #
188
185
  # @return [String]
189
186
  def find_locale
190
- locale = ::Contentful::Management::Locale.all(id).detect(&:default)
187
+ locale = locales.all.detect(&:default)
191
188
  return locale.code unless locale.nil?
192
189
  @default_locale
193
190
  end
@@ -4,7 +4,7 @@ module Contentful
4
4
  # @private
5
5
  module SpaceAssociationAllPublishedMethodsFactory
6
6
  def all_published(params = {})
7
- associated_class.all_published(space.id, params)
7
+ associated_class.all_published(space.client, space.id, params)
8
8
  end
9
9
  end
10
10
  end
@@ -1,3 +1,5 @@
1
+ require_relative 'resource_requester'
2
+
1
3
  module Contentful
2
4
  module Management
3
5
  # Wrapper for Space Association Methods
@@ -10,20 +12,21 @@ module Contentful
10
12
  end
11
13
 
12
14
  def all(params = {})
13
- associated_class.all(space.id, params)
15
+ associated_class.all(space.client, space.id, params)
14
16
  end
15
17
 
16
18
  def find(id)
17
- associated_class.find(space.id, id)
19
+ associated_class.find(space.client, space.id, id)
18
20
  end
19
21
 
20
22
  def create(attributes)
21
- associated_class.create(space.id, attributes)
23
+ associated_class.create(space.client, space.id, attributes)
22
24
  end
23
25
 
24
26
  def new
25
27
  object = associated_class.new
26
28
  object.sys[:space] = space
29
+ object.client = space.client
27
30
  object
28
31
  end
29
32
 
@@ -3,7 +3,7 @@ require_relative 'space_association_all_published_method_factory'
3
3
 
4
4
  module Contentful
5
5
  module Management
6
- # Wrapper for ContentType API for usage from within Space
6
+ # Wrapper for Entry API for usage from within Space
7
7
  # @private
8
8
  class SpaceEntryMethodsFactory
9
9
  include Contentful::Management::SpaceAssociationMethodsFactory
@@ -3,6 +3,6 @@ module Contentful
3
3
  # Management Namespace
4
4
  module Management
5
5
  # Gem Version
6
- VERSION = '0.9.0'
6
+ VERSION = '1.0.0'
7
7
  end
8
8
  end
@@ -12,93 +12,20 @@ module Contentful
12
12
  property :url, :string
13
13
  property :httpBasicUsername, :string
14
14
 
15
- # Gets a collection of webhooks.
16
- #
17
- # @param [String] space_id
18
- # @param [Hash] parameters
19
- # @option parameters [String] 'sys.id'
20
- # @option parameters [String] :url
21
- #
22
- # @return [Contentful::Management::Array<Contentful::Management::Webhook>]
23
- def self.all(space_id, parameters = {})
24
- request = Request.new(
25
- "/#{space_id}/webhook_definitions",
26
- parameters
27
- )
28
- response = request.get
29
- result = ResourceBuilder.new(response, {}, {})
30
- result.run
31
- end
32
-
33
- # Gets a specific webhook.
34
- #
35
- # @param [String] space_id
36
- # @param [String] webhook_id
37
- #
38
- # @return [Contentful::Management::Webhook]
39
- def self.find(space_id, webhook_id)
40
- request = Request.new("/#{space_id}/webhook_definitions/#{webhook_id}")
41
- response = request.get
42
- result = ResourceBuilder.new(response, {}, {})
43
- result.run
44
- end
45
-
46
- # Creates a webhook.
47
- #
48
- # @param [String] space_id
49
- # @param [Hash] attributes
50
- # @option attributes [String] :url
51
- # @option attributes [String] :httpBasicUsername
52
- # @option attributes [String] :httpBasicPassword
53
- #
54
- # @return [Contentful::Management::Webhook]
55
- def self.create(space_id, attributes)
56
- id = attributes[:id]
57
- request = Request.new(
58
- "/#{space_id}/webhook_definitions/#{id}",
59
- endpoint_parameters(attributes)
60
- )
61
- response = id.nil? ? request.post : request.put
62
- ResourceBuilder.new(response, {}, {}).run
15
+ # @private
16
+ def self.endpoint
17
+ 'webhook_definitions'
63
18
  end
64
19
 
65
- # Updates a webhook.
66
- #
67
- # @param [Hash] attributes
68
- # @option attributes [String] :url
69
- # @option attributes [String] :httpBasicUsername
70
- # @option attributes [String] :httpBasicPassword
71
- #
72
- # @return [Contentful::Management::Webhook]
73
- def update(attributes)
74
- request = Request.new(
75
- "/#{space.id}/webhook_definitions/#{id}",
76
- self.class.endpoint_parameters(attributes),
77
- nil,
78
- version: sys[:version]
79
- )
80
- response = request.put
81
- result = ResourceBuilder.new(response, {}, {})
82
- refresh_data(result.run)
20
+ # @private
21
+ def self.create_attributes(_client, attributes)
22
+ attributes.select { |key, _value| [:httpBasicUsername, :httpBasicPassword, :url].include? key }
83
23
  end
84
24
 
85
- # Destroys an webhook.
86
- #
87
- # @return [true, Contentful::Management::Error] success
88
- def destroy
89
- request = Request.new("/#{space.id}/webhook_definitions/#{id}")
90
- response = request.delete
91
- if response.status == :no_content
92
- true
93
- else
94
- result = ResourceBuilder.new(response, {}, {})
95
- result.run
96
- end
97
- end
25
+ protected
98
26
 
99
- # @private
100
- def self.endpoint_parameters(attributes)
101
- attributes.select { |key, _value| [:httpBasicUsername, :httpBasicPassword, :url].include? key }
27
+ def query_attributes(attributes)
28
+ self.class.create_attributes(nil, attributes)
102
29
  end
103
30
  end
104
31
  end
@@ -2,7 +2,7 @@
2
2
  http_interactions:
3
3
  - request:
4
4
  method: post
5
- uri: https://api.contentful.com/spaces/yr5m0jky5hsh/assets/
5
+ uri: https://api.contentful.com/spaces/yr5m0jky5hsh/assets
6
6
  body:
7
7
  encoding: UTF-8
8
8
  string: '{"fields":{"title":{"en-US":"titlebyCreateAPI"},"description":{"en-US":"descByAPI"},"file":{"en-US":{"contentType":"image/jpeg","fileName":"pic1.jpg","upload":"https://upload.wikimedia.org/wikipedia/commons/c/c7/Gasometer_Berlin_Sch%C3%B6neberg_2011.jpg"}}}}'