contentful 0.3.4 → 0.3.5

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 (57) hide show
  1. checksums.yaml +7 -0
  2. data/.travis.yml +1 -1
  3. data/CHANGELOG.md +43 -24
  4. data/README.md +22 -0
  5. data/RELEASE.md +8 -0
  6. data/Rakefile +4 -6
  7. data/contentful.gemspec +7 -9
  8. data/examples/custom_classes.rb +1 -2
  9. data/examples/dynamic_entries.rb +2 -4
  10. data/examples/example_queries.rb +2 -3
  11. data/examples/raise_errors.rb +3 -5
  12. data/examples/raw_mode.rb +2 -3
  13. data/examples/resource_mapping.rb +3 -4
  14. data/lib/contentful/array.rb +0 -1
  15. data/lib/contentful/asset.rb +1 -1
  16. data/lib/contentful/client.rb +47 -38
  17. data/lib/contentful/deleted_entry.rb +1 -1
  18. data/lib/contentful/request.rb +1 -2
  19. data/lib/contentful/resource.rb +1 -1
  20. data/lib/contentful/resource/array_like.rb +2 -3
  21. data/lib/contentful/resource/asset_fields.rb +1 -1
  22. data/lib/contentful/resource/fields.rb +2 -3
  23. data/lib/contentful/resource/system_properties.rb +1 -1
  24. data/lib/contentful/resource_builder.rb +14 -23
  25. data/lib/contentful/response.rb +40 -35
  26. data/lib/contentful/sync.rb +1 -3
  27. data/lib/contentful/sync_page.rb +2 -3
  28. data/lib/contentful/version.rb +1 -1
  29. data/spec/array_spec.rb +20 -20
  30. data/spec/asset_spec.rb +14 -14
  31. data/spec/auto_includes_spec.rb +4 -4
  32. data/spec/client_class_spec.rb +22 -24
  33. data/spec/client_configuration_spec.rb +48 -49
  34. data/spec/coercions_spec.rb +4 -4
  35. data/spec/content_type_spec.rb +12 -12
  36. data/spec/deleted_asset_spec.rb +10 -11
  37. data/spec/deleted_entry_spec.rb +9 -10
  38. data/spec/dynamic_entry_spec.rb +11 -11
  39. data/spec/entry_spec.rb +14 -14
  40. data/spec/error_class_spec.rb +15 -18
  41. data/spec/error_requests_spec.rb +6 -7
  42. data/spec/field_spec.rb +9 -9
  43. data/spec/file_spec.rb +7 -7
  44. data/spec/link_spec.rb +14 -14
  45. data/spec/locale_spec.rb +6 -6
  46. data/spec/location_spec.rb +10 -10
  47. data/spec/request_spec.rb +13 -13
  48. data/spec/resource_building_spec.rb +7 -8
  49. data/spec/resource_spec.rb +14 -14
  50. data/spec/response_spec.rb +25 -24
  51. data/spec/space_spec.rb +10 -10
  52. data/spec/spec_helper.rb +1 -2
  53. data/spec/support/json_responses.rb +2 -2
  54. data/spec/support/vcr.rb +3 -3
  55. data/spec/sync_page_spec.rb +26 -27
  56. data/spec/sync_spec.rb +22 -24
  57. metadata +41 -66
@@ -7,4 +7,4 @@ module Contentful
7
7
  include Contentful::Resource
8
8
  include Contentful::Resource::SystemProperties
9
9
  end
10
- end
10
+ end
@@ -11,7 +11,7 @@ module Contentful
11
11
  @absolute = true if @endpoint.start_with?('http')
12
12
 
13
13
  @query = if query && !query.empty?
14
- normalize_query(query)
14
+ normalize_query(query)
15
15
  end
16
16
 
17
17
  if id
@@ -43,7 +43,6 @@ module Contentful
43
43
  Marshal.load(Marshal.dump(self))
44
44
  end
45
45
 
46
-
47
46
  private
48
47
 
49
48
  def normalize_query(query)
@@ -21,7 +21,7 @@ module Contentful
21
21
 
22
22
  attr_reader :properties, :request, :client, :default_locale
23
23
 
24
- def initialize(object, request = nil, client = nil, nested_locale_fields = false, default_locale = Contentful::Client::DEFAULT_CONFIGURATION[:default_locale] )
24
+ def initialize(object, request = nil, client = nil, nested_locale_fields = false, default_locale = Contentful::Client::DEFAULT_CONFIGURATION[:default_locale])
25
25
  self.class.update_coercions!
26
26
  @nested_locale_fields = nested_locale_fields
27
27
  @default_locale = default_locale
@@ -14,7 +14,7 @@ module Contentful
14
14
  def each_item(&block)
15
15
  items.each(&block)
16
16
  end
17
- alias each each_item
17
+ alias_method :each, :each_item
18
18
 
19
19
  # Delegates to items#empty?
20
20
  def empty?
@@ -25,8 +25,7 @@ module Contentful
25
25
  def size
26
26
  items.size
27
27
  end
28
- alias length size
29
-
28
+ alias_method :length, :size
30
29
  end
31
30
  end
32
31
  end
@@ -19,7 +19,7 @@ module Contentful
19
19
  def initialize(object, *)
20
20
  super
21
21
  @fields = {}
22
- @fields[locale] = extract_from_object object["fields"], :fields
22
+ @fields[locale] = extract_from_object object['fields'], :fields
23
23
  end
24
24
 
25
25
  def inspect(info = nil)
@@ -23,14 +23,13 @@ module Contentful
23
23
  end
24
24
  end
25
25
 
26
-
27
26
  private
28
27
 
29
28
  def extract_fields_from_object!(object)
30
29
  @fields = {}
31
30
 
32
31
  if nested_locale_fields?
33
- object["fields"].each do |field_name, nested_child_object|
32
+ object['fields'].each do |field_name, nested_child_object|
34
33
  nested_child_object.each do |object_locale, real_child_object|
35
34
  @fields[object_locale] ||= {}
36
35
  @fields[object_locale].merge! extract_from_object(
@@ -39,7 +38,7 @@ module Contentful
39
38
  end
40
39
  end
41
40
  else
42
- @fields[locale] = extract_from_object object["fields"], :fields
41
+ @fields[locale] = extract_from_object object['fields'], :fields
43
42
  end
44
43
  end
45
44
 
@@ -17,7 +17,7 @@ module Contentful
17
17
 
18
18
  def initialize(object, *)
19
19
  super
20
- @sys = extract_from_object object["sys"], :sys
20
+ @sys = extract_from_object object['sys'], :sys
21
21
  end
22
22
 
23
23
  def inspect(info = nil)
@@ -28,12 +28,11 @@ module Contentful
28
28
 
29
29
  attr_reader :client, :response, :resource_mapping, :entry_mapping, :resource
30
30
 
31
-
32
31
  def initialize(client, response, resource_mapping = {}, entry_mapping = {}, default_locale = Contentful::Client::DEFAULT_CONFIGURATION[:default_locale])
33
32
  @response = response
34
33
  @client = client
35
34
  @included_resources = {}
36
- @known_resources = Hash.new{ |h,k| h[k] = {} }
35
+ @known_resources = Hash.new { |h, k| h[k] = {} }
37
36
  @nested_locales = false
38
37
  @default_locale = default_locale
39
38
  @resource_mapping = default_resource_mapping.merge(resource_mapping)
@@ -43,19 +42,10 @@ module Contentful
43
42
  # Starts the parsing process.
44
43
  # Either returns an Error, or the parsed Resource
45
44
  def run
46
- case response.status
47
- when :contentful_error
48
- Error[response.raw.status].new(response)
49
- when :unparsable_json
50
- UnparsableJson.new(response)
51
- when :not_contentful
52
- Error.new(response)
45
+ if response.status == :ok
46
+ create_all_resources!
53
47
  else
54
- begin
55
- create_all_resources!
56
- rescue UnparsableResource => error
57
- error
58
- end
48
+ response.object
59
49
  end
60
50
  end
61
51
 
@@ -76,6 +66,8 @@ module Contentful
76
66
  end
77
67
 
78
68
  @resource
69
+ rescue UnparsableResource => error
70
+ error
79
71
  end
80
72
 
81
73
  # Creates a single resource from the response object
@@ -110,15 +102,15 @@ module Contentful
110
102
 
111
103
  # Returns the id of the related ContentType, if there is one
112
104
  def content_type_id_for_entry(object)
113
- object["sys"] &&
114
- object["sys"]["contentType"] &&
115
- object["sys"]["contentType"]["sys"] &&
116
- object["sys"]["contentType"]["sys"]["id"]
105
+ object['sys'] &&
106
+ object['sys']['contentType'] &&
107
+ object['sys']['contentType']['sys'] &&
108
+ object['sys']['contentType']['sys']['id']
117
109
  end
118
110
 
119
111
  # Detects if a resource is an Contentful::Array or a SyncPage
120
112
  def array_or_sync_page(object)
121
- if object["nextPageUrl"] || object["nextSyncUrl"]
113
+ if object['nextPageUrl'] || object['nextSyncUrl']
122
114
  SyncPage
123
115
  else
124
116
  Array
@@ -133,7 +125,7 @@ module Contentful
133
125
  # - Proc: Will be called, expected to return the proper Class
134
126
  # - Symbol: Will be called as method of the ResourceBuilder itself
135
127
  def detect_resource_class(object)
136
- type = object["sys"] && object["sys"]["type"]
128
+ type = object['sys'] && object['sys']['type']
137
129
 
138
130
  case res_class = resource_mapping[type]
139
131
  when Symbol
@@ -141,7 +133,7 @@ module Contentful
141
133
  when Proc
142
134
  res_class[object]
143
135
  when nil
144
- raise UnparsableResource.new(response)
136
+ fail UnparsableResource.new(response)
145
137
  else
146
138
  res_class
147
139
  end
@@ -157,7 +149,6 @@ module Contentful
157
149
  DEFAULT_ENTRY_MAPPING.dup
158
150
  end
159
151
 
160
-
161
152
  private
162
153
 
163
154
  def detect_child_objects(object)
@@ -191,7 +182,7 @@ module Contentful
191
182
  res.public_send(name)[child_name.to_sym] = create_resource(child_object)
192
183
  end
193
184
  next if name == 'includes'
194
- detect_child_arrays(potential_objects).each do |child_name, child_array|
185
+ detect_child_arrays(potential_objects).each do |child_name, _child_array|
195
186
  replace_child_array res.public_send(name)[child_name.to_sym]
196
187
  end
197
188
  end
@@ -27,62 +27,67 @@ module Contentful
27
27
  @request = request
28
28
  @status = :ok
29
29
 
30
- if service_unavailable_response?
31
- @status = :service_unavailable
32
- @error_message = 'Service Unavailable, contenful.com API seems to be down'
30
+ if valid_http_response?
31
+ parse_json!
33
32
  elsif no_content_response?
34
33
  @status = :no_content
35
- @object = true
36
- elsif parse_json!
37
- parse_contentful_error!
34
+ elsif no_resource_or_bad_request?
35
+ parse_contentful_error
36
+ else
37
+ parse_http_error
38
38
  end
39
39
  end
40
40
 
41
41
  private
42
42
 
43
- def service_unavailable_response?
44
- @raw.status == 503
43
+ def error_object?
44
+ object['sys']['type'] == 'Error'
45
+ end
46
+
47
+ def parse_contentful_error
48
+ @object = load_json
49
+ @error_message = object['message'] if error_object?
50
+ parse_http_error
51
+ end
52
+
53
+ def valid_http_response?
54
+ [200, 201].include?(raw.status)
55
+ end
56
+
57
+ def parse_http_error
58
+ @status = :error
59
+ @object = Error[raw.status].new(self)
60
+ end
61
+
62
+ def no_resource_or_bad_request?
63
+ [400, 404].include?(raw.status)
45
64
  end
46
65
 
47
66
  def no_content_response?
48
- @raw.to_s == '' && @raw.status == 204
67
+ raw.to_s == '' && raw.status == 204
49
68
  end
50
69
 
51
70
  def parse_json!
52
- body = unzip_response(raw)
53
- @object = MultiJson.load(body)
54
- true
55
- rescue MultiJson::LoadError => e
56
- @status = :unparsable_json
57
- @error_message = e.message
58
- @object = e
59
- false
71
+ @object = load_json
72
+ rescue MultiJson::LoadError => error
73
+ @error_message = error.message
74
+ @status = :error
75
+ UnparsableJson.new(self)
60
76
  end
61
77
 
62
- def parse_contentful_error!
63
- if @object && @object["sys"]
64
- if @object["sys"]["type"] == 'Error'
65
- @status = :contentful_error
66
- @error_message = object['message']
67
- true
68
- else
69
- false
70
- end
71
- else
72
- @status = :not_contentful
73
- @error_message = "No contentful system properties found in object"
74
- end
78
+ def load_json
79
+ MultiJson.load(unzip_response(raw))
75
80
  end
76
81
 
77
82
  def unzip_response(response)
78
- if response.headers['Content-Encoding'].eql?('gzip') then
79
- sio = StringIO.new(response.to_s)
83
+ parsed_response = response.to_s
84
+ if response.headers['Content-Encoding'].eql?('gzip')
85
+ sio = StringIO.new(parsed_response)
80
86
  gz = Zlib::GzipReader.new(sio)
81
- gz.read()
87
+ gz.read
82
88
  else
83
- response.to_s
89
+ parsed_response
84
90
  end
85
91
  end
86
-
87
92
  end
88
93
  end
@@ -32,7 +32,7 @@ module Contentful
32
32
 
33
33
  # Returns false as long as last sync page has not been reached
34
34
  def completed?
35
- !! next_sync_url
35
+ !!next_sync_url
36
36
  end
37
37
 
38
38
  # Directly iterates over all resources that have changed
@@ -57,7 +57,6 @@ module Contentful
57
57
  page
58
58
  end
59
59
 
60
-
61
60
  private
62
61
 
63
62
  def link_page_to_sync!(page)
@@ -67,6 +66,5 @@ module Contentful
67
66
  def update_sync_state_from!(page)
68
67
  @next_sync_url = page.next_sync_url
69
68
  end
70
-
71
69
  end
72
70
  end
@@ -22,12 +22,11 @@ module Contentful
22
22
  end
23
23
 
24
24
  def next_page?
25
- !! next_page_url
25
+ !!next_page_url
26
26
  end
27
27
 
28
28
  def last_page?
29
- ! next_page_url
29
+ !next_page_url
30
30
  end
31
-
32
31
  end
33
32
  end
@@ -1,3 +1,3 @@
1
1
  module Contentful
2
- VERSION = '0.3.4'
2
+ VERSION = '0.3.5'
3
3
  end
data/spec/array_spec.rb CHANGED
@@ -1,69 +1,69 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Contentful::Array do
4
- let(:array){ vcr('array'){ create_client.content_types } }
4
+ let(:array) { vcr('array') { create_client.content_types } }
5
5
 
6
6
  describe 'SystemProperties' do
7
7
  it 'has a #sys getter returning a hash with symbol keys' do
8
- expect( array.sys ).to be_a Hash
9
- expect( array.sys.keys.sample ).to be_a Symbol
8
+ expect(array.sys).to be_a Hash
9
+ expect(array.sys.keys.sample).to be_a Symbol
10
10
  end
11
11
 
12
12
  it 'has #type' do
13
- expect( array.type ).to eq "Array"
13
+ expect(array.type).to eq 'Array'
14
14
  end
15
15
  end
16
16
 
17
17
  describe 'Properties' do
18
18
  it 'has a #properties getter returning a hash with symbol keys' do
19
- expect( array.properties ).to be_a Hash
20
- expect( array.properties.keys.sample ).to be_a Symbol
19
+ expect(array.properties).to be_a Hash
20
+ expect(array.properties.keys.sample).to be_a Symbol
21
21
  end
22
22
 
23
23
  it 'has #total' do
24
- expect( array.total ).to eq 4
24
+ expect(array.total).to eq 4
25
25
  end
26
26
 
27
27
  it 'has #skip' do
28
- expect( array.skip ).to eq 0
28
+ expect(array.skip).to eq 0
29
29
  end
30
30
 
31
31
  it 'has #limit' do
32
- expect( array.limit ).to eq 100
32
+ expect(array.limit).to eq 100
33
33
  end
34
34
 
35
35
  it 'has #items which contain resources' do
36
- expect( array.items ).to be_a Array
37
- expect( array.items.sample ).to be_a Contentful::Resource
36
+ expect(array.items).to be_a Array
37
+ expect(array.items.sample).to be_a Contentful::Resource
38
38
  end
39
39
  end
40
40
 
41
41
  describe '#each' do
42
42
  it 'is an Enumerator' do
43
- expect( array.each ).to be_a Enumerator
43
+ expect(array.each).to be_a Enumerator
44
44
  end
45
45
 
46
46
  it 'iterates over items' do
47
- expect( array.each.to_a ).to eq array.items
47
+ expect(array.each.to_a).to eq array.items
48
48
  end
49
49
 
50
50
  it 'includes Enumerable' do
51
- expect( array.map{ |r| r.type } ).to eq array.items.map{ |r| r.type }
51
+ expect(array.map { |r| r.type }).to eq array.items.map { |r| r.type }
52
52
  end
53
53
  end
54
54
 
55
55
  describe '#next_page' do
56
56
  it 'requests more of the same content from the server, using its limit and skip values' do
57
- array_page_1 = vcr('array_page_1'){ create_client.content_types(skip: 3, limit: 2) }
58
- array_page_2 = vcr('array_page_2'){ array_page_1.next_page }
57
+ array_page_1 = vcr('array_page_1') { create_client.content_types(skip: 3, limit: 2) }
58
+ array_page_2 = vcr('array_page_2') { array_page_1.next_page }
59
59
 
60
- expect( array_page_2 ).to be_a Contentful::Array
61
- expect( array_page_2.limit ).to eq 2
62
- expect( array_page_2.skip ).to eq 5
60
+ expect(array_page_2).to be_a Contentful::Array
61
+ expect(array_page_2.limit).to eq 2
62
+ expect(array_page_2.skip).to eq 5
63
63
  end
64
64
 
65
65
  it 'will return false if #request not available' do
66
- expect( Contentful::Array.new({}).reload ).to be_false
66
+ expect(Contentful::Array.new({}).reload).to be_false
67
67
  end
68
68
  end
69
69
  end
data/spec/asset_spec.rb CHANGED
@@ -1,62 +1,62 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Contentful::Asset do
4
- let(:asset){ vcr('asset'){ create_client.asset('nyancat') } }
4
+ let(:asset) { vcr('asset') { create_client.asset('nyancat') } }
5
5
 
6
6
  describe 'SystemProperties' do
7
7
  it 'has a #sys getter returning a hash with symbol keys' do
8
- expect( asset.sys ).to be_a Hash
9
- expect( asset.sys.keys.sample ).to be_a Symbol
8
+ expect(asset.sys).to be_a Hash
9
+ expect(asset.sys.keys.sample).to be_a Symbol
10
10
  end
11
11
 
12
12
  it 'has #id' do
13
- expect( asset.id ).to eq "nyancat"
13
+ expect(asset.id).to eq 'nyancat'
14
14
  end
15
15
 
16
16
  it 'has #type' do
17
- expect( asset.type ).to eq "Asset"
17
+ expect(asset.type).to eq 'Asset'
18
18
  end
19
19
 
20
20
  it 'has #space' do
21
- expect( asset.space ).to be_a Contentful::Link
21
+ expect(asset.space).to be_a Contentful::Link
22
22
  end
23
23
 
24
24
  it 'has #created_at' do
25
- expect( asset.created_at ).to be_a DateTime
25
+ expect(asset.created_at).to be_a DateTime
26
26
  end
27
27
 
28
28
  it 'has #updated_at' do
29
- expect( asset.updated_at ).to be_a DateTime
29
+ expect(asset.updated_at).to be_a DateTime
30
30
  end
31
31
 
32
32
  it 'has #revision' do
33
- expect( asset.revision ).to eq 1
33
+ expect(asset.revision).to eq 1
34
34
  end
35
35
  end
36
36
 
37
37
  describe 'Fields' do
38
38
  it 'has #title' do
39
- expect( asset.title ).to eq "Nyan Cat"
39
+ expect(asset.title).to eq 'Nyan Cat'
40
40
  end
41
41
 
42
42
  it 'could have #description' do
43
- expect( asset ).to respond_to :description
43
+ expect(asset).to respond_to :description
44
44
  end
45
45
 
46
46
  it 'has #file' do
47
- expect( asset.file ).to be_a Contentful::File
47
+ expect(asset.file).to be_a Contentful::File
48
48
  end
49
49
  end
50
50
 
51
51
  describe '#image_url' do
52
52
  it 'returns #url of #file without parameter' do
53
- expect( asset.image_url ).to eq asset.file.url
53
+ expect(asset.image_url).to eq asset.file.url
54
54
  end
55
55
 
56
56
  it 'adds image options if given' do
57
57
  url = asset.image_url(width: 100, format: 'jpg', quality: 50)
58
58
  expect(url).to include asset.file.url
59
- expect(url).to include "?w=100&fm=jpg&q=50"
59
+ expect(url).to include '?w=100&fm=jpg&q=50'
60
60
  end
61
61
  end
62
62
  end