contentful 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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,112 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
+ <head>
5
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
+ <title>
7
+ Top Level Namespace
8
+
9
+ &mdash; Documentation by YARD 0.8.7.3
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
16
+
17
+ <script type="text/javascript" charset="utf-8">
18
+ hasFrames = window.top.frames.main ? true : false;
19
+ relpath = '';
20
+ framesUrl = "frames.html#!" + escape(window.location.href);
21
+ </script>
22
+
23
+
24
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
25
+
26
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
27
+
28
+
29
+ </head>
30
+ <body>
31
+ <div id="header">
32
+ <div id="menu">
33
+
34
+ <a href="_index.html">Index</a> &raquo;
35
+
36
+
37
+ <span class="title">Top Level Namespace</span>
38
+
39
+
40
+ <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
41
+ </div>
42
+
43
+ <div id="search">
44
+
45
+ <a class="full_list_link" id="class_list_link"
46
+ href="class_list.html">
47
+ Class List
48
+ </a>
49
+
50
+ <a class="full_list_link" id="method_list_link"
51
+ href="method_list.html">
52
+ Method List
53
+ </a>
54
+
55
+ <a class="full_list_link" id="file_list_link"
56
+ href="file_list.html">
57
+ File List
58
+ </a>
59
+
60
+ </div>
61
+ <div class="clear"></div>
62
+ </div>
63
+
64
+ <iframe id="search_frame"></iframe>
65
+
66
+ <div id="content"><h1>Top Level Namespace
67
+
68
+
69
+
70
+ </h1>
71
+
72
+ <dl class="box">
73
+
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+ </dl>
82
+ <div class="clear"></div>
83
+
84
+ <h2>Defined Under Namespace</h2>
85
+ <p class="children">
86
+
87
+
88
+ <strong class="modules">Modules:</strong> <span class='object_link'><a href="Contentful.html" title="Contentful (module)">Contentful</a></span>
89
+
90
+
91
+
92
+
93
+ </p>
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+
103
+ </div>
104
+
105
+ <div id="footer">
106
+ Generated on Tue Mar 11 09:25:52 2014 by
107
+ <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
108
+ 0.8.7.3 (ruby-1.9.3).
109
+ </div>
110
+
111
+ </body>
112
+ </html>
@@ -0,0 +1,43 @@
1
+ # Contentful resource classes are just plain Ruby classes that include the
2
+ # Contentful::Resource module.
3
+ #
4
+ # You can then define properties of the class. This will create a getter method
5
+ # with this name You can optionally pass a type identifier (Symbol or Class).
6
+ #
7
+ # Classes will be instantiated for the properties,
8
+ # Symbols will be looked up in Contentful::Resource::COERCIONS
9
+
10
+ require 'contentful'
11
+
12
+ class MyResource
13
+ include Contentful::Resource
14
+
15
+ property :some
16
+ property :age, :integer
17
+ property :country, Contentful::Locale
18
+ end
19
+
20
+ res = MyResource.new(
21
+ 'some' => 'value',
22
+ 'age' => '25',
23
+ 'country' => {'code' => 'de', 'name' => 'Deutschland'},
24
+ 'unknown_property' => 'ignored'
25
+ )
26
+
27
+ p res.some # => "value"
28
+ p res.age # => 25
29
+ p res.country # #<Contentful::Locale: ...
30
+ p res.unknown_property # NoMethodError
31
+
32
+
33
+ # Another possibility to create customized resources is to just inherit from an
34
+ # existing one:
35
+
36
+ class MyBetterArray < Contentful::Array
37
+ # e.g. define more methods that you need
38
+ def last
39
+ items.last
40
+ end
41
+ end
42
+
43
+ # Read further in examples/resource_mapping.rb
@@ -0,0 +1,126 @@
1
+ require 'contentful'
2
+
3
+ # A DynamicEntry is a resource classes, specifically for one ContentType
4
+ # This is the manual way of creating a dynamic class. This should not be
5
+ # neceassary in :auto mode
6
+
7
+ client = Contentful::Client.new(
8
+ space: 'cfexampleapi',
9
+ access_token: "b4c0n73n7fu1",
10
+ dynamic_entries: :manual,
11
+ )
12
+
13
+ cat = client.content_type('cat')
14
+ CatEntry = Contentful::DynamicEntry.create(cat)
15
+ client.register_dynamic_entry 'cat', CatEntry
16
+
17
+ # The CatEntry behaves just like a normal entry, but it has knowlegde about the entry fields.
18
+ # It will create getter methods and convert field contents to the proper type:
19
+
20
+ nyancat = client.entry('nyancat')
21
+ p nyancat.is_a? CatEntry # => true
22
+ p nyancat.fields[:name] # => "Nyan Cat" # This would also be possible with a non-dynamic entry
23
+ p nyancat.name # => "Nyan Cat"
24
+
25
+
26
+ # You don't need to initialize a ContentType resource to create a DynamicEntry
27
+ # You could also pass the content type's JSON representation:
28
+
29
+ SuperCatEntry = Contentful::DynamicEntry.create <<JSON
30
+ {
31
+ "fields": [
32
+ {
33
+ "id": "name",
34
+ "name": "Name",
35
+ "type": "Text",
36
+ "required": true,
37
+ "localized": true
38
+ },
39
+ {
40
+ "id": "likes",
41
+ "name": "Likes",
42
+ "type": "Array",
43
+ "required": false,
44
+ "localized": false,
45
+ "items": {
46
+ "type": "Symbol"
47
+ }
48
+ },
49
+ {
50
+ "id": "color",
51
+ "name": "Color",
52
+ "type": "Symbol",
53
+ "required": false,
54
+ "localized": false
55
+ },
56
+ {
57
+ "id": "bestFriend",
58
+ "name": "Best Friend",
59
+ "type": "Link",
60
+ "required": false,
61
+ "localized": false,
62
+ "linkType": "Entry"
63
+ },
64
+ {
65
+ "id": "birthday",
66
+ "name": "Birthday",
67
+ "type": "Date",
68
+ "required": false,
69
+ "localized": false
70
+ },
71
+ {
72
+ "id": "lifes",
73
+ "name": "Lifes left",
74
+ "type": "Integer",
75
+ "required": false,
76
+ "localized": false,
77
+ "disabled": true
78
+ },
79
+ {
80
+ "id": "lives",
81
+ "name": "Lives left",
82
+ "type": "Integer",
83
+ "required": false,
84
+ "localized": false
85
+ },
86
+ {
87
+ "id": "image",
88
+ "name": "Image",
89
+ "required": false,
90
+ "localized": false,
91
+ "type": "Link",
92
+ "linkType": "Asset"
93
+ }
94
+ ],
95
+ "name": "Cat",
96
+ "displayField": "name",
97
+ "description": "Meow.",
98
+ "sys": {
99
+ "space": {
100
+ "sys": {
101
+ "type": "Link",
102
+ "linkType": "Space",
103
+ "id": "cfexampleapi"
104
+ }
105
+ },
106
+ "type": "ContentType",
107
+ "id": "cat",
108
+ "revision": 2,
109
+ "createdAt": "2013-06-27T22:46:12.852Z",
110
+ "updatedAt": "2013-09-02T13:14:47.863Z"
111
+ }
112
+ }
113
+ JSON
114
+
115
+
116
+ # AUTO MODE - All entries will be converted to dynamic entries
117
+
118
+ client = Contentful::Client.new(
119
+ space: 'cfexampleapi',
120
+ access_token: "b4c0n73n7fu1",
121
+ dynamic_entries: :auto,
122
+ )
123
+
124
+ p happycat = client.entry('happycat') # #<Contentful::DynamicEntry[cat]:10078260 @fields={:name=>"Happy Cat" ...
125
+ p happycat.is_a? Contentful::DynamicEntry # true
126
+ p happycat.color # gray
@@ -0,0 +1,27 @@
1
+ require 'contentful'
2
+
3
+
4
+ client = Contentful::Client.new(
5
+ space: 'cfexampleapi',
6
+ access_token: "b4c0n73n7fu1",
7
+ )
8
+
9
+ p client.space
10
+
11
+ p client.content_types
12
+
13
+ p client.entry 'nyancat', locale: 'tlh'
14
+
15
+ p client.entries(
16
+ 'content_type' => 'cat',
17
+ 'fields.likes' => 'lasagna',
18
+ )
19
+
20
+ p client.entries(
21
+ query: 'bacon',
22
+ )
23
+
24
+ p client.content_types(
25
+ order: '-sys.updatedAt',
26
+ limit: 3,
27
+ )
@@ -0,0 +1,22 @@
1
+ require 'contentful'
2
+
3
+
4
+ client = Contentful::Client.new(
5
+ space: 'cfexampleapi',
6
+ access_token: "b4c0n73n7fu1",
7
+ )
8
+
9
+ begin
10
+ p client.entry 'not found'
11
+ rescue => error
12
+ p error
13
+ end
14
+
15
+
16
+ client2 = Contentful::Client.new(
17
+ space: 'cfexampleapi',
18
+ access_token: "b4c0n73n7fu1",
19
+ raise_errors: false,
20
+ )
21
+
22
+ p client2.entry 'not found'
@@ -0,0 +1,15 @@
1
+ require 'contentful'
2
+
3
+
4
+ client = Contentful::Client.new(
5
+ space: 'cfexampleapi',
6
+ access_token: "b4c0n73n7fu1",
7
+ raw_mode: true,
8
+ )
9
+
10
+ entry = client.entry 'nyancat'
11
+ p entry.is_a? Contentful::Resource # false
12
+ p entry.is_a? Contentful::Response # true
13
+ p entry.status
14
+ p entry
15
+ puts entry.raw
@@ -0,0 +1,33 @@
1
+ # Using the :resource_mapping configuration, you can register your own classes to be used
2
+ # for the results from contentful.
3
+ #
4
+ # The key of the array defines the resource type (object["sys"]["type"]) and the value is:
5
+ # - the Class to use
6
+ # - a Proc, that returns the Class to use
7
+ # - a Symbol for a method of the ResourceBuilder object
8
+
9
+
10
+ require 'contentful'
11
+
12
+ class MyBetterArray < Contentful::Array
13
+ # e.g. define more methods that you need
14
+ def last
15
+ items.last
16
+ end
17
+ end
18
+
19
+ client = Contentful::Client.new(
20
+ space: 'cfexampleapi',
21
+ access_token: "b4c0n73n7fu1",
22
+ resource_mapping: {
23
+ 'Array' => MyBetterArray,
24
+ 'Asset' => ->(json_object){
25
+ # might return different class if some criteria is matched
26
+ Contentful::Asset
27
+ },
28
+ }
29
+ )
30
+
31
+ assets = client.assets
32
+ p assets.class # => MyBetterArray
33
+ p assets.last # => #<Contentful::Asset ...
data/lib/contentful.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'contentful/version'
2
+ require 'contentful/support'
3
+ require 'contentful/client'
@@ -0,0 +1,47 @@
1
+ require_relative 'resource'
2
+
3
+ module Contentful
4
+ # Resource Class for Arrays (e.g. search results)
5
+ # https://www.contentful.com/developers/documentation/content-delivery-api/#arrays
6
+ # It also provides an #each method and includes Ruby's Enumerable module (gives you methods like #min, #first, etc)
7
+ class Array
8
+ DEFAULT_LIMIT = 100
9
+
10
+ include Contentful::Resource
11
+ include Contentful::Resource::SystemProperties
12
+ include Enumerable
13
+
14
+ property :total, :integer
15
+ property :limit, :integer
16
+ property :skip, :integer
17
+ property :items
18
+
19
+ # Only returns true for Contentful::Array
20
+ def array?
21
+ true
22
+ end
23
+
24
+ # Simplifies pagination
25
+ def next_page
26
+ if request
27
+ new_skip = (skip || 0) + (limit || DEFAULT_LIMIT)
28
+ new_request = request.copy
29
+ new_request.query[:skip] = new_skip
30
+ new_request.get
31
+ else
32
+ false
33
+ end
34
+ end
35
+
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
+
46
+ end
47
+ end
@@ -0,0 +1,34 @@
1
+ require_relative 'resource'
2
+ require_relative 'resource/asset_fields'
3
+
4
+ module Contentful
5
+ # Resource class for Asset.
6
+ # https://www.contentful.com/developers/documentation/content-delivery-api/#assets
7
+ class Asset
8
+ include Contentful::Resource
9
+ include Contentful::Resource::SystemProperties
10
+ include Contentful::Resource::AssetFields
11
+
12
+ # Returns the image url of an asset
13
+ # Allows you to pass in the following options for image resizing:
14
+ # :width
15
+ # :height
16
+ # :format
17
+ # :quality
18
+ # See https://www.contentful.com/developers/documentation/content-delivery-api/#image-asset-resizing
19
+ def image_url(options = {})
20
+ query = {
21
+ w: options[:w] || options[:width],
22
+ h: options[:h] || options[:height],
23
+ fm: options[:fm] || options[:format],
24
+ q: options[:q] || options[:quality],
25
+ }.reject{ |k,v| v.nil? }
26
+
27
+ if query.empty?
28
+ file.url
29
+ else
30
+ "#{file.url}?#{ URI.encode_www_form(query) }"
31
+ end
32
+ end
33
+ end
34
+ end