contentful 1.2.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +21 -0
  3. data/LICENSE.txt +1 -0
  4. data/README.md +8 -0
  5. data/contentful.gemspec +2 -1
  6. data/examples/custom_classes.rb +23 -26
  7. data/examples/raise_errors.rb +2 -2
  8. data/lib/contentful.rb +0 -1
  9. data/lib/contentful/array.rb +27 -19
  10. data/lib/contentful/array_like.rb +51 -0
  11. data/lib/contentful/asset.rb +43 -11
  12. data/lib/contentful/base_resource.rb +87 -0
  13. data/lib/contentful/client.rb +43 -34
  14. data/lib/contentful/coercions.rb +116 -0
  15. data/lib/contentful/content_type.rb +23 -8
  16. data/lib/contentful/content_type_cache.rb +26 -0
  17. data/lib/contentful/deleted_asset.rb +2 -5
  18. data/lib/contentful/deleted_entry.rb +2 -5
  19. data/lib/contentful/entry.rb +55 -33
  20. data/lib/contentful/error.rb +1 -1
  21. data/lib/contentful/field.rb +37 -9
  22. data/lib/contentful/fields_resource.rb +115 -0
  23. data/lib/contentful/file.rb +7 -8
  24. data/lib/contentful/link.rb +3 -6
  25. data/lib/contentful/locale.rb +6 -6
  26. data/lib/contentful/location.rb +7 -5
  27. data/lib/contentful/resource_builder.rb +72 -226
  28. data/lib/contentful/space.rb +16 -6
  29. data/lib/contentful/support.rb +41 -3
  30. data/lib/contentful/sync_page.rb +17 -10
  31. data/lib/contentful/version.rb +1 -1
  32. data/spec/array_spec.rb +4 -8
  33. data/spec/client_class_spec.rb +12 -23
  34. data/spec/client_configuration_spec.rb +13 -23
  35. data/spec/content_type_spec.rb +0 -5
  36. data/spec/entry_spec.rb +130 -125
  37. data/spec/error_requests_spec.rb +1 -1
  38. data/spec/field_spec.rb +0 -5
  39. data/spec/file_spec.rb +0 -5
  40. data/spec/fixtures/vcr_cassettes/entry.yml +54 -64
  41. data/spec/fixtures/vcr_cassettes/entry/include_resolution.yml +101 -0
  42. data/spec/fixtures/vcr_cassettes/entry/marshall.yml +227 -251
  43. data/spec/fixtures/vcr_cassettes/entry/raw.yml +88 -124
  44. data/spec/fixtures/vcr_cassettes/entry_locales.yml +56 -74
  45. data/spec/fixtures/vcr_cassettes/human.yml +63 -40
  46. data/spec/fixtures/vcr_cassettes/location.yml +99 -211
  47. data/spec/fixtures/vcr_cassettes/multi_locale_array_reference.yml +12 -16
  48. data/spec/fixtures/vcr_cassettes/not_found.yml +26 -21
  49. data/spec/fixtures/vcr_cassettes/nyancat.yml +53 -63
  50. data/spec/fixtures/vcr_cassettes/ratelimit.yml +1 -1
  51. data/spec/fixtures/vcr_cassettes/reloaded_entry.yml +54 -64
  52. data/spec/fixtures/vcr_cassettes/unauthorized.yml +1 -1
  53. data/spec/fixtures/vcr_cassettes/unavailable.yml +27 -15
  54. data/spec/link_spec.rb +3 -2
  55. data/spec/locale_spec.rb +0 -5
  56. data/spec/location_spec.rb +1 -6
  57. data/spec/request_spec.rb +3 -2
  58. data/spec/resource_building_spec.rb +10 -7
  59. data/spec/response_spec.rb +1 -1
  60. data/spec/space_spec.rb +0 -5
  61. data/spec/spec_helper.rb +3 -0
  62. data/spec/support/json_responses.rb +3 -3
  63. data/spec/sync_page_spec.rb +1 -6
  64. data/spec/sync_spec.rb +11 -7
  65. metadata +69 -20
  66. data/examples/dynamic_entries.rb +0 -124
  67. data/examples/resource_mapping.rb +0 -32
  68. data/lib/contentful/constants.rb +0 -504
  69. data/lib/contentful/dynamic_entry.rb +0 -57
  70. data/lib/contentful/resource.rb +0 -239
  71. data/lib/contentful/resource/array_like.rb +0 -39
  72. data/lib/contentful/resource/asset_fields.rb +0 -58
  73. data/lib/contentful/resource/custom_resource.rb +0 -29
  74. data/lib/contentful/resource/fields.rb +0 -73
  75. data/lib/contentful/resource/system_properties.rb +0 -55
  76. data/spec/coercions_spec.rb +0 -23
  77. data/spec/dynamic_entry_spec.rb +0 -75
  78. data/spec/resource_spec.rb +0 -79
@@ -1,14 +1,24 @@
1
- require_relative 'resource'
1
+ require_relative 'base_resource'
2
2
  require_relative 'locale'
3
3
 
4
4
  module Contentful
5
5
  # Resource class for Space.
6
6
  # https://www.contentful.com/developers/documentation/content-delivery-api/#spaces
7
- class Space
8
- include Contentful::Resource
9
- include Contentful::Resource::SystemProperties
7
+ class Space < BaseResource
8
+ attr_reader :name, :locales
10
9
 
11
- property :name, :string
12
- property :locales, Locale
10
+ def initialize(item, *)
11
+ super
12
+
13
+ @name = item.fetch('name', nil)
14
+ @locales = item.fetch('locales', []).map { |locale| Locale.new(locale) }
15
+ end
16
+
17
+ # @private
18
+ def reload(client = nil)
19
+ return client.space unless client.nil?
20
+
21
+ false
22
+ end
13
23
  end
14
24
  end
@@ -8,17 +8,55 @@ module Contentful
8
8
  #
9
9
  # @return [String] snake_case_name
10
10
  def snakify(object)
11
- snake = String(object).gsub(/(?<!^)[A-Z]/) { "_#{$&}" }
12
- snake.downcase
11
+ String(object)
12
+ .gsub(/::/, '/')
13
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
14
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
15
+ .tr('-', '_')
16
+ .downcase
13
17
  end
14
18
 
15
19
  # Returns true if resource is localized
16
20
  #
17
- # @return [Boolean]
21
+ # @return [true, false]
18
22
  def localized?(value)
19
23
  return false unless value.is_a? ::Hash
20
24
  value.keys.any? { |possible_locale| Contentful::Constants::KNOWN_LOCALES.include?(possible_locale) }
21
25
  end
26
+
27
+ # Checks if value is a link
28
+ #
29
+ # @param value
30
+ #
31
+ # @return [true, false]
32
+ def link?(value)
33
+ value.is_a?(::Hash) &&
34
+ value.fetch('sys', {}).fetch('type', '') == 'Link'
35
+ end
36
+
37
+ # Checks if value is an array of links
38
+ #
39
+ # @param value
40
+ #
41
+ # @return [true, false]
42
+ def link_array?(value)
43
+ return link?(value[0]) if value.is_a?(::Array) && !value.empty?
44
+
45
+ false
46
+ end
47
+
48
+ # Returns the resource that matches the link
49
+ #
50
+ # @param [Hash] link
51
+ # @param [::Array] includes
52
+ #
53
+ # @return [Hash]
54
+ def resource_for_link(link, includes)
55
+ includes.detect do |i|
56
+ i['sys']['id'] == link['sys']['id'] &&
57
+ i['sys']['type'] == link['sys']['linkType']
58
+ end
59
+ end
22
60
  end
23
61
  end
24
62
  end
@@ -1,18 +1,25 @@
1
- require_relative 'resource'
2
- require_relative 'resource/array_like'
1
+ require_relative 'base_resource'
2
+ require_relative 'array_like'
3
3
 
4
4
  module Contentful
5
5
  # Wrapper Class for Sync results
6
- class SyncPage
7
- attr_reader :sync
6
+ class SyncPage < BaseResource
7
+ include Contentful::ArrayLike
8
8
 
9
- include Contentful::Resource
10
- include Contentful::Resource::SystemProperties
11
- include Contentful::Resource::ArrayLike
9
+ attr_reader :sync, :items, :next_sync_url, :next_page_url
12
10
 
13
- property :items
14
- property :nextSyncUrl
15
- property :nextPageUrl
11
+ def initialize(item, default_locale, *)
12
+ super(item, { default_locale: default_locale }, true)
13
+
14
+ @items = item.fetch('items', [])
15
+ @next_sync_url = item.fetch('nextSyncUrl', nil)
16
+ @next_page_url = item.fetch('nextPageUrl', nil)
17
+ end
18
+
19
+ # @private
20
+ def inspect
21
+ "<#{repr_name} next_sync_url='#{next_sync_url}' last_page=#{last_page?}>"
22
+ end
16
23
 
17
24
  # Requests next sync page from API
18
25
  #
@@ -1,5 +1,5 @@
1
1
  # Contentful Namespace
2
2
  module Contentful
3
3
  # Gem Version
4
- VERSION = '1.2.2'
4
+ VERSION = '2.0.0'
5
5
  end
@@ -1,7 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Contentful::Array do
4
- let(:array) { vcr('array') { create_client.content_types } }
4
+ let(:client) { create_client }
5
+ let(:array) { vcr('array') { client.content_types } }
5
6
 
6
7
  describe 'SystemProperties' do
7
8
  it 'has a #sys getter returning a hash with symbol keys' do
@@ -15,11 +16,6 @@ describe Contentful::Array do
15
16
  end
16
17
 
17
18
  describe 'Properties' do
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
21
- end
22
-
23
19
  it 'has #total' do
24
20
  expect(array.total).to eq 4
25
21
  end
@@ -34,7 +30,7 @@ describe Contentful::Array do
34
30
 
35
31
  it 'has #items which contain resources' do
36
32
  expect(array.items).to be_a Array
37
- expect(array.items.sample).to be_a Contentful::Resource
33
+ expect(array.items.sample).to be_a Contentful::BaseResource
38
34
  end
39
35
  end
40
36
 
@@ -55,7 +51,7 @@ describe Contentful::Array do
55
51
  describe '#next_page' do
56
52
  it 'requests more of the same content from the server, using its limit and skip values' do
57
53
  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 }
54
+ array_page_2 = vcr('array_page_2') { array_page_1.next_page(client) }
59
55
 
60
56
  expect(array_page_2).to be_a Contentful::Array
61
57
  expect(array_page_2.limit).to eq 2
@@ -6,50 +6,39 @@ describe Contentful::Client do
6
6
  let(:proxy_client) { create_client(proxy_host: '183.207.232.194',
7
7
  proxy_port: 8080,
8
8
  secure: false) }
9
- let(:request) do
10
- Contentful::Request.new(nil, '/content_types', nil, 'cat')
11
- end
9
+ let(:request) { Contentful::Request.new(nil, '/content_types', nil, 'cat') }
12
10
 
13
11
  it 'uses #base_url' do
14
- stub(client).base_url { 'https://cdn.contentful.com/spaces/cfexampleapi' }
15
- vcr('content_type') { client.get(request) }
16
- expect(client).to have_received.base_url
12
+ expect(client).to receive(:base_url).and_call_original
13
+
14
+ vcr('content_type') {
15
+ client.get(request)
16
+ }
17
17
  end
18
18
 
19
19
  it 'uses #request_headers' do
20
- stub(client).request_headers do
21
- {
22
- 'User-Agent' => 'RubyContentfulGem/0.1.0',
23
- 'Authorization' => 'Bearer b4c0n73n7fu1',
24
- 'Content-Type' => 'application/vnd.contentful.delivery.v1+json',
25
- }
26
- end
20
+ expect(client).to receive(:request_headers).and_call_original
27
21
  vcr('content_type') { client.get(request) }
28
- expect(client).to have_received.request_headers
29
22
  end
30
23
 
31
24
  it 'uses Request#url' do
32
- stub(request).url { '/content_types/cat' }
25
+ expect(request).to receive(:url).and_call_original
33
26
  vcr('content_type') { client.get(request) }
34
- expect(request).to have_received.url
35
27
  end
36
28
 
37
29
  it 'uses Request#query' do
38
- stub(request).query
30
+ expect(request).to receive(:query).twice.and_call_original
39
31
  vcr('content_type') { client.get(request) }
40
- expect(request).to have_received.query
41
32
  end
42
33
 
43
34
  it 'calls #get_http' do
44
- stub(client.class).get_http { raw_fixture('content_type') }
35
+ expect(client.class).to receive(:get_http).with(client.base_url + request.url, request.query, client.request_headers, client.proxy_params) { raw_fixture('content_type') }
45
36
  client.get(request)
46
- expect(client.class).to have_received.get_http(client.base_url + request.url, request.query, client.request_headers, client.proxy_params)
47
37
  end
48
38
 
49
39
  it 'calls #get_http via proxy' do
50
- stub(proxy_client.class).get_http { raw_fixture('content_type') }
40
+ expect(proxy_client.class).to receive(:get_http).with(proxy_client.base_url + request.url, request.query, proxy_client.request_headers, proxy_client.proxy_params) { raw_fixture('content_type') }
51
41
  proxy_client.get(request)
52
- expect(proxy_client.class).to have_received.get_http(proxy_client.base_url + request.url, request.query, proxy_client.request_headers, proxy_client.proxy_params)
53
42
  expect(proxy_client.proxy_params[:host]).to eq '183.207.232.194'
54
43
  expect(proxy_client.proxy_params[:port]).to eq 8080
55
44
  end
@@ -57,7 +46,7 @@ describe Contentful::Client do
57
46
  describe 'build_resources parameter' do
58
47
  it 'returns Contentful::Resource object if second parameter is true [default]' do
59
48
  res = vcr('content_type') { client.get(request) }
60
- expect(res).to be_a Contentful::Resource
49
+ expect(res).to be_a Contentful::BaseResource
61
50
  end
62
51
 
63
52
  it 'returns a Contentful::Response object if second parameter is not true' do
@@ -34,7 +34,7 @@ describe 'Client Configuration Options' do
34
34
  describe ':raise_errors' do
35
35
  it 'will raise response errors if set to true [default]' do
36
36
  expect_vcr('not found')do
37
- create_client.entry 'not found'
37
+ create_client.content_type 'not found'
38
38
  end.to raise_error Contentful::NotFound
39
39
  end
40
40
 
@@ -42,16 +42,15 @@ describe 'Client Configuration Options' do
42
42
  res = nil
43
43
 
44
44
  expect_vcr('not found')do
45
- res = create_client(raise_errors: false).entry 'not found'
45
+ res = create_client(raise_errors: false).content_type 'not found'
46
46
  end.not_to raise_error
47
47
  expect(res).to be_instance_of Contentful::NotFound
48
48
  end
49
49
  end
50
50
 
51
51
  describe ':dynamic_entries' do
52
- it 'will create static if dynamic_entry_cache is empty' do
53
- entry = vcr('nyancat') { create_client(dynamic_entries: :manual).entry('nyancat') }
54
- expect(entry).not_to be_a Contentful::DynamicEntry
52
+ before :each do
53
+ Contentful::ContentTypeCache.clear!
55
54
  end
56
55
 
57
56
  it 'will create dynamic entries if dynamic_entry_cache is not empty' do
@@ -59,24 +58,22 @@ describe 'Client Configuration Options' do
59
58
  vcr('entry_cache') { client.update_dynamic_entry_cache! }
60
59
  entry = vcr('nyancat') { client.entry('nyancat') }
61
60
 
62
- expect(entry).to be_a Contentful::DynamicEntry
61
+ expect(entry).to be_a Contentful::Entry
63
62
  end
64
63
 
65
64
  context ':auto' do
66
65
  it 'will call update dynamic_entry_cache on start-up' do
67
- client = vcr('entry_cache')do
66
+ vcr('entry_cache') do
68
67
  create_client(dynamic_entries: :auto)
69
68
  end
70
- expect(client.dynamic_entry_cache).not_to be_empty
71
- expect(client.dynamic_entry_cache.values.first.ancestors).to \
72
- include Contentful::DynamicEntry
69
+ expect(Contentful::ContentTypeCache.cache).not_to be_empty
73
70
  end
74
71
  end
75
72
 
76
73
  context ':manual' do
77
74
  it 'will not call #update_dynamic_entry_cache! on start-up' do
78
- client = create_client(dynamic_entries: :manual)
79
- expect(client.dynamic_entry_cache).to be_empty
75
+ create_client(dynamic_entries: :manual)
76
+ expect(Contentful::ContentTypeCache.cache).to be_empty
80
77
  end
81
78
  end
82
79
 
@@ -84,18 +81,15 @@ describe 'Client Configuration Options' do
84
81
  let(:client) { create_client(dynamic_entries: :manual) }
85
82
 
86
83
  it 'will fetch all content_types' do
87
- stub(client).content_types { {} }
84
+ expect(client).to receive(:content_types).with(limit: 1000) { {} }
88
85
  client.update_dynamic_entry_cache!
89
- expect(client).to have_received.content_types(limit: 1000)
90
86
  end
91
87
 
92
88
  it 'will save dynamic entries in @dynamic_entry_cache' do
93
89
  vcr('entry_cache')do
94
90
  client.update_dynamic_entry_cache!
95
91
  end
96
- expect(client.dynamic_entry_cache).not_to be_empty
97
- expect(client.dynamic_entry_cache.values.first.ancestors).to \
98
- include Contentful::DynamicEntry
92
+ expect(Contentful::ContentTypeCache.cache).not_to be_empty
99
93
  end
100
94
  end
101
95
 
@@ -104,12 +98,9 @@ describe 'Client Configuration Options' do
104
98
 
105
99
  it 'can be used to register a dynamic entry manually' do
106
100
  cat = vcr('content_type') { client.content_type 'cat' }
107
- CatEntry = Contentful::DynamicEntry.create(cat)
108
- client.register_dynamic_entry 'cat', CatEntry
101
+ client.register_dynamic_entry 'cat', cat
109
102
 
110
- entry = vcr('nyancat') { client.entry('nyancat') }
111
- expect(entry).to be_a Contentful::DynamicEntry
112
- expect(entry).to be_a CatEntry
103
+ expect(Contentful::ContentTypeCache.cache).not_to be_empty
113
104
  end
114
105
  end
115
106
  end
@@ -197,7 +188,6 @@ describe 'Client Configuration Options' do
197
188
  describe ':entry_mapping' do
198
189
  it 'lets you register your own entry classes for certain entry types' do
199
190
  class Cat < Contentful::Entry
200
- # define methods based on :fields, etc
201
191
  end
202
192
 
203
193
  client = create_client(
@@ -19,11 +19,6 @@ describe Contentful::ContentType do
19
19
  end
20
20
 
21
21
  describe 'Properties' do
22
- it 'has a #properties getter returning a hash with symbol keys' do
23
- expect(content_type.properties).to be_a Hash
24
- expect(content_type.properties.keys.sample).to be_a Symbol
25
- end
26
-
27
22
  it 'has #name' do
28
23
  expect(content_type.name).to eq 'Cat'
29
24
  end
@@ -46,20 +46,20 @@ describe Contentful::Entry do
46
46
 
47
47
  it "contains the entry's fields" do
48
48
  expect(entry.fields[:color]).to eq 'rainbow'
49
- expect(entry.fields[:bestFriend]).to be_a Contentful::Link
49
+ expect(entry.fields[:best_friend]).to be_a Contentful::Entry
50
50
  end
51
51
  end
52
52
 
53
53
  describe 'multiple locales' do
54
54
  it 'can handle multiple locales' do
55
55
  vcr('entry_locales') {
56
- cat = create_client.entries(locale: "*").items.first
57
- expect(cat.fields('en-US')[:name]).to eq "Nyan Cat"
58
- expect(cat.fields('es')[:name]).to eq "Gato Nyan"
56
+ nyancat = create_client.entries(locale: "*", 'sys.id' => 'nyancat').items.first
57
+ expect(nyancat.fields('en-US')[:name]).to eq "Nyan Cat"
58
+ expect(nyancat.fields('tlh')[:name]).to eq "Nyan vIghro'"
59
59
 
60
60
 
61
- expect(cat.fields(:'en-US')[:name]).to eq "Nyan Cat"
62
- expect(cat.fields(:es)[:name]).to eq "Gato Nyan"
61
+ expect(nyancat.fields(:'en-US')[:name]).to eq "Nyan Cat"
62
+ expect(nyancat.fields(:tlh)[:name]).to eq "Nyan vIghro'"
63
63
  }
64
64
  end
65
65
 
@@ -74,10 +74,10 @@ describe Contentful::Entry do
74
74
 
75
75
  it 'can handle entries with multiple locales' do
76
76
  vcr('entry_locales') {
77
- nyancat = create_client.entries(locale: "*").items.first
77
+ nyancat = create_client.entries(locale: "*", 'sys.id' => 'nyancat').items.first
78
78
  expect(nyancat.fields_with_locales[:name].size).to eq(2)
79
79
  expect(nyancat.fields_with_locales[:name][:'en-US']).to eq("Nyan Cat")
80
- expect(nyancat.fields_with_locales[:name][:es]).to eq("Gato Nyan")
80
+ expect(nyancat.fields_with_locales[:name][:tlh]).to eq("Nyan vIghro'")
81
81
  }
82
82
  end
83
83
 
@@ -128,133 +128,51 @@ describe Contentful::Entry do
128
128
  it '#raw' do
129
129
  vcr('entry/raw') {
130
130
  nyancat = create_client.entry('nyancat')
131
- expect(nyancat.raw).to eq(create_client(raw_mode: true).entry('nyancat').object)
131
+ expect(nyancat.raw).to eq(create_client(raw_mode: true).entry('nyancat').object['items'].first)
132
132
  }
133
133
  end
134
134
 
135
- describe 'custom resources' do
136
- class Kategorie < Contentful::Entry
137
- include ::Contentful::Resource::CustomResource
138
-
139
- property :title
140
- property :slug
141
- property :image
142
- property :top
143
- property :subcategories
144
- property :featuredArticles
145
- property :catIntroHead
146
- property :catIntroduction
147
- property :seoText
148
- property :metaKeywords
149
- property :metaDescription
150
- property :metaRobots
151
- end
152
-
153
- it 'maps fields properly' do
154
- vcr('entry/custom_resource') {
155
- entry = create_client(
156
- space: 'g2b4ltw00meh',
157
- dynamic_entries: :auto,
158
- entry_mapping: {
159
- 'kategorie' => Kategorie
160
- }
161
- ).entries.first
162
-
163
- expect(entry).to be_a Kategorie
164
- expect(entry.title).to eq "Some Title"
165
- expect(entry.slug).to eq "/asda.html"
166
- expect(entry.featured_articles.first.is_a?(Contentful::Entry)).to be_truthy
167
- expect(entry.top).to be_truthy
168
- }
169
- end
170
-
171
- describe 'can be marshalled' do
172
- class Cat < Contentful::Entry
173
- include ::Contentful::Resource::CustomResource
174
-
175
- property :name
176
- property :lives
177
- property :bestFriend, Cat
178
- property :catPack
179
- property :image, Contentful::Asset
180
- end
181
-
182
- def test_dump(nyancat)
183
- dump = Marshal.dump(nyancat)
184
- new_cat = Marshal.load(dump)
185
-
186
- # Attributes
187
- expect(new_cat).to be_a Cat
188
- expect(new_cat.name).to eq "Nyan Cat"
189
- expect(new_cat.lives).to eq 1337
190
-
191
- # Single linked objects
192
- expect(new_cat.best_friend).to be_a Cat
193
- expect(new_cat.best_friend.name).to eq "Happy Cat"
135
+ describe 'can be marshalled' do
136
+ def test_dump(nyancat)
137
+ dump = Marshal.dump(nyancat)
138
+ new_cat = Marshal.load(dump)
194
139
 
195
- # Array of linked objects
196
- expect(new_cat.cat_pack.count).to eq 2
197
- expect(new_cat.cat_pack[0].name).to eq "Happy Cat"
198
- expect(new_cat.cat_pack[1].name).to eq "Worried Cat"
140
+ # Attributes
141
+ expect(new_cat).to be_a Contentful::Entry
142
+ expect(new_cat.name).to eq "Nyan Cat"
143
+ expect(new_cat.lives).to eq 1337
199
144
 
200
- # Nested Links
201
- expect(new_cat.best_friend.best_friend).to be_a Cat
202
- expect(new_cat.best_friend.best_friend.name).to eq "Worried Cat"
145
+ # Single linked objects
146
+ expect(new_cat.best_friend).to be_a Contentful::Entry
147
+ expect(new_cat.best_friend.name).to eq "Happy Cat"
203
148
 
204
- # Nested array of linked objects
205
- expect(new_cat.best_friend.cat_pack.count).to eq 2
206
- expect(new_cat.best_friend.cat_pack[0].name).to eq "Nyan Cat"
207
- expect(new_cat.best_friend.cat_pack[1].name).to eq "Worried Cat"
149
+ # Array of linked objects
150
+ expect(new_cat.cat_pack.count).to eq 2
151
+ expect(new_cat.cat_pack[0].name).to eq "Happy Cat"
152
+ expect(new_cat.cat_pack[1].name).to eq "Worried Cat"
208
153
 
209
- # Array of linked objects in a nested array of linked objects
210
- expect(new_cat.cat_pack.first.name).to eq "Happy Cat"
211
- expect(new_cat.cat_pack.first.cat_pack[0].name).to eq "Nyan Cat"
212
- expect(new_cat.cat_pack.first.cat_pack[1].name).to eq "Worried Cat"
154
+ # Nested Links
155
+ expect(new_cat.best_friend.best_friend).to be_a Contentful::Entry
156
+ expect(new_cat.best_friend.best_friend.name).to eq "Nyan Cat"
213
157
 
214
- expect(new_cat.image.file.url).to eq "//images.contentful.com/cfexampleapi/4gp6taAwW4CmSgumq2ekUm/9da0cd1936871b8d72343e895a00d611/Nyan_cat_250px_frame.png"
215
- end
216
-
217
- it 'using entry_mapping' do
218
- vcr('entry/marshall') {
219
- nyancat = create_client(entry_mapping: {'cat' => Cat}).entries(include: 2, 'sys.id' => 'nyancat').first
220
- test_dump(nyancat)
221
- }
222
- end
223
-
224
- it 'using resource_mapping' do
225
- vcr('entry/marshall') {
226
- nyancat = create_client(resource_mapping: {
227
- 'Entry' => ->(_json_object) do
228
- return Cat if _json_object.fetch('sys', {}).fetch('contentType', {}).fetch('sys', {}).fetch('id', nil) == 'cat'
229
- Contentful::Entry
230
- end
231
- }).entries(include: 2, 'sys.id' => 'nyancat').first
232
- test_dump(nyancat)
233
- }
234
- end
158
+ # Asset
159
+ expect(new_cat.image.file.url).to eq "//images.contentful.com/cfexampleapi/4gp6taAwW4CmSgumq2ekUm/9da0cd1936871b8d72343e895a00d611/Nyan_cat_250px_frame.png"
160
+ end
235
161
 
236
- it 'can remarshall an unmarshalled object' do
237
- vcr('entry/marshall') {
238
- nyancat = create_client(resource_mapping: {
239
- 'Entry' => ->(_json_object) do
240
- return Cat if _json_object.fetch('sys', {}).fetch('contentType', {}).fetch('sys', {}).fetch('id', nil) == 'cat'
241
- Contentful::Entry
242
- end
243
- }).entries(include: 2, 'sys.id' => 'nyancat').first
244
-
245
- # The double load/dump is on purpose
246
- test_dump(Marshal.load(Marshal.dump(nyancat)))
247
- }
248
- end
162
+ it 'marshals properly' do
163
+ vcr('entry/marshall') {
164
+ nyancat = create_client(gzip_encoded: false, max_include_resolution_depth: 2).entries(include: 2, 'sys.id' => 'nyancat').first
165
+ test_dump(nyancat)
166
+ }
167
+ end
249
168
 
250
- it 'newly created custom resources have property mappings' do
251
- entry = Cat.new
169
+ it 'can remarshall an unmarshalled object' do
170
+ vcr('entry/marshall') {
171
+ nyancat = create_client(max_include_resolution_depth: 2).entries(include: 2, 'sys.id' => 'nyancat').first
252
172
 
253
- expect(entry).to respond_to :name
254
- expect(entry).to respond_to :lives
255
- expect(entry).to respond_to :best_friend
256
- expect(entry).to respond_to :cat_pack
257
- end
173
+ # The double load/dump is on purpose
174
+ test_dump(Marshal.load(Marshal.dump(nyancat)))
175
+ }
258
176
  end
259
177
  end
260
178
 
@@ -314,15 +232,102 @@ describe Contentful::Entry do
314
232
  end
315
233
  end
316
234
 
235
+ describe 'include resolution' do
236
+ it 'defaults to 20 depth' do
237
+ vcr('entry/include_resolution') {
238
+ entry = create_client.entry('nyancat', include: 2)
239
+
240
+ expect(entry.best_friend.name).to eq 'Happy Cat'
241
+ expect(entry
242
+ .best_friend.best_friend
243
+ .best_friend.best_friend
244
+ .best_friend.best_friend
245
+ .best_friend.best_friend
246
+ .best_friend.best_friend
247
+ .best_friend.best_friend
248
+ .best_friend.best_friend
249
+ .best_friend.best_friend
250
+ .best_friend.best_friend
251
+ .best_friend.best_friend.name).to eq 'Nyan Cat'
252
+
253
+ expect(entry
254
+ .best_friend.best_friend
255
+ .best_friend.best_friend
256
+ .best_friend.best_friend
257
+ .best_friend.best_friend
258
+ .best_friend.best_friend
259
+ .best_friend.best_friend
260
+ .best_friend.best_friend
261
+ .best_friend.best_friend
262
+ .best_friend.best_friend
263
+ .best_friend.best_friend
264
+ .best_friend).to be_a ::Contentful::Link
265
+ }
266
+ end
267
+
268
+ it 'can be configured arbitrarily' do
269
+ vcr('entry/include_resolution') {
270
+ entry = create_client(max_include_resolution_depth: 3).entry('nyancat', include: 2)
271
+
272
+ expect(entry.best_friend.name).to eq 'Happy Cat'
273
+ expect(entry
274
+ .best_friend.best_friend
275
+ .best_friend.name).to eq 'Happy Cat'
276
+ expect(entry
277
+ .best_friend.best_friend
278
+ .best_friend.best_friend).to be_a ::Contentful::Link
279
+ }
280
+ end
281
+ end
282
+
317
283
  describe 'issues' do
318
284
  it 'Symbol/Text field with null values should be serialized as nil - #117' do
319
285
  vcr('entries/issue_117') {
320
- client = create_client(space: '8jbbayggj9gj', access_token: '4ce0108f04e55c76476ba84ab0e6149734db73d67cd1b429323ef67f00977e07', dynamic_entries: :auto)
286
+ client = create_client(space: '8jbbayggj9gj', access_token: '4ce0108f04e55c76476ba84ab0e6149734db73d67cd1b429323ef67f00977e07')
321
287
  entry = client.entries.first
322
288
 
323
289
  expect(entry.nil).to be_nil
324
290
  expect(entry.nil).not_to eq ''
325
291
  }
326
292
  end
293
+
294
+ describe 'JSON Fields should not be treated as locale data - #96' do
295
+ before do
296
+ vcr('entry/json_objects_client') {
297
+ @client = create_client(
298
+ space: 'h425t6gef30p',
299
+ access_token: '278f7aa72f2eb90c0e002d60f85bf2144c925acd2d37dd990d3ca274f25076cf',
300
+ dynamic_entries: :auto
301
+ )
302
+
303
+ }
304
+ vcr('entry/json_objects') {
305
+ @entry = @client.entries.first
306
+ }
307
+ end
308
+
309
+ it 'only has default locale' do
310
+ expect(@entry.locales).to eq ['en-US']
311
+ end
312
+
313
+ it 'can obtain all values properly' do
314
+ expect(@entry.name).to eq('Test')
315
+ expect(@entry.object_test).to eq({
316
+ null: nil,
317
+ text: 'some text',
318
+ array: [1, 2, 3],
319
+ number: 123,
320
+ boolean: true,
321
+ object: {
322
+ null: nil,
323
+ text: 'bar',
324
+ array: [1, 2, 3],
325
+ number: 123,
326
+ boolean: false,
327
+ object: {foo: 'bar'}
328
+ }
329
+ })
330
+ end
331
+ end
327
332
  end
328
333
  end