contentful 0.1.0

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 (133) hide show
  1. data/.README.md.swp +0 -0
  2. data/.gitignore +2 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +10 -0
  5. data/ChangeLog.md +3 -0
  6. data/Gemfile +3 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +193 -0
  9. data/Rakefile +30 -0
  10. data/contentful.gemspec +31 -0
  11. data/do_request.sh +5 -0
  12. data/doc/Contentful.html +131 -0
  13. data/doc/Contentful/AccessDenied.html +158 -0
  14. data/doc/Contentful/Array.html +346 -0
  15. data/doc/Contentful/Asset.html +315 -0
  16. data/doc/Contentful/BadRequest.html +158 -0
  17. data/doc/Contentful/Client.html +1407 -0
  18. data/doc/Contentful/ContentType.html +183 -0
  19. data/doc/Contentful/DynamicEntry.html +333 -0
  20. data/doc/Contentful/Entry.html +198 -0
  21. data/doc/Contentful/Error.html +413 -0
  22. data/doc/Contentful/Field.html +161 -0
  23. data/doc/Contentful/File.html +160 -0
  24. data/doc/Contentful/Link.html +275 -0
  25. data/doc/Contentful/Locale.html +161 -0
  26. data/doc/Contentful/NotFound.html +158 -0
  27. data/doc/Contentful/Request.html +669 -0
  28. data/doc/Contentful/Resource.html +606 -0
  29. data/doc/Contentful/Resource/AssetFields.html +413 -0
  30. data/doc/Contentful/Resource/AssetFields/ClassMethods.html +174 -0
  31. data/doc/Contentful/Resource/ClassMethods.html +271 -0
  32. data/doc/Contentful/Resource/Fields.html +398 -0
  33. data/doc/Contentful/Resource/Fields/ClassMethods.html +187 -0
  34. data/doc/Contentful/Resource/SystemProperties.html +444 -0
  35. data/doc/Contentful/Resource/SystemProperties/ClassMethods.html +174 -0
  36. data/doc/Contentful/ResourceBuilder.html +1400 -0
  37. data/doc/Contentful/Response.html +546 -0
  38. data/doc/Contentful/ServerError.html +158 -0
  39. data/doc/Contentful/Space.html +183 -0
  40. data/doc/Contentful/Support.html +198 -0
  41. data/doc/Contentful/Unauthorized.html +158 -0
  42. data/doc/Contentful/UnparsableJson.html +158 -0
  43. data/doc/Contentful/UnparsableResource.html +158 -0
  44. data/doc/_index.html +410 -0
  45. data/doc/class_list.html +54 -0
  46. data/doc/css/common.css +1 -0
  47. data/doc/css/full_list.css +57 -0
  48. data/doc/css/style.css +338 -0
  49. data/doc/file.README.html +214 -0
  50. data/doc/file_list.html +56 -0
  51. data/doc/frames.html +26 -0
  52. data/doc/index.html +214 -0
  53. data/doc/js/app.js +219 -0
  54. data/doc/js/full_list.js +178 -0
  55. data/doc/js/jquery.js +4 -0
  56. data/doc/method_list.html +533 -0
  57. data/doc/top-level-namespace.html +112 -0
  58. data/examples/custom_classes.rb +43 -0
  59. data/examples/dynamic_entries.rb +126 -0
  60. data/examples/example_queries.rb +27 -0
  61. data/examples/raise_errors.rb +22 -0
  62. data/examples/raw_mode.rb +15 -0
  63. data/examples/resource_mapping.rb +33 -0
  64. data/lib/contentful.rb +3 -0
  65. data/lib/contentful/array.rb +47 -0
  66. data/lib/contentful/asset.rb +34 -0
  67. data/lib/contentful/client.rb +193 -0
  68. data/lib/contentful/content_type.rb +16 -0
  69. data/lib/contentful/dynamic_entry.rb +54 -0
  70. data/lib/contentful/entry.rb +12 -0
  71. data/lib/contentful/error.rb +52 -0
  72. data/lib/contentful/field.rb +16 -0
  73. data/lib/contentful/file.rb +13 -0
  74. data/lib/contentful/link.rb +20 -0
  75. data/lib/contentful/locale.rb +12 -0
  76. data/lib/contentful/location.rb +12 -0
  77. data/lib/contentful/request.rb +37 -0
  78. data/lib/contentful/resource.rb +146 -0
  79. data/lib/contentful/resource/asset_fields.rb +50 -0
  80. data/lib/contentful/resource/fields.rb +39 -0
  81. data/lib/contentful/resource/system_properties.rb +48 -0
  82. data/lib/contentful/resource_builder.rb +197 -0
  83. data/lib/contentful/response.rb +64 -0
  84. data/lib/contentful/space.rb +14 -0
  85. data/lib/contentful/support.rb +18 -0
  86. data/lib/contentful/version.rb +3 -0
  87. data/spec/array_spec.rb +69 -0
  88. data/spec/asset_spec.rb +62 -0
  89. data/spec/auto_includes_spec.rb +12 -0
  90. data/spec/client_class_spec.rb +59 -0
  91. data/spec/client_configuration_spec.rb +197 -0
  92. data/spec/coercions_spec.rb +10 -0
  93. data/spec/content_type_spec.rb +44 -0
  94. data/spec/dynamic_entry_spec.rb +34 -0
  95. data/spec/entry_spec.rb +53 -0
  96. data/spec/error_class_spec.rb +60 -0
  97. data/spec/error_requests_spec.rb +32 -0
  98. data/spec/field_spec.rb +36 -0
  99. data/spec/file_spec.rb +28 -0
  100. data/spec/fixtures/json_responses/content_type.json +83 -0
  101. data/spec/fixtures/json_responses/not_found.json +13 -0
  102. data/spec/fixtures/json_responses/nyancat.json +48 -0
  103. data/spec/fixtures/json_responses/unparsable.json +13 -0
  104. data/spec/fixtures/vcr_cassettes/array.yml +288 -0
  105. data/spec/fixtures/vcr_cassettes/array_page_1.yml +106 -0
  106. data/spec/fixtures/vcr_cassettes/array_page_2.yml +73 -0
  107. data/spec/fixtures/vcr_cassettes/asset.yml +96 -0
  108. data/spec/fixtures/vcr_cassettes/bad_request.yml +76 -0
  109. data/spec/fixtures/vcr_cassettes/content_type.yml +147 -0
  110. data/spec/fixtures/vcr_cassettes/entries.yml +561 -0
  111. data/spec/fixtures/vcr_cassettes/entry.yml +112 -0
  112. data/spec/fixtures/vcr_cassettes/entry_cache.yml +288 -0
  113. data/spec/fixtures/vcr_cassettes/field.yml +147 -0
  114. data/spec/fixtures/vcr_cassettes/locale.yml +81 -0
  115. data/spec/fixtures/vcr_cassettes/location.yml +305 -0
  116. data/spec/fixtures/vcr_cassettes/not_found.yml +71 -0
  117. data/spec/fixtures/vcr_cassettes/nyancat.yml +112 -0
  118. data/spec/fixtures/vcr_cassettes/nyancat_include.yml +112 -0
  119. data/spec/fixtures/vcr_cassettes/reloaded_entry.yml +112 -0
  120. data/spec/fixtures/vcr_cassettes/space.yml +81 -0
  121. data/spec/fixtures/vcr_cassettes/unauthorized.yml +64 -0
  122. data/spec/link_spec.rb +40 -0
  123. data/spec/locale_spec.rb +20 -0
  124. data/spec/location_spec.rb +30 -0
  125. data/spec/request_spec.rb +48 -0
  126. data/spec/resource_spec.rb +52 -0
  127. data/spec/response_spec.rb +50 -0
  128. data/spec/space_spec.rb +36 -0
  129. data/spec/spec_helper.rb +6 -0
  130. data/spec/support/client.rb +6 -0
  131. data/spec/support/json_responses.rb +11 -0
  132. data/spec/support/vcr.rb +16 -0
  133. metadata +374 -0
@@ -0,0 +1,10 @@
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
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe Contentful::ContentType do
4
+ let(:content_type){ vcr('content_type'){ create_client.content_type 'cat' } }
5
+
6
+ describe 'SystemProperties' do
7
+ it 'has a #sys getter returning a hash with symbol keys' do
8
+ expect( content_type.sys ).to be_a Hash
9
+ expect( content_type.sys.keys.sample ).to be_a Symbol
10
+ end
11
+
12
+ it 'has #id' do
13
+ expect( content_type.id ).to eq "cat"
14
+ end
15
+
16
+ it 'has #type' do
17
+ expect( content_type.type ).to eq "ContentType"
18
+ end
19
+ end
20
+
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
+ it 'has #name' do
28
+ expect( content_type.name ).to eq "Cat"
29
+ end
30
+
31
+ it 'has #description' do
32
+ expect( content_type.description ).to eq "Meow."
33
+ end
34
+
35
+ it 'has #fields' do
36
+ expect( content_type.fields ).to be_a Array
37
+ expect( content_type.fields.first ).to be_a Contentful::Field
38
+ end
39
+
40
+ it 'could have #display_field' do
41
+ expect( content_type ).to respond_to :display_field
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,34 @@
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{
21
+ NyanCatEntry = Contentful::DynamicEntry.create(content_type)
22
+ }
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
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe Contentful::ContentType do
4
+ let(:entry){ vcr('entry'){ create_client.entry 'nyancat' } }
5
+
6
+ describe 'SystemProperties' do
7
+ it 'has a #sys getter returning a hash with symbol keys' do
8
+ expect( entry.sys ).to be_a Hash
9
+ expect( entry.sys.keys.sample ).to be_a Symbol
10
+ end
11
+
12
+ it 'has #id' do
13
+ expect( entry.id ).to eq "nyancat"
14
+ end
15
+
16
+ it 'has #type' do
17
+ expect( entry.type ).to eq "Entry"
18
+ end
19
+
20
+ it 'has #space' do
21
+ expect( entry.space ).to be_a Contentful::Link
22
+ end
23
+
24
+ it 'has #content_type' do
25
+ expect( entry.content_type ).to be_a Contentful::Link
26
+ end
27
+
28
+ it 'has #created_at' do
29
+ expect( entry.created_at ).to be_a DateTime
30
+ end
31
+
32
+ it 'has #updated_at' do
33
+ expect( entry.updated_at ).to be_a DateTime
34
+ end
35
+
36
+ it 'has #revision' do
37
+ expect( entry.revision ).to eq 5
38
+ end
39
+ end
40
+
41
+ describe 'Fields' do
42
+ it 'has a #fields getter returning a hash with symbol keys' do
43
+ expect( entry.sys ).to be_a Hash
44
+ expect( entry.sys.keys.sample ).to be_a Symbol
45
+ end
46
+
47
+ it "contains the entry's fields" do
48
+ expect( entry.fields[:color] ).to eq "rainbow"
49
+ expect( entry.fields[:bestFriend] ).to be_a Contentful::Link
50
+ end
51
+ end
52
+
53
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe Contentful::Error do
4
+ let(:r){ Contentful::Response.new raw_fixture("not_found") }
5
+
6
+
7
+ describe '#response' do
8
+ it 'returns the response the error has been initialized with' do
9
+ expect( Contentful::Error.new(r).response ).to be r
10
+ end
11
+ end
12
+
13
+ describe '#message' do
14
+ it 'returns the message found in the response json' do
15
+ expect( Contentful::Error.new(r).message ).not_to be_nil
16
+ expect( Contentful::Error.new(r).message ).to \
17
+ eq json_fixture("not_found")["message"]
18
+ end
19
+ end
20
+
21
+
22
+ describe Contentful::UnparsableJson do
23
+ describe '#message' do
24
+ it 'returns the json parser\'s message' do
25
+ uj = Contentful::Response.new raw_fixture("unparsable")
26
+ expect( Contentful::UnparsableJson.new(uj).message ).to \
27
+ include "unexpected token"
28
+ end
29
+ end
30
+ end
31
+
32
+
33
+ describe '.[]' do
34
+ it 'returns NotFound error class for 404' do
35
+ expect( Contentful::Error[404] ).to eq Contentful::NotFound
36
+ end
37
+
38
+ it 'returns BadRequest error class for 400' do
39
+ expect( Contentful::Error[400] ).to eq Contentful::BadRequest
40
+ end
41
+
42
+ it 'returns AccessDenied error class for 403' do
43
+ expect( Contentful::Error[403] ).to eq Contentful::AccessDenied
44
+ end
45
+
46
+ it 'returns Unauthorized error class for 401' do
47
+ expect( Contentful::Error[401] ).to eq Contentful::Unauthorized
48
+ end
49
+
50
+ it 'returns ServerError error class for 500' do
51
+ expect( Contentful::Error[500] ).to eq Contentful::ServerError
52
+ end
53
+
54
+ it 'returns generic error class for any other value' do
55
+ expect( Contentful::Error[nil] ).to eq Contentful::Error
56
+ expect( Contentful::Error[200] ).to eq Contentful::Error
57
+ end
58
+ end
59
+
60
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe 'Error Requests' do
5
+ it 'will return 404 (Unauthorized) if resource not found' do
6
+ expect_vcr("not found"){
7
+ create_client.entry 'not found'
8
+ }.to raise_error(Contentful::NotFound)
9
+ end
10
+
11
+ it 'will return 400 (BadRequest) if invalid parameters have been passed' do
12
+ expect_vcr("bad request"){
13
+ create_client.entries(some: 'parameter')
14
+ }.to raise_error(Contentful::BadRequest)
15
+ end
16
+
17
+ it 'will return 403 (AccessDenied) if ...' do
18
+ pending
19
+ end
20
+
21
+ it 'will return 401 (Unauthorized) if wrong credentials given' do
22
+ client = Contentful::Client.new(space: "wrong", access_token: "credentials")
23
+
24
+ expect_vcr("unauthorized"){
25
+ client.entry 'nyancat'
26
+ }.to raise_error(Contentful::Unauthorized)
27
+ end
28
+
29
+ it 'will return 500 (ServerError) if ...' do
30
+ pending
31
+ end
32
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Contentful::Field do
4
+ let(:field){ vcr('field'){ create_client.content_type('cat').fields.first } }
5
+
6
+ describe 'Properties' do
7
+ it 'has a #properties getter returning a hash with symbol keys' do
8
+ expect( field.properties ).to be_a Hash
9
+ expect( field.properties.keys.sample ).to be_a Symbol
10
+ end
11
+
12
+ it 'has #id' do
13
+ expect( field.id ).to eq "name"
14
+ end
15
+
16
+ it 'has #name' do
17
+ expect( field.name ).to eq "Name"
18
+ end
19
+
20
+ it 'has #type' do
21
+ expect( field.type ).to eq "Text"
22
+ end
23
+
24
+ it 'could have #items' do
25
+ expect( field ).to respond_to :items
26
+ end
27
+
28
+ it 'has #required' do
29
+ expect( field.required ).to be_true
30
+ end
31
+
32
+ it 'has #localized' do
33
+ expect( field.required ).to be_true
34
+ end
35
+ end
36
+ end
data/spec/file_spec.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Contentful::File do
4
+ let(:file){ vcr('asset'){ create_client.asset('nyancat').file } }
5
+
6
+ describe 'Properties' do
7
+ it 'has a #properties getter returning a hash with symbol keys' do
8
+ expect( file.properties ).to be_a Hash
9
+ expect( file.properties.keys.sample ).to be_a Symbol
10
+ end
11
+
12
+ it 'has #file_name' do
13
+ expect( file.file_name ).to eq "Nyan_cat_250px_frame.png"
14
+ end
15
+
16
+ it 'has #content_type' do
17
+ expect( file.content_type ).to eq "image/png"
18
+ end
19
+
20
+ it 'has #url' do
21
+ expect( file.url ).to eq "//images.contentful.com/cfexampleapi/4gp6taAwW4CmSgumq2ekUm/9da0cd1936871b8d72343e895a00d611/Nyan_cat_250px_frame.png"
22
+ end
23
+
24
+ it 'has #details' do
25
+ expect( file.details ).to be_instance_of Hash
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,83 @@
1
+ {
2
+ "fields": [
3
+ {
4
+ "id": "name",
5
+ "name": "Name",
6
+ "type": "Text",
7
+ "required": true,
8
+ "localized": true
9
+ },
10
+ {
11
+ "id": "likes",
12
+ "name": "Likes",
13
+ "type": "Array",
14
+ "required": false,
15
+ "localized": false,
16
+ "items": {
17
+ "type": "Symbol"
18
+ }
19
+ },
20
+ {
21
+ "id": "color",
22
+ "name": "Color",
23
+ "type": "Symbol",
24
+ "required": false,
25
+ "localized": false
26
+ },
27
+ {
28
+ "id": "bestFriend",
29
+ "name": "Best Friend",
30
+ "type": "Link",
31
+ "required": false,
32
+ "localized": false,
33
+ "linkType": "Entry"
34
+ },
35
+ {
36
+ "id": "birthday",
37
+ "name": "Birthday",
38
+ "type": "Date",
39
+ "required": false,
40
+ "localized": false
41
+ },
42
+ {
43
+ "id": "lifes",
44
+ "name": "Lifes left",
45
+ "type": "Integer",
46
+ "required": false,
47
+ "localized": false,
48
+ "disabled": true
49
+ },
50
+ {
51
+ "id": "lives",
52
+ "name": "Lives left",
53
+ "type": "Integer",
54
+ "required": false,
55
+ "localized": false
56
+ },
57
+ {
58
+ "id": "image",
59
+ "name": "Image",
60
+ "required": false,
61
+ "localized": false,
62
+ "type": "Link",
63
+ "linkType": "Asset"
64
+ }
65
+ ],
66
+ "name": "Cat",
67
+ "displayField": "name",
68
+ "description": "Meow.",
69
+ "sys": {
70
+ "space": {
71
+ "sys": {
72
+ "type": "Link",
73
+ "linkType": "Space",
74
+ "id": "cfexampleapi"
75
+ }
76
+ },
77
+ "type": "ContentType",
78
+ "id": "cat",
79
+ "revision": 2,
80
+ "createdAt": "2013-06-27T22:46:12.852Z",
81
+ "updatedAt": "2013-09-02T13:14:47.863Z"
82
+ }
83
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "sys": {
3
+ "type": "Error",
4
+ "id": "NotFound"
5
+ },
6
+ "details": {
7
+ "type": "Entry",
8
+ "space": "cfexampleapi",
9
+ "id": "not found"
10
+ },
11
+ "message": "The resource could not be found.",
12
+ "requestId": "85f-351076632"
13
+ }
@@ -0,0 +1,48 @@
1
+ {
2
+ "fields": {
3
+ "name": "Nyan Cat",
4
+ "likes": [
5
+ "rainbows",
6
+ "fish"
7
+ ],
8
+ "color": "rainbow",
9
+ "bestFriend": {
10
+ "sys": {
11
+ "type": "Link",
12
+ "linkType": "Entry",
13
+ "id": "happycat"
14
+ }
15
+ },
16
+ "birthday": "2011-04-04T22:00:00+00:00",
17
+ "lives": 1337,
18
+ "image": {
19
+ "sys": {
20
+ "type": "Link",
21
+ "linkType": "Asset",
22
+ "id": "nyancat"
23
+ }
24
+ }
25
+ },
26
+ "sys": {
27
+ "space": {
28
+ "sys": {
29
+ "type": "Link",
30
+ "linkType": "Space",
31
+ "id": "cfexampleapi"
32
+ }
33
+ },
34
+ "type": "Entry",
35
+ "contentType": {
36
+ "sys": {
37
+ "type": "Link",
38
+ "linkType": "ContentType",
39
+ "id": "cat"
40
+ }
41
+ },
42
+ "id": "nyancat",
43
+ "revision": 5,
44
+ "createdAt": "2013-06-27T22:46:19.513Z",
45
+ "updatedAt": "2013-09-04T09:19:39.027Z",
46
+ "locale": "en-US"
47
+ }
48
+ }