contentful-management 0.0.2 → 0.0.3

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 (36) hide show
  1. data/CHANGELOG.md +8 -0
  2. data/README.md +17 -0
  3. data/lib/contentful/management/array.rb +3 -1
  4. data/lib/contentful/management/asset.rb +16 -14
  5. data/lib/contentful/management/client.rb +1 -1
  6. data/lib/contentful/management/content_type.rb +18 -18
  7. data/lib/contentful/management/entry.rb +20 -19
  8. data/lib/contentful/management/locale.rb +4 -4
  9. data/lib/contentful/management/request.rb +4 -2
  10. data/lib/contentful/management/resource.rb +8 -8
  11. data/lib/contentful/management/resource/refresher.rb +8 -1
  12. data/lib/contentful/management/resource_builder.rb +3 -3
  13. data/lib/contentful/management/response.rb +10 -3
  14. data/lib/contentful/management/space.rb +11 -11
  15. data/lib/contentful/management/version.rb +1 -1
  16. data/spec/fixtures/vcr_cassettes/asset/create_with_locale.yml +151 -0
  17. data/spec/fixtures/vcr_cassettes/asset/limited_assets_next_page.yml +123 -0
  18. data/spec/fixtures/vcr_cassettes/asset/reload.yml +406 -0
  19. data/spec/fixtures/vcr_cassettes/content_type/reload.yml +394 -0
  20. data/spec/fixtures/vcr_cassettes/entry/all.yml +231 -36
  21. data/spec/fixtures/vcr_cassettes/entry/content_type_entires.yml +99 -23
  22. data/spec/fixtures/vcr_cassettes/entry/create_with_specified_locale.yml +509 -0
  23. data/spec/fixtures/vcr_cassettes/entry/limited_entries.yml +236 -0
  24. data/spec/fixtures/vcr_cassettes/entry/reload.yml +1027 -0
  25. data/spec/fixtures/vcr_cassettes/locale/reload.yml +279 -0
  26. data/spec/fixtures/vcr_cassettes/space/asset/all_with_skip_and_limit.yml +355 -0
  27. data/spec/fixtures/vcr_cassettes/space/asset/with_skipped_and_limited_assets_next_page.yml +635 -0
  28. data/spec/fixtures/vcr_cassettes/space/entry/with_skipped_and_limited_entires_next_page.yml +648 -0
  29. data/spec/fixtures/vcr_cassettes/space/entry/with_skipped_andlimited_entires.yml +468 -0
  30. data/spec/fixtures/vcr_cassettes/space/reload.yml +739 -0
  31. data/spec/lib/contentful/management/asset_spec.rb +42 -2
  32. data/spec/lib/contentful/management/content_type_spec.rb +16 -1
  33. data/spec/lib/contentful/management/entry_spec.rb +38 -5
  34. data/spec/lib/contentful/management/locale_spec.rb +14 -0
  35. data/spec/lib/contentful/management/space_spec.rb +46 -2
  36. metadata +31 -5
@@ -8,6 +8,8 @@ module Contentful
8
8
  attr_reader :client, :type, :query, :id
9
9
 
10
10
  def initialize(endpoint, query = {}, id = nil, header = {})
11
+ @header = header
12
+ @initial_id = id
11
13
  @client = Contentful::Management::Client.shared_instance
12
14
  @client.version = header[:version] if header[:version]
13
15
  @client.organization_id = header[:organization_id] if header[:organization_id]
@@ -60,7 +62,7 @@ module Contentful
60
62
 
61
63
  # Returns a new Request object with the same data
62
64
  def copy
63
- Marshal.load(Marshal.dump(self))
65
+ self.class.new(@endpoint, @query, @initial_id, @header)
64
66
  end
65
67
 
66
68
 
@@ -72,7 +74,7 @@ module Contentful
72
74
  [
73
75
  key.to_sym,
74
76
  value
75
- # TODO why there was this line? value.is_a?(::Array) ? value.join(',') : value
77
+ # value.is_a?(::Array) ? value.join(',') : value
76
78
  ]
77
79
  end
78
80
  ]
@@ -59,14 +59,9 @@ module Contentful
59
59
  nil
60
60
  end
61
61
 
62
- # Issues the request that was made to fetch this response again.
63
- # Only works for top-level resources
64
- def reload
65
- if request
66
- request.get
67
- else
68
- false
69
- end
62
+ # Shared instance of the API client
63
+ def client
64
+ Contentful::Management::Client.shared_instance
70
65
  end
71
66
 
72
67
  private
@@ -115,6 +110,11 @@ module Contentful
115
110
  @property_coercions ||= {}
116
111
  end
117
112
 
113
+ # Shared instance of the API client
114
+ def client
115
+ Contentful::Management::Client.shared_instance
116
+ end
117
+
118
118
  # Defines which properties of a resource your class expects
119
119
  # Define them in :camelCase, they will be available as #snake_cased methods
120
120
  #
@@ -5,13 +5,20 @@ module Contentful
5
5
  # Adds the feature to have properties and system data reload for Resource.
6
6
  module Refresher
7
7
 
8
+ # Reload an object
9
+ # Updates the current version of the object to the version on the system
10
+ def reload
11
+ resource = self.is_a?(Space) ? self.class.find(id) : self.class.find(space.id, id)
12
+ refresh_data(resource) if resource.is_a? self.class
13
+ end
14
+
8
15
  def refresh_data(resource)
9
16
  if resource.is_a? Error
10
17
  resource
11
18
  else
12
19
  @properties = resource.instance_variable_get(:@properties)
13
20
  @fields = resource.instance_variable_get(:@fields) if self.is_a?(Contentful::Management::Entry)
14
- @sys = resource.instance_variable_get(:@sys)
21
+ @sys = resource.instance_variable_get(:@sys).merge(locale: locale)
15
22
  self
16
23
  end
17
24
  end
@@ -26,13 +26,13 @@ module Contentful
26
26
 
27
27
  attr_reader :client, :response, :resource_mapping, :entry_mapping, :resource
28
28
 
29
- def initialize(client, response, resource_mapping = {}, entry_mapping = {})
29
+ def initialize(response, resource_mapping = {}, entry_mapping = {})
30
30
  @response = response
31
- @client = client
31
+ @client = Contentful::Management::Client.shared_instance
32
32
  @included_resources = {}
33
33
  @known_resources = Hash.new { |h, k| h[k] = {} }
34
34
  @nested_locales = true
35
- @default_locale = (Contentful::Management::Client.shared_instance.configuration || Contentful::Client::DEFAULT_CONFIGURATION)[:default_locale]
35
+ @default_locale = (client.configuration || Contentful::Client::DEFAULT_CONFIGURATION)[:default_locale]
36
36
  @resource_mapping = default_resource_mapping.merge(resource_mapping)
37
37
  @entry_mapping = default_entry_mapping.merge(entry_mapping)
38
38
  end
@@ -25,11 +25,14 @@ module Contentful
25
25
  attr_reader :raw, :object, :status, :error_message, :request
26
26
 
27
27
  def initialize(raw, request = nil)
28
- @raw = raw
28
+ @raw = raw
29
29
  @request = request
30
- @status = :ok
30
+ @status = :ok
31
31
 
32
- if no_content_response?
32
+ if service_unavailable_response?
33
+ @status = :service_unavailable
34
+ @error_message = 'Service Unavailable, contenful.com API seems to be down'
35
+ elsif no_content_response?
33
36
  @status = :no_content
34
37
  @object = true
35
38
  elsif parse_json!
@@ -39,6 +42,10 @@ module Contentful
39
42
 
40
43
  private
41
44
 
45
+ def service_unavailable_response?
46
+ @raw.status == 503
47
+ end
48
+
42
49
  def no_content_response?
43
50
  @raw.to_s == '' && @raw.status == 204
44
51
  end
@@ -23,9 +23,9 @@ module Contentful
23
23
  def self.all
24
24
  request = Request.new('')
25
25
  response = request.get
26
- result = ResourceBuilder.new(self, response, {}, {})
26
+ result = ResourceBuilder.new(response, {}, {})
27
27
  spaces = result.run
28
- Contentful::Management::Client.shared_instance.update_dynamic_entry_cache_for_spaces!(spaces)
28
+ client.update_dynamic_entry_cache_for_spaces!(spaces)
29
29
  spaces
30
30
  end
31
31
 
@@ -35,9 +35,9 @@ module Contentful
35
35
  def self.find(space_id)
36
36
  request = Request.new("/#{ space_id }")
37
37
  response = request.get
38
- result = ResourceBuilder.new(self, response, {}, {})
38
+ result = ResourceBuilder.new(response, {}, {})
39
39
  space = result.run
40
- Contentful::Management::Client.shared_instance.update_dynamic_entry_cache_for_space!(space) if space.is_a? Space
40
+ client.update_dynamic_entry_cache_for_space!(space) if space.is_a? Space
41
41
  space
42
42
  end
43
43
 
@@ -47,7 +47,7 @@ module Contentful
47
47
  def self.create(attributes)
48
48
  request = Request.new('', {'name' => attributes.fetch(:name)}, id = nil, organization_id: attributes[:organization_id])
49
49
  response = request.post
50
- result = ResourceBuilder.new(self, response, {}, {})
50
+ result = ResourceBuilder.new(response, {}, {})
51
51
  result.run
52
52
  end
53
53
 
@@ -57,7 +57,7 @@ module Contentful
57
57
  def update(attributes)
58
58
  request = Request.new("/#{ id }", { 'name' => attributes.fetch(:name) }, id = nil, version: sys[:version], organization_id: attributes[:organization_id])
59
59
  response = request.put
60
- result = ResourceBuilder.new(self, response, {}, {})
60
+ result = ResourceBuilder.new(response, {}, {})
61
61
  refresh_data(result.run)
62
62
  end
63
63
 
@@ -80,7 +80,7 @@ module Contentful
80
80
  if response.status == :no_content
81
81
  return true
82
82
  else
83
- ResourceBuilder.new(self, response, {}, {}).run
83
+ ResourceBuilder.new(response, {}, {}).run
84
84
  end
85
85
  end
86
86
 
@@ -92,8 +92,8 @@ module Contentful
92
92
 
93
93
  content_types.instance_exec(self) do |space|
94
94
 
95
- define_singleton_method(:all) do
96
- ContentType.all(space.id)
95
+ define_singleton_method(:all) do |attributes = {}|
96
+ ContentType.all(space.id, attributes)
97
97
  end
98
98
 
99
99
  define_singleton_method(:create) do |params|
@@ -144,8 +144,8 @@ module Contentful
144
144
  assets = nil
145
145
 
146
146
  assets.instance_exec(self) do |space|
147
- define_singleton_method(:all) do
148
- Asset.all(space.id)
147
+ define_singleton_method(:all) do |attributes = {}|
148
+ Asset.all(space.id,attributes)
149
149
  end
150
150
 
151
151
  define_singleton_method(:find) do |asset_id|
@@ -1,6 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  module Contentful
3
3
  module Management
4
- VERSION = '0.0.2'
4
+ VERSION = '0.0.3'
5
5
  end
6
6
  end
@@ -0,0 +1,151 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://api.contentful.com/spaces/bfsvtul0c41g/assets/
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"fields":{"title":{"pl-PL":"Title PL"},"description":{"pl-PL":"Description
9
+ PL"},"file":{"pl-PL":{"contentType":"image/jpeg","fileName":"pic1.jpg","upload":"https://upload.wikimedia.org/wikipedia/commons/c/c7/Gasometer_Berlin_Sch%C3%B6neberg_2011.jpg"}}}}'
10
+ headers:
11
+ User-Agent:
12
+ - RubyContenfulManagementGem/0.0.2
13
+ Authorization:
14
+ - Bearer <ACCESS_TOKEN>
15
+ Content-Type:
16
+ - application/vnd.contentful.management.v1+json
17
+ Host:
18
+ - api.contentful.com
19
+ response:
20
+ status:
21
+ code: 201
22
+ message: Created
23
+ headers:
24
+ Server:
25
+ - nginx
26
+ Date:
27
+ - Tue, 19 Aug 2014 09:33:18 GMT
28
+ Content-Type:
29
+ - application/vnd.contentful.management.v1+json
30
+ Content-Length:
31
+ - '945'
32
+ Connection:
33
+ - keep-alive
34
+ X-Powered-By:
35
+ - Express
36
+ Cf-Space-Id:
37
+ - bfsvtul0c41g
38
+ Etag:
39
+ - '"172bc1b440a6d45f2d26c5c4b7d640d8"'
40
+ Access-Control-Allow-Origin:
41
+ - "*"
42
+ Access-Control-Allow-Headers:
43
+ - Accept,Accept-Language,Authorization,Cache-Control,Content-Length,Content-Range,Content-Type,DNT,Destination,Expires,If-Match,If-Modified-Since,If-None-Match,Keep-Alive,Last-Modified,Origin,Pragma,Range,User-Agent,X-Http-Method-Override,X-Mx-ReqToken,X-Requested-With,X-Contentful-Version,X-Contentful-Content-Type,X-Contentful-Organization
44
+ Access-Control-Allow-Methods:
45
+ - DELETE,GET,HEAD,POST,PUT,OPTIONS
46
+ "^access-Control-Expose-Headers":
47
+ - Etag
48
+ Access-Control-Max-Age:
49
+ - '1728000'
50
+ body:
51
+ encoding: UTF-8
52
+ string: |
53
+ {
54
+ "fields": {
55
+ "title": {
56
+ "pl-PL": "Title PL"
57
+ },
58
+ "description": {
59
+ "pl-PL": "Description PL"
60
+ },
61
+ "file": {
62
+ "pl-PL": {
63
+ "contentType": "image/jpeg",
64
+ "fileName": "pic1.jpg",
65
+ "upload": "https://upload.wikimedia.org/wikipedia/commons/c/c7/Gasometer_Berlin_Sch%C3%B6neberg_2011.jpg"
66
+ }
67
+ }
68
+ },
69
+ "sys": {
70
+ "id": "6HW6OmvN3UMgysEIicCmCC",
71
+ "type": "Asset",
72
+ "version": 1,
73
+ "createdAt": "2014-08-19T09:33:18.308Z",
74
+ "createdBy": {
75
+ "sys": {
76
+ "type": "Link",
77
+ "linkType": "User",
78
+ "id": "1E7acJL8I5XUXAMHQt9Grs"
79
+ }
80
+ },
81
+ "space": {
82
+ "sys": {
83
+ "type": "Link",
84
+ "linkType": "Space",
85
+ "id": "bfsvtul0c41g"
86
+ }
87
+ },
88
+ "updatedAt": "2014-08-19T09:33:18.308Z",
89
+ "updatedBy": {
90
+ "sys": {
91
+ "type": "Link",
92
+ "linkType": "User",
93
+ "id": "1E7acJL8I5XUXAMHQt9Grs"
94
+ }
95
+ }
96
+ }
97
+ }
98
+ http_version:
99
+ recorded_at: Tue, 19 Aug 2014 09:33:15 GMT
100
+ - request:
101
+ method: put
102
+ uri: https://api.contentful.com/spaces/bfsvtul0c41g/assets/6HW6OmvN3UMgysEIicCmCC/files/pl-PL/process
103
+ body:
104
+ encoding: US-ASCII
105
+ string: ''
106
+ headers:
107
+ User-Agent:
108
+ - RubyContenfulManagementGem/0.0.2
109
+ Authorization:
110
+ - Bearer <ACCESS_TOKEN>
111
+ Content-Type:
112
+ - application/vnd.contentful.management.v1+json
113
+ X-Contentful-Version:
114
+ - '1'
115
+ Content-Length:
116
+ - '0'
117
+ Host:
118
+ - api.contentful.com
119
+ response:
120
+ status:
121
+ code: 204
122
+ message: No Content
123
+ headers:
124
+ Server:
125
+ - nginx
126
+ Date:
127
+ - Tue, 19 Aug 2014 09:33:18 GMT
128
+ Content-Type:
129
+ - application/vnd.contentful.management.v1+json
130
+ Connection:
131
+ - keep-alive
132
+ X-Powered-By:
133
+ - Express
134
+ Cf-Space-Id:
135
+ - bfsvtul0c41g
136
+ Access-Control-Allow-Origin:
137
+ - "*"
138
+ Access-Control-Allow-Headers:
139
+ - Accept,Accept-Language,Authorization,Cache-Control,Content-Length,Content-Range,Content-Type,DNT,Destination,Expires,If-Match,If-Modified-Since,If-None-Match,Keep-Alive,Last-Modified,Origin,Pragma,Range,User-Agent,X-Http-Method-Override,X-Mx-ReqToken,X-Requested-With,X-Contentful-Version,X-Contentful-Content-Type,X-Contentful-Organization
140
+ Access-Control-Allow-Methods:
141
+ - DELETE,GET,HEAD,POST,PUT,OPTIONS
142
+ "^access-Control-Expose-Headers":
143
+ - Etag
144
+ Access-Control-Max-Age:
145
+ - '1728000'
146
+ body:
147
+ encoding: UTF-8
148
+ string: ''
149
+ http_version:
150
+ recorded_at: Tue, 19 Aug 2014 09:33:15 GMT
151
+ recorded_with: VCR 2.9.2
@@ -0,0 +1,123 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://api.contentful.com/spaces/bfsvtul0c41g/assets?limit=20&skip=2
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - RubyContenfulManagementGem/0.0.2
12
+ Authorization:
13
+ - Bearer <ACCESS_TOKEN>
14
+ Content-Type:
15
+ - application/vnd.contentful.management.v1+json
16
+ Host:
17
+ - api.contentful.com
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Server:
24
+ - nginx
25
+ Date:
26
+ - Mon, 18 Aug 2014 11:35:20 GMT
27
+ Content-Type:
28
+ - application/vnd.contentful.management.v1+json
29
+ Content-Length:
30
+ - '96'
31
+ Connection:
32
+ - keep-alive
33
+ X-Powered-By:
34
+ - Express
35
+ Cf-Space-Id:
36
+ - bfsvtul0c41g
37
+ Etag:
38
+ - '"254a0144c2cebdfa3c927ed18df93e57"'
39
+ Access-Control-Allow-Origin:
40
+ - "*"
41
+ Access-Control-Allow-Headers:
42
+ - Accept,Accept-Language,Authorization,Cache-Control,Content-Length,Content-Range,Content-Type,DNT,Destination,Expires,If-Match,If-Modified-Since,If-None-Match,Keep-Alive,Last-Modified,Origin,Pragma,Range,User-Agent,X-Http-Method-Override,X-Mx-ReqToken,X-Requested-With,X-Contentful-Version,X-Contentful-Content-Type,X-Contentful-Organization
43
+ Access-Control-Allow-Methods:
44
+ - DELETE,GET,HEAD,POST,PUT,OPTIONS
45
+ "^access-Control-Expose-Headers":
46
+ - Etag
47
+ Access-Control-Max-Age:
48
+ - '1728000'
49
+ body:
50
+ encoding: UTF-8
51
+ string: |
52
+ {
53
+ "sys": {
54
+ "type": "Array"
55
+ },
56
+ "total": 1,
57
+ "skip": 2,
58
+ "limit": 20,
59
+ "items": []
60
+ }
61
+ http_version:
62
+ recorded_at: Mon, 18 Aug 2014 11:35:22 GMT
63
+ - request:
64
+ method: get
65
+ uri: https://api.contentful.com/spaces/bfsvtul0c41g/assets?limit=20&skip=22
66
+ body:
67
+ encoding: US-ASCII
68
+ string: ''
69
+ headers:
70
+ User-Agent:
71
+ - RubyContenfulManagementGem/0.0.2
72
+ Authorization:
73
+ - Bearer <ACCESS_TOKEN>
74
+ Content-Type:
75
+ - application/vnd.contentful.management.v1+json
76
+ Host:
77
+ - api.contentful.com
78
+ response:
79
+ status:
80
+ code: 200
81
+ message: OK
82
+ headers:
83
+ Server:
84
+ - nginx
85
+ Date:
86
+ - Mon, 18 Aug 2014 11:35:38 GMT
87
+ Content-Type:
88
+ - application/vnd.contentful.management.v1+json
89
+ Content-Length:
90
+ - '97'
91
+ Connection:
92
+ - keep-alive
93
+ X-Powered-By:
94
+ - Express
95
+ Cf-Space-Id:
96
+ - bfsvtul0c41g
97
+ Etag:
98
+ - '"2b8587e6e9441d7e11a0bb162725080d"'
99
+ Access-Control-Allow-Origin:
100
+ - "*"
101
+ Access-Control-Allow-Headers:
102
+ - Accept,Accept-Language,Authorization,Cache-Control,Content-Length,Content-Range,Content-Type,DNT,Destination,Expires,If-Match,If-Modified-Since,If-None-Match,Keep-Alive,Last-Modified,Origin,Pragma,Range,User-Agent,X-Http-Method-Override,X-Mx-ReqToken,X-Requested-With,X-Contentful-Version,X-Contentful-Content-Type,X-Contentful-Organization
103
+ Access-Control-Allow-Methods:
104
+ - DELETE,GET,HEAD,POST,PUT,OPTIONS
105
+ "^access-Control-Expose-Headers":
106
+ - Etag
107
+ Access-Control-Max-Age:
108
+ - '1728000'
109
+ body:
110
+ encoding: UTF-8
111
+ string: |
112
+ {
113
+ "sys": {
114
+ "type": "Array"
115
+ },
116
+ "total": 1,
117
+ "skip": 22,
118
+ "limit": 20,
119
+ "items": []
120
+ }
121
+ http_version:
122
+ recorded_at: Mon, 18 Aug 2014 11:35:40 GMT
123
+ recorded_with: VCR 2.9.2