contentful 1.2.2 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +21 -0
- data/LICENSE.txt +1 -0
- data/README.md +8 -0
- data/contentful.gemspec +2 -1
- data/examples/custom_classes.rb +23 -26
- data/examples/raise_errors.rb +2 -2
- data/lib/contentful.rb +0 -1
- data/lib/contentful/array.rb +27 -19
- data/lib/contentful/array_like.rb +51 -0
- data/lib/contentful/asset.rb +43 -11
- data/lib/contentful/base_resource.rb +87 -0
- data/lib/contentful/client.rb +43 -34
- data/lib/contentful/coercions.rb +116 -0
- data/lib/contentful/content_type.rb +23 -8
- data/lib/contentful/content_type_cache.rb +26 -0
- data/lib/contentful/deleted_asset.rb +2 -5
- data/lib/contentful/deleted_entry.rb +2 -5
- data/lib/contentful/entry.rb +55 -33
- data/lib/contentful/error.rb +1 -1
- data/lib/contentful/field.rb +37 -9
- data/lib/contentful/fields_resource.rb +115 -0
- data/lib/contentful/file.rb +7 -8
- data/lib/contentful/link.rb +3 -6
- data/lib/contentful/locale.rb +6 -6
- data/lib/contentful/location.rb +7 -5
- data/lib/contentful/resource_builder.rb +72 -226
- data/lib/contentful/space.rb +16 -6
- data/lib/contentful/support.rb +41 -3
- data/lib/contentful/sync_page.rb +17 -10
- data/lib/contentful/version.rb +1 -1
- data/spec/array_spec.rb +4 -8
- data/spec/client_class_spec.rb +12 -23
- data/spec/client_configuration_spec.rb +13 -23
- data/spec/content_type_spec.rb +0 -5
- data/spec/entry_spec.rb +130 -125
- data/spec/error_requests_spec.rb +1 -1
- data/spec/field_spec.rb +0 -5
- data/spec/file_spec.rb +0 -5
- data/spec/fixtures/vcr_cassettes/entry.yml +54 -64
- data/spec/fixtures/vcr_cassettes/entry/include_resolution.yml +101 -0
- data/spec/fixtures/vcr_cassettes/entry/marshall.yml +227 -251
- data/spec/fixtures/vcr_cassettes/entry/raw.yml +88 -124
- data/spec/fixtures/vcr_cassettes/entry_locales.yml +56 -74
- data/spec/fixtures/vcr_cassettes/human.yml +63 -40
- data/spec/fixtures/vcr_cassettes/location.yml +99 -211
- data/spec/fixtures/vcr_cassettes/multi_locale_array_reference.yml +12 -16
- data/spec/fixtures/vcr_cassettes/not_found.yml +26 -21
- data/spec/fixtures/vcr_cassettes/nyancat.yml +53 -63
- data/spec/fixtures/vcr_cassettes/ratelimit.yml +1 -1
- data/spec/fixtures/vcr_cassettes/reloaded_entry.yml +54 -64
- data/spec/fixtures/vcr_cassettes/unauthorized.yml +1 -1
- data/spec/fixtures/vcr_cassettes/unavailable.yml +27 -15
- data/spec/link_spec.rb +3 -2
- data/spec/locale_spec.rb +0 -5
- data/spec/location_spec.rb +1 -6
- data/spec/request_spec.rb +3 -2
- data/spec/resource_building_spec.rb +10 -7
- data/spec/response_spec.rb +1 -1
- data/spec/space_spec.rb +0 -5
- data/spec/spec_helper.rb +3 -0
- data/spec/support/json_responses.rb +3 -3
- data/spec/sync_page_spec.rb +1 -6
- data/spec/sync_spec.rb +11 -7
- metadata +69 -20
- data/examples/dynamic_entries.rb +0 -124
- data/examples/resource_mapping.rb +0 -32
- data/lib/contentful/constants.rb +0 -504
- data/lib/contentful/dynamic_entry.rb +0 -57
- data/lib/contentful/resource.rb +0 -239
- data/lib/contentful/resource/array_like.rb +0 -39
- data/lib/contentful/resource/asset_fields.rb +0 -58
- data/lib/contentful/resource/custom_resource.rb +0 -29
- data/lib/contentful/resource/fields.rb +0 -73
- data/lib/contentful/resource/system_properties.rb +0 -55
- data/spec/coercions_spec.rb +0 -23
- data/spec/dynamic_entry_spec.rb +0 -75
- data/spec/resource_spec.rb +0 -79
@@ -1,55 +0,0 @@
|
|
1
|
-
module Contentful
|
2
|
-
module Resource
|
3
|
-
# Adds the feature to have system properties to a Resource.
|
4
|
-
module SystemProperties
|
5
|
-
# @private
|
6
|
-
# Coercions for System Properties to native types
|
7
|
-
SYS_COERCIONS = {
|
8
|
-
type: :string,
|
9
|
-
id: :string,
|
10
|
-
space: nil,
|
11
|
-
contentType: nil,
|
12
|
-
linkType: :string,
|
13
|
-
revision: :integer,
|
14
|
-
createdAt: :date,
|
15
|
-
updatedAt: :date,
|
16
|
-
locale: :string
|
17
|
-
}
|
18
|
-
attr_reader :sys
|
19
|
-
|
20
|
-
# @private
|
21
|
-
def initialize(object = nil, *)
|
22
|
-
super
|
23
|
-
@sys = object ? extract_from_object(object['sys'], :sys) : {}
|
24
|
-
end
|
25
|
-
|
26
|
-
# @private
|
27
|
-
def inspect(info = nil)
|
28
|
-
if sys.empty?
|
29
|
-
super(info)
|
30
|
-
else
|
31
|
-
super("#{info} @sys=#{sys.inspect}")
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
# @private
|
36
|
-
module ClassMethods
|
37
|
-
# @private
|
38
|
-
def sys_coercions
|
39
|
-
SYS_COERCIONS
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
# @private
|
44
|
-
def self.included(base)
|
45
|
-
base.extend(ClassMethods)
|
46
|
-
|
47
|
-
base.sys_coercions.keys.each do |name|
|
48
|
-
base.send :define_method, Contentful::Support.snakify(name) do
|
49
|
-
sys[name.to_sym]
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
data/spec/coercions_spec.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe 'Coercion Examples' do
|
4
|
-
let(:entry) { vcr('entry') { create_client.entry 'nyancat' } }
|
5
|
-
|
6
|
-
it 'converts contentful to ruby DateTime objects' do
|
7
|
-
expect(entry.created_at).to be_a DateTime
|
8
|
-
expect(entry.created_at.day).to eq 27
|
9
|
-
end
|
10
|
-
|
11
|
-
describe 'custom coercion' do
|
12
|
-
class TestCar
|
13
|
-
include Contentful::Resource
|
14
|
-
|
15
|
-
property :parts, ->(v) { Array(v) unless v }
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'can use proc' do
|
19
|
-
car = TestCar.new("parts" => nil)
|
20
|
-
expect(car.parts).to be_empty
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
data/spec/dynamic_entry_spec.rb
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Contentful::DynamicEntry do
|
4
|
-
let(:content_type) { vcr('content_type') { create_client.content_type 'cat' } }
|
5
|
-
|
6
|
-
it 'create as a class' do
|
7
|
-
expect(Contentful::DynamicEntry.create(content_type)).to be_instance_of Class
|
8
|
-
end
|
9
|
-
|
10
|
-
it 'also works with raw json object input as argument' do
|
11
|
-
expect(Contentful::DynamicEntry.create(json_fixture('content_type'))).to be_instance_of Class
|
12
|
-
end
|
13
|
-
|
14
|
-
it 'should create Contentful::DynamicEntry instances' do
|
15
|
-
expect(Contentful::DynamicEntry.create(content_type).new(json_fixture('nyancat'))).to \
|
16
|
-
be_a Contentful::DynamicEntry
|
17
|
-
end
|
18
|
-
|
19
|
-
describe 'Example Entry Class' do
|
20
|
-
before do
|
21
|
-
NyanCatEntry = Contentful::DynamicEntry.create(content_type)
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'defines getters for entry fields' do
|
25
|
-
nyancat = NyanCatEntry.new json_fixture('nyancat')
|
26
|
-
expect(nyancat.color).to eq 'rainbow'
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'automatically coerces types' do
|
30
|
-
nyancat = NyanCatEntry.new json_fixture('nyancat')
|
31
|
-
expect(nyancat.birthday).to be_a DateTime
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
describe 'issues' do
|
36
|
-
describe 'JSON Fields should not be treated as locale data - #96' do
|
37
|
-
before do
|
38
|
-
vcr('entry/json_objects_client') {
|
39
|
-
@client = create_client(
|
40
|
-
space: 'h425t6gef30p',
|
41
|
-
access_token: '278f7aa72f2eb90c0e002d60f85bf2144c925acd2d37dd990d3ca274f25076cf',
|
42
|
-
dynamic_entries: :auto
|
43
|
-
)
|
44
|
-
|
45
|
-
}
|
46
|
-
vcr('entry/json_objects') {
|
47
|
-
@entry = @client.entries.first
|
48
|
-
}
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'only has default locale' do
|
52
|
-
expect(@entry.instance_variable_get(:@fields).keys).to eq ['en-US']
|
53
|
-
end
|
54
|
-
|
55
|
-
it 'can obtain all values properly' do
|
56
|
-
expect(@entry.name).to eq('Test')
|
57
|
-
expect(@entry.object_test).to eq({
|
58
|
-
null: nil,
|
59
|
-
text: 'some text',
|
60
|
-
array: [1, 2, 3],
|
61
|
-
number: 123,
|
62
|
-
boolean: true,
|
63
|
-
object: {
|
64
|
-
null: nil,
|
65
|
-
text: 'bar',
|
66
|
-
array: [1, 2, 3],
|
67
|
-
number: 123,
|
68
|
-
boolean: false,
|
69
|
-
object: {foo: 'bar'}
|
70
|
-
}
|
71
|
-
})
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
data/spec/resource_spec.rb
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Contentful::Resource do
|
4
|
-
it 'must be included to get functionality' do
|
5
|
-
c = Class.new
|
6
|
-
c.send :include, Contentful::Resource
|
7
|
-
expect(c).to respond_to :property_coercions
|
8
|
-
end
|
9
|
-
|
10
|
-
describe 'creation' do
|
11
|
-
it 'must be initialized with a (hash) object' do
|
12
|
-
expect do
|
13
|
-
Contentful::ContentType.new json_fixture('nyancat')
|
14
|
-
end.not_to raise_error
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'can deal with invalid objects' do
|
18
|
-
expect do
|
19
|
-
Contentful::ContentType.new
|
20
|
-
end.not_to raise_error
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
describe 'custom resource' do
|
25
|
-
class TestCat
|
26
|
-
include Contentful::Resource
|
27
|
-
|
28
|
-
property :name
|
29
|
-
property :likes
|
30
|
-
property :color
|
31
|
-
property :bestFriend
|
32
|
-
property :birthday
|
33
|
-
property :lives
|
34
|
-
property :image
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'can create a custom resource' do
|
38
|
-
cat = TestCat.new("name" => "foobar")
|
39
|
-
expect(cat).to respond_to(:name)
|
40
|
-
expect(cat.name).to eq "foobar"
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'can create a custom resource from API' do
|
44
|
-
vcr('entry') {
|
45
|
-
cat = TestCat.new create_client(raw_mode: true).entry('nyancat').object["fields"]
|
46
|
-
expect(cat.name).to eq "Nyan Cat"
|
47
|
-
}
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
describe '#request' do
|
52
|
-
it 'will return a request object, if it has been initialized with one' do
|
53
|
-
request = Contentful::Request.new(nil, 'content_types')
|
54
|
-
resource = Contentful::ContentType.new({}, request)
|
55
|
-
expect(resource.request).to eq request
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
describe '#reload' do
|
60
|
-
let(:client) { create_client }
|
61
|
-
let(:entry) { vcr('entry') { client.entry 'nyancat' } }
|
62
|
-
|
63
|
-
it 'triggers the request connected to the resource (again)' do
|
64
|
-
stub(entry.request).get
|
65
|
-
entry.reload
|
66
|
-
expect(entry.request).to have_received.get
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'the reloaded resource is different from the original one' do
|
70
|
-
reloaded_entry = vcr('reloaded_entry') { entry.reload }
|
71
|
-
expect(reloaded_entry).to be_a Contentful::Entry
|
72
|
-
expect(reloaded_entry).not_to be entry
|
73
|
-
end
|
74
|
-
|
75
|
-
it 'will return false if #request not available' do
|
76
|
-
expect(Contentful::ContentType.new({}).reload).to be_falsey
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|