contentful 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 2.1.1
4
- - 2.1.0
4
+ - 2.1
5
5
  - 2.0.0
6
6
  - 1.9.3
7
7
  - jruby-19mode
8
- notifications:
9
- flowdock:
10
- - secure: "jO51sETvF1Je7jLQibT5P2ty3KAC8Q9/jJKPUKLDHAaz927XhhkrMXVs6tjzVG1BmHc5A+Qhuni26XerFRsocR7CTrqKdmvmXLgWyMtpW5CvUbtUCeApRQE2K4k/fb+7mz3EBAE494AK5Fz8f3vk2WVu5ROj5shuuggiYhoOqHw="
@@ -1,3 +1,8 @@
1
+ ### 0.3.0
2
+
3
+ * Support Synchronization
4
+
5
+
1
6
  ### 0.2.0
2
7
 
3
8
  * Introduce new :entry_mapping configuration to enable custom Entry classes based on ContentTypes
data/README.md CHANGED
@@ -168,7 +168,7 @@ You can register your custom class on client initialization:
168
168
  space: 'cfexampleapi',
169
169
  access_token: 'b4c0n73n7fu1',
170
170
  resource_mapping: {
171
- 'Asset' => MyBetterAsset,
171
+ 'Asset' => MyBetterAsset
172
172
  }
173
173
  )
174
174
 
@@ -191,6 +191,46 @@ You can also register custom entry classes to be used based on the entry's conte
191
191
  client.entry('nyancat') # is instance of Cat
192
192
 
193
193
 
194
+ ## Synchronization
195
+
196
+ The client also includes a wrapper for the synchronization endpoint. You can initialize it with the options described in the [Delivery API Documentation](https://www.contentful.com/developers/documentation/content-delivery-api/#sync) or an URL you received from a previous sync:
197
+
198
+ client = Contentful::Client.new(
199
+ access_token: 'b4c0n73n7fu1',
200
+ space: 'cfexampleapi',
201
+ default_locale: 'en-US'
202
+ )
203
+ sync = client.sync(initial: true, type: 'Deletion') # Only returns deleted entries and assets
204
+ sync = client.sync("https://cdn.contentful.com/spaces/cfexampleapi/sync?sync_token=w5ZGw6JFwqZmVcKsE8Kow4gr...sGPg") # Continues a sync
205
+
206
+ You can access the results either wrapped in `Contentful::SyncPage` objects:
207
+
208
+ sync.each_page do |page|
209
+ # Find resources at: page.items
210
+ end
211
+
212
+ # More explicit version:
213
+ page = sync.first_page
214
+ until sync.completed?
215
+ page = s.next_page
216
+ end
217
+
218
+ Or directly iterative over all resources:
219
+
220
+ sync.each_item do |resource|
221
+ # ...
222
+ end
223
+
224
+ When a sync is completed, the next sync url can be read from the Sync or SyncPage object:
225
+
226
+ sync.next_sync_url
227
+
228
+ **Please note** that synchronization entries come in all locales, so make sure, you supply a :default_locale property to the client configuration, when using the sync feature. This locale will be returned by default, when you call `Entry#fields`. The other localized data will also be saved and can be accessed by calling the fields method with a locale parameter:
229
+
230
+ first_entry = client.sync(initial: true, type: 'Entry').first_page.items.first
231
+ first_entry.fields('de-DE') # Returns German localizations
232
+
233
+
194
234
  ## License
195
235
 
196
236
  Copyright (c) 2014 Contentful GmbH - Jan Lelis. See LICENSE.txt for further details.
data/TAGS ADDED
@@ -0,0 +1,312 @@
1
+
2
+ examples/custom_classes.rb,120
3
+ class MyResourceMyResource12,405
4
+ class MyBetterArray < Contentful::ArrayMyBetterArray36,919
5
+ def lastlast38,1002
6
+
7
+ examples/dynamic_entries.rb,0
8
+
9
+ examples/example_queries.rb,0
10
+
11
+ examples/raise_errors.rb,0
12
+
13
+ examples/raw_mode.rb,0
14
+
15
+ examples/resource_mapping.rb,84
16
+ class MyBetterArray < Contentful::ArrayMyBetterArray12,361
17
+ def lastlast14,444
18
+
19
+ lib/contentful/array.rb,95
20
+ module ContentfulContentful4,68
21
+ class ArrayArray8,341
22
+ def next_pagenext_page21,647
23
+
24
+ lib/contentful/asset.rb,109
25
+ module ContentfulContentful4,70
26
+ class AssetAsset7,203
27
+ def image_url(options = {})image_url19,627
28
+
29
+ lib/contentful/client.rb,1109
30
+ module ContentfulContentful10,256
31
+ class ClientClient14,419
32
+ def self.get_http(url, query, headers = {})get_http32,886
33
+ def initialize(given_configuration = {})initialize36,987
34
+ def default_configurationdefault_configuration49,1356
35
+ def space(query = {})space56,1537
36
+ def content_type(id, query = {})content_type63,1742
37
+ def content_types(query = {})content_types70,1993
38
+ def entry(id, query = {})entry77,2207
39
+ def entries(query = {})entries84,2433
40
+ def asset(id, query = {})asset91,2635
41
+ def assets(query = {})assets98,2859
42
+ def base_urlbase_url103,3001
43
+ def request_headersrequest_headers108,3190
44
+ def request_query(query)request_query117,3660
45
+ def get(request, build_resource = true)get128,3998
46
+ def update_dynamic_entry_cache!update_dynamic_entry_cache!154,4785
47
+ def register_dynamic_entry(key, klass)register_dynamic_entry167,5115
48
+ def sync(options = { initial: true })sync174,5355
49
+ def normalize_configuration!normalize_configuration!181,5450
50
+ def validate_configuration!validate_configuration!186,5700
51
+
52
+ lib/contentful/content_type.rb,72
53
+ module ContentfulContentful4,54
54
+ class ContentTypeContentType7,201
55
+
56
+ lib/contentful/deleted_asset.rb,74
57
+ module ContentfulContentful3,29
58
+ class DeletedAssetDeletedAsset6,185
59
+
60
+ lib/contentful/deleted_entry.rb,74
61
+ module ContentfulContentful3,29
62
+ class DeletedEntryDeletedEntry6,185
63
+
64
+ lib/contentful/dynamic_entry.rb,130
65
+ module ContentfulContentful5,92
66
+ class DynamicEntry < EntryDynamicEntry6,110
67
+ def self.create(content_type)create18,397
68
+
69
+ lib/contentful/entry.rb,60
70
+ module ContentfulContentful4,64
71
+ class EntryEntry7,198
72
+
73
+ lib/contentful/error.rb,547
74
+ module ContentfulContentful1,0
75
+ class Error < StandardErrorError4,142
76
+ def initialize(response)initialize7,199
77
+ def self.[](error_status_code)[]14,396
78
+ class NotFound < Error; endNotFound33,691
79
+ class BadRequest < Error; endBadRequest36,730
80
+ class AccessDenied < Error; endAccessDenied39,771
81
+ class Unauthorized < Error; endUnauthorized42,814
82
+ class ServerError < Error; endServerError45,857
83
+ class UnparsableJson < Error; endUnparsableJson48,933
84
+ class UnparsableResource < Error; endUnparsableResource51,1037
85
+
86
+ lib/contentful/field.rb,60
87
+ module ContentfulContentful3,29
88
+ class FieldField6,195
89
+
90
+ lib/contentful/file.rb,57
91
+ module ContentfulContentful3,29
92
+ class FileFile5,73
93
+
94
+ lib/contentful/link.rb,101
95
+ module ContentfulContentful3,29
96
+ class LinkLink6,160
97
+ def resolve(query = {})resolve12,359
98
+
99
+ lib/contentful/locale.rb,62
100
+ module ContentfulContentful3,29
101
+ class LocaleLocale6,207
102
+
103
+ lib/contentful/location.rb,66
104
+ module ContentfulContentful3,29
105
+ class LocationLocation6,200
106
+
107
+ lib/contentful/request.rb,301
108
+ module ContentfulContentful1,0
109
+ class RequestRequest5,234
110
+ def initialize(client, endpoint, query = {}, id = nil)initialize8,295
111
+ def urlurl27,740
112
+ def getget32,870
113
+ def absolute?absolute?37,964
114
+ def copycopy42,1064
115
+ def normalize_query(query)normalize_query49,1139
116
+
117
+ lib/contentful/resource/array_like.rb,235
118
+ module ContentfulContentful1,0
119
+ module ResourceResource2,18
120
+ module ArrayLikeArrayLike5,138
121
+ def array?array?9,231
122
+ def each_item(&block)each_item14,304
123
+ def empty?empty?20,431
124
+ def sizesize25,512
125
+
126
+ lib/contentful/resource/asset_fields.rb,378
127
+ module ContentfulContentful3,28
128
+ module ResourceResource4,46
129
+ module AssetFieldsAssetFields8,212
130
+ def fieldsfields15,345
131
+ def initialize(object, *)initialize19,397
132
+ def inspect(info = nil)inspect25,547
133
+ module ClassMethodsClassMethods33,713
134
+ def fields_coercionsfields_coercions34,739
135
+ def self.included(base)included39,818
136
+
137
+ lib/contentful/resource/fields.rb,482
138
+ module ContentfulContentful1,0
139
+ module ResourceResource2,18
140
+ module FieldsFields7,215
141
+ def fields(wanted_locale = default_locale)fields9,273
142
+ def initialize(object, *)initialize13,374
143
+ def inspect(info = nil)inspect18,474
144
+ def extract_fields_from_object!(object)extract_fields_from_object!29,656
145
+ module ClassMethodsClassMethods46,1227
146
+ def fields_coercionsfields_coercions48,1309
147
+ def self.included(base)included53,1374
148
+
149
+ lib/contentful/resource/system_properties.rb,350
150
+ module ContentfulContentful1,0
151
+ module ResourceResource2,18
152
+ module SystemPropertiesSystemProperties4,100
153
+ def initialize(object, *)initialize18,406
154
+ def inspect(info = nil)inspect23,518
155
+ module ClassMethodsClassMethods31,675
156
+ def sys_coercionssys_coercions32,701
157
+ def self.included(base)included37,774
158
+
159
+ lib/contentful/resource.rb,1041
160
+ module ContentfulContentful4,62
161
+ module ResourceResource13,463
162
+ def initialize(object, request = nil, client = nil, nested_locale_fields = false, default_locale = Contentful::Client::DEFAULT_CONFIGURATION[:default_locale] )initialize24,743
163
+ def inspect(info = nil)inspect34,1184
164
+ def array?array?40,1417
165
+ def nested_locale_fields?nested_locale_fields?45,1544
166
+ def syssys50,1686
167
+ def fieldsfields55,1797
168
+ def reloadreload61,1941
169
+ def extract_from_object(object, namespace, keys = nil)extract_from_object71,2050
170
+ def coerce_value_or_array(value, what = nil)coerce_value_or_array85,2459
171
+ def coerce_or_create_class(value, what)coerce_or_create_class93,2669
172
+ module ClassMethodsClassMethods105,2982
173
+ def nested_locale_fields?nested_locale_fields?107,3098
174
+ def property_coercionsproperty_coercions111,3155
175
+ def property(name, property_class = nil)property126,3829
176
+ def update_coercions!update_coercions!134,4100
177
+ def self.included(base)included153,4689
178
+
179
+ lib/contentful/resource_builder.rb,2028
180
+ module ContentfulContentful13,309
181
+ class ResourceBuilderResourceBuilder16,470
182
+ def initialize(client, response, resource_mapping = {}, entry_mapping = {}, default_locale = Contentful::Client::DEFAULT_CONFIGURATION[:default_locale])initialize32,904
183
+ def runrun45,1502
184
+ def create_all_resources!create_all_resources!69,2206
185
+ def create_resource(object)create_resource82,2600
186
+ def find_entry_class(object)find_entry_class95,3044
187
+ def try_dynamic_entry(object)try_dynamic_entry100,3233
188
+ def get_dynamic_entry(object)get_dynamic_entry105,3372
189
+ def content_type_id_for_entry(object)content_type_id_for_entry112,3600
190
+ def array_or_sync_page(object)array_or_sync_page120,3871
191
+ def detect_resource_class(object)detect_resource_class135,4329
192
+ def default_resource_mappingdefault_resource_mapping151,4728
193
+ def default_entry_mappingdefault_entry_mapping156,4837
194
+ def detect_child_objects(object)detect_child_objects163,4922
195
+ def detect_child_arrays(object)detect_child_arrays171,5091
196
+ def add_to_known_resources(res)add_to_known_resources184,5353
197
+ def replace_children(res, object)replace_children188,5488
198
+ def replace_child_array(child_array)replace_child_array200,5969
199
+ def create_included_resources!(included_objects)create_included_resources!204,6097
200
+ def replace_links_with_known_resources(res, seen_resource_ids = [])replace_links_with_known_resources217,6436
201
+ def replace_links_in_properties(property_container, seen_resource_ids)replace_links_in_properties231,6892
202
+ def replace_links_in_array(property_container, seen_resource_ids)replace_links_in_array241,7296
203
+ def replace_link_or_check_recursively(property_value, property_container, property_name, seen_resource_ids)replace_link_or_check_recursively247,7574
204
+ def maybe_replace_link(link, parent, key)maybe_replace_link255,8011
205
+ def replace_links_in_included_resources_with_known_resourcesreplace_links_in_included_resources_with_known_resources262,8243
206
+
207
+ lib/contentful/response.rb,228
208
+ module ContentfulContentful4,47
209
+ class ResponseResponse22,837
210
+ def initialize(raw, request = nil)initialize25,920
211
+ def parse_json!parse_json!38,1113
212
+ def parse_contentful_error!parse_contentful_error!48,1327
213
+
214
+ lib/contentful/space.rb,60
215
+ module ContentfulContentful4,55
216
+ class SpaceSpace7,188
217
+
218
+ lib/contentful/support.rb,103
219
+ module ContentfulContentful1,0
220
+ module SupportSupport3,65
221
+ def snakify(object)snakify6,165
222
+
223
+ lib/contentful/sync.rb,450
224
+ module ContentfulContentful6,132
225
+ class SyncSync7,150
226
+ def initialize(client, options_or_url)initialize10,195
227
+ def each_page(&block)each_page18,468
228
+ def first_pagefirst_page29,680
229
+ def completed?completed?34,813
230
+ def each_item(&block)each_item39,925
231
+ def get(options_or_url)get47,1067
232
+ def link_page_to_sync!(page)link_page_to_sync!63,1375
233
+ def update_sync_state_from!(page)update_sync_state_from!67,1463
234
+
235
+ lib/contentful/sync_page.rb,237
236
+ module ContentfulContentful4,68
237
+ class SyncPageSyncPage5,86
238
+ def self.nested_locale_fields?nested_locale_fields16,328
239
+ def next_pagenext_page20,383
240
+ def next_page?next_page?24,454
241
+ def last_page?last_page?28,505
242
+
243
+ lib/contentful/version.rb,33
244
+ module ContentfulContentful1,0
245
+
246
+ lib/contentful.rb,0
247
+
248
+ spec/array_spec.rb,0
249
+
250
+ spec/asset_spec.rb,0
251
+
252
+ spec/auto_includes_spec.rb,0
253
+
254
+ spec/client_class_spec.rb,0
255
+
256
+ spec/client_configuration_spec.rb,171
257
+ class MyBetterAsset < Contentful::AssetMyBetterAsset180,5381
258
+ def https_image_urlhttps_image_url181,5427
259
+ class Cat < Contentful::EntryCat200,5922
260
+
261
+ spec/coercions_spec.rb,0
262
+
263
+ spec/content_type_spec.rb,0
264
+
265
+ spec/deleted_asset_spec.rb,0
266
+
267
+ spec/deleted_entry_spec.rb,0
268
+
269
+ spec/dynamic_entry_spec.rb,0
270
+
271
+ spec/entry_spec.rb,0
272
+
273
+ spec/error_class_spec.rb,0
274
+
275
+ spec/error_requests_spec.rb,0
276
+
277
+ spec/field_spec.rb,0
278
+
279
+ spec/file_spec.rb,0
280
+
281
+ spec/link_spec.rb,0
282
+
283
+ spec/locale_spec.rb,0
284
+
285
+ spec/location_spec.rb,0
286
+
287
+ spec/request_spec.rb,0
288
+
289
+ spec/resource_building_spec.rb,0
290
+
291
+ spec/resource_spec.rb,0
292
+
293
+ spec/response_spec.rb,0
294
+
295
+ spec/space_spec.rb,0
296
+
297
+ spec/spec_helper.rb,0
298
+
299
+ spec/support/client.rb,50
300
+ def create_client(options = {})create_client1,0
301
+
302
+ spec/support/json_responses.rb,117
303
+ def raw_fixture(which, as_json = false)raw_fixture3,22
304
+ def json_fixture(which, as_json = false)json_fixture7,148
305
+
306
+ spec/support/vcr.rb,80
307
+ def vcr(name, &block)vcr10,201
308
+ def expect_vcr(name, &block)expect_vcr14,261
309
+
310
+ spec/sync_page_spec.rb,0
311
+
312
+ spec/sync_spec.rb,0
@@ -1,4 +1,5 @@
1
1
  require_relative 'resource'
2
+ require_relative 'resource/array_like'
2
3
 
3
4
  module Contentful
4
5
  # Resource Class for Arrays (e.g. search results)
@@ -9,18 +10,13 @@ module Contentful
9
10
 
10
11
  include Contentful::Resource
11
12
  include Contentful::Resource::SystemProperties
12
- include Enumerable
13
+ include Contentful::Resource::ArrayLike
13
14
 
14
15
  property :total, :integer
15
16
  property :limit, :integer
16
17
  property :skip, :integer
17
18
  property :items
18
19
 
19
- # Only returns true for Contentful::Array
20
- def array?
21
- true
22
- end
23
-
24
20
  # Simplifies pagination
25
21
  def next_page
26
22
  if request
@@ -33,14 +29,5 @@ module Contentful
33
29
  end
34
30
  end
35
31
 
36
- # Delegates to items#each
37
- def each(&block)
38
- items.each(&block)
39
- end
40
-
41
- # Delegates to items#empty?
42
- def empty?
43
- items.empty?
44
- end
45
32
  end
46
33
  end
@@ -1,7 +1,11 @@
1
1
  require_relative 'request'
2
2
  require_relative 'response'
3
3
  require_relative 'resource_builder'
4
+ require_relative 'sync'
5
+
4
6
  require 'http'
7
+ # see: https://github.com/tarcieri/http/commit/6a2a9d22902572d672dfc7c7b250d022364c8e01#commitcomment-6192269
8
+ require 'cgi'
5
9
 
6
10
  module Contentful
7
11
  # The client object is initialized with a space and a key and then used
@@ -18,6 +22,7 @@ module Contentful
18
22
  resource_builder: ResourceBuilder,
19
23
  resource_mapping: {},
20
24
  entry_mapping: {},
25
+ default_locale: 'en-US',
21
26
  raw_mode: false
22
27
  }
23
28
 
@@ -121,9 +126,10 @@ module Contentful
121
126
  # Set second parameter to false to deactivate Resource building and
122
127
  # return Response objects instead
123
128
  def get(request, build_resource = true)
129
+ url = request.absolute? ? request.url : base_url + request.url
124
130
  response = Response.new(
125
131
  self.class.get_http(
126
- base_url + request.url,
132
+ url,
127
133
  request_query(request.query),
128
134
  request_headers
129
135
  ), request
@@ -133,8 +139,10 @@ module Contentful
133
139
 
134
140
  result = configuration[:resource_builder].new(
135
141
  self,
136
- response, configuration[:resource_mapping],
137
- configuration[:entry_mapping]
142
+ response,
143
+ configuration[:resource_mapping],
144
+ configuration[:entry_mapping],
145
+ configuration[:default_locale]
138
146
  ).run
139
147
 
140
148
  raise result if result.is_a?(Error) && configuration[:raise_errors]
@@ -160,10 +168,18 @@ module Contentful
160
168
  @dynamic_entry_cache[key.to_sym] = klass
161
169
  end
162
170
 
171
+ # Create a new synchronisation object
172
+ # Takes sync options or a sync_url
173
+ # You will need to call #each_page or #first_page on it
174
+ def sync(options = { initial: true })
175
+ Sync.new(self, options)
176
+ end
177
+
178
+
163
179
  private
164
180
 
165
181
  def normalize_configuration!
166
- [:space, :access_token, :api_url].each { |s| configuration[s] = configuration[s].to_s }
182
+ [:space, :access_token, :api_url, :default_locale].each { |s| configuration[s] = configuration[s].to_s }
167
183
  configuration[:authentication_mechanism] = configuration[:authentication_mechanism].to_sym
168
184
  end
169
185
 
@@ -180,6 +196,10 @@ module Contentful
180
196
  raise ArgumentError, 'The client configuration needs to contain an :api_url'
181
197
  end
182
198
 
199
+ if configuration[:default_locale].empty?
200
+ raise ArgumentError, 'The client configuration needs to contain a :default_locale'
201
+ end
202
+
183
203
  unless configuration[:api_version].to_i >= 0
184
204
  raise ArgumentError, 'The :api_version must be a positive number or nil'
185
205
  end