contentful 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
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
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 457f3ffcb4578d1cabdc27b4fca30123f4f55168
4
+ data.tar.gz: 7fda79c6a4f4a72cd741f98a60d12cbb176f69ae
5
+ SHA512:
6
+ metadata.gz: 12180232bf6cf60187fc190c0ea0a31d87fd96fbe6358710dd892c19f0625ae9c485e196c201c95b9ae70a07bc22a5b34a1ecd4db1c83528912edbbe08dc2a4e
7
+ data.tar.gz: cac25970176537ea082e5747d63740d8a70ae24cc3f637c7b0dce225b38a47b5be6ac5e87ce5e12a34147eae8a0300aa39d20f1eb64b6f0ed9c0f8ebf49a7c72
data/.travis.yml CHANGED
@@ -5,4 +5,4 @@ rvm:
5
5
  - 2.1
6
6
  - 2.0.0
7
7
  - 1.9.3
8
- - jruby-19mode
8
+ # - jruby-19mode
data/CHANGELOG.md CHANGED
@@ -1,45 +1,64 @@
1
- ### 0.3.4
2
- * add optional gzip compression
1
+ # Change Log
2
+ ## 0.3.5
3
+ ### Added
4
+ * Logging of requests
3
5
 
4
- ### 0.3.3
5
- * Fix: handle 503 errors from the api
6
+ ### Fixed
7
+ * Cleaner and better error handling
6
8
 
7
- ### 0.3.2
8
- * add default property to locale, #23
9
+ ### Other
10
+ * Code cleanup
11
+ * Remove encoding strings from the source code files
9
12
 
10
- ### 0.3.1
11
- * return nil when a value is not supplied, fixes #18
12
- * do not parse empty responses
13
- * remove CGI dependency for http gem
13
+ ## 0.3.4
14
+ ### Added
15
+ * Optional gzip compression
14
16
 
15
- ### 0.3.0
17
+ ## 0.3.3
18
+ ### Fixed
19
+ * Fix: handle 503 errors from the API
16
20
 
17
- * Support Synchronization
21
+ ## 0.3.2
22
+ ### Added
23
+ * Default property to locale, #23
24
+
25
+ ## 0.3.1
26
+ ### Fixed
27
+ * Return nil when a value is not supplied, fixes #18
28
+ * Do not parse empty responses
18
29
 
30
+ ### Other
31
+ * Remove CGI dependency for http Gem
19
32
 
20
- ### 0.2.0
33
+ ## 0.3.0
34
+ ### Added
35
+ * Support Synchronization
21
36
 
37
+ ## 0.2.0
38
+ ### Added
22
39
  * Introduce new :entry_mapping configuration to enable custom Entry classes based on ContentTypes
40
+
41
+ ### Other
23
42
  * Update HTTP gem dependency to 0.6
24
43
  * Convert arrays in query values to strings, separated by comma
25
44
 
26
45
 
27
- ### 0.1.3
46
+ ## 0.1.3
47
+ ### Fixed
48
+ * Better link inclusion processing, prevents "stack level to deep" errors
28
49
 
29
- * Better link inclusion processing, prevent "stack level to deep" errors
30
50
 
51
+ ## 0.1.2
52
+ ### Fixed
53
+ * The way all content types are retrieved
31
54
 
32
- ### 0.1.2
33
55
 
34
- * Fix the way all content types are retrieved
56
+ ## 0.1.1
57
+ ### Fixed
58
+ * Bug that prevented fields with multiple resources to be parsed correctly
35
59
 
36
-
37
- ### 0.1.1
38
-
39
- * Fix a bug that prevented fields with multiple resources to be parsed correctly
60
+ ### Other
40
61
  * Restrict HTTP gem dependency to < 0.6
41
62
 
42
-
43
- ### 0.1.0
44
-
63
+ ## 0.1.0
45
64
  * Initial release.
data/README.md CHANGED
@@ -172,6 +172,28 @@ See next paragraph for explanation
172
172
 
173
173
  Enables gzip response content encoding, default to: true
174
174
 
175
+ ### :logger
176
+
177
+ Logging is disabled by default, it can be enabled by setting a logger instance and a logging severity.
178
+ ```ruby
179
+ client = Contentful::Client.new(
180
+ access_token: 'b4c0n73n7fu1',
181
+ space: 'cfexampleapi',
182
+ logger: logger_instance,
183
+ log_level: Logger::DEBUG
184
+ )
185
+ ```
186
+ Example loggers:
187
+
188
+ ```ruby
189
+ Rails.logger
190
+ Logger.new('logfile.log')
191
+ ```
192
+
193
+ ### :log_level
194
+ The default severity is set to INFO and logs only the request attributes (headers, parameters and url). Setting it to DEBUG will also log the raw JSON response.
195
+
196
+
175
197
  ## Advanced Usage
176
198
  ### Custom Resource Classes
177
199
 
data/RELEASE.md ADDED
@@ -0,0 +1,8 @@
1
+ # Releasing new versions:
2
+
3
+ Make sure all points are taken care of:
4
+
5
+ - [ ] Make sure tests are green.
6
+ - [ ] Update Changelog: http://keepachangelog.com/
7
+ - [ ] Bump version number: http://semver.org/
8
+ - [ ] Release gem and communicate updates
data/Rakefile CHANGED
@@ -1,12 +1,10 @@
1
- # encoding: utf-8
2
-
3
1
  require 'rubygems'
4
2
 
5
3
  begin
6
4
  require 'bundler'
7
5
  rescue LoadError => e
8
6
  warn e.message
9
- warn "Run `gem install bundler` to install Bundler."
7
+ warn 'Run `gem install bundler` to install Bundler.'
10
8
  exit -1
11
9
  end
12
10
 
@@ -14,7 +12,7 @@ begin
14
12
  Bundler.setup(:development)
15
13
  rescue Bundler::BundlerError => e
16
14
  warn e.message
17
- warn "Run `bundle install` to install missing gems."
15
+ warn 'Run `bundle install` to install missing gems.'
18
16
  exit e.status_code
19
17
  end
20
18
 
@@ -26,5 +24,5 @@ Gem::Tasks.new
26
24
  require 'rspec/core/rake_task'
27
25
  RSpec::Core::RakeTask.new
28
26
 
29
- task :test => :spec
30
- task :default => :spec
27
+ task test: :spec
28
+ task default: :spec
data/contentful.gemspec CHANGED
@@ -1,19 +1,17 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
1
  require File.expand_path('../lib/contentful/version', __FILE__)
4
2
 
5
3
  Gem::Specification.new do |gem|
6
- gem.name = "contentful"
4
+ gem.name = 'contentful'
7
5
  gem.version = Contentful::VERSION
8
6
  gem.summary = 'contentful'
9
7
  gem.description = 'Ruby client for the https://www.contentful.com Content Delivery API'
10
- gem.license = "MIT"
11
- gem.authors = ["Contentful GmbH (Jan Lelis)",'Contentful GmbH (Andreas Tiefenthaler)']
12
- gem.email = "rubygems@contentful.com"
13
- gem.homepage = "https://github.com/contentful/contentful.rb"
8
+ gem.license = 'MIT'
9
+ gem.authors = ['Contentful GmbH (Jan Lelis)', 'Contentful GmbH (Andreas Tiefenthaler)']
10
+ gem.email = 'rubygems@contentful.com'
11
+ gem.homepage = 'https://github.com/contentful/contentful.rb'
14
12
 
15
- gem.files = Dir['{**/}{.*,*}'].select { |path| File.file?(path) && !path.start_with?("pkg") }
16
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.files = Dir['{**/}{.*,*}'].select { |path| File.file?(path) && !path.start_with?('pkg') }
14
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
17
15
  gem.test_files = gem.files.grep(%r{^spec/})
18
16
  gem.require_paths = ['lib']
19
17
 
@@ -20,7 +20,7 @@ end
20
20
  res = MyResource.new(
21
21
  'some' => 'value',
22
22
  'age' => '25',
23
- 'country' => {'code' => 'de', 'name' => 'Deutschland'},
23
+ 'country' => { 'code' => 'de', 'name' => 'Deutschland' },
24
24
  'unknown_property' => 'ignored'
25
25
  )
26
26
 
@@ -29,7 +29,6 @@ p res.age # => 25
29
29
  p res.country # #<Contentful::Locale: ...
30
30
  p res.unknown_property # NoMethodError
31
31
 
32
-
33
32
  # Another possibility to create customized resources is to just inherit from an
34
33
  # existing one:
35
34
 
@@ -6,7 +6,7 @@ require 'contentful'
6
6
 
7
7
  client = Contentful::Client.new(
8
8
  space: 'cfexampleapi',
9
- access_token: "b4c0n73n7fu1",
9
+ access_token: 'b4c0n73n7fu1',
10
10
  dynamic_entries: :manual,
11
11
  )
12
12
 
@@ -22,7 +22,6 @@ p nyancat.is_a? CatEntry # => true
22
22
  p nyancat.fields[:name] # => "Nyan Cat" # This would also be possible with a non-dynamic entry
23
23
  p nyancat.name # => "Nyan Cat"
24
24
 
25
-
26
25
  # You don't need to initialize a ContentType resource to create a DynamicEntry
27
26
  # You could also pass the content type's JSON representation:
28
27
 
@@ -112,12 +111,11 @@ SuperCatEntry = Contentful::DynamicEntry.create <<JSON
112
111
  }
113
112
  JSON
114
113
 
115
-
116
114
  # AUTO MODE - All entries will be converted to dynamic entries
117
115
 
118
116
  client = Contentful::Client.new(
119
117
  space: 'cfexampleapi',
120
- access_token: "b4c0n73n7fu1",
118
+ access_token: 'b4c0n73n7fu1',
121
119
  dynamic_entries: :auto,
122
120
  )
123
121
 
@@ -1,9 +1,8 @@
1
1
  require 'contentful'
2
2
 
3
-
4
3
  client = Contentful::Client.new(
5
4
  space: 'cfexampleapi',
6
- access_token: "b4c0n73n7fu1",
5
+ access_token: 'b4c0n73n7fu1',
7
6
  )
8
7
 
9
8
  p client.space
@@ -24,4 +23,4 @@ p client.entries(
24
23
  p client.content_types(
25
24
  order: '-sys.updatedAt',
26
25
  limit: 3,
27
- )
26
+ )
@@ -1,9 +1,8 @@
1
1
  require 'contentful'
2
2
 
3
-
4
3
  client = Contentful::Client.new(
5
4
  space: 'cfexampleapi',
6
- access_token: "b4c0n73n7fu1",
5
+ access_token: 'b4c0n73n7fu1',
7
6
  )
8
7
 
9
8
  begin
@@ -12,11 +11,10 @@ rescue => error
12
11
  p error
13
12
  end
14
13
 
15
-
16
14
  client2 = Contentful::Client.new(
17
15
  space: 'cfexampleapi',
18
- access_token: "b4c0n73n7fu1",
16
+ access_token: 'b4c0n73n7fu1',
19
17
  raise_errors: false,
20
18
  )
21
19
 
22
- p client2.entry 'not found'
20
+ p client2.entry 'not found'
data/examples/raw_mode.rb CHANGED
@@ -1,9 +1,8 @@
1
1
  require 'contentful'
2
2
 
3
-
4
3
  client = Contentful::Client.new(
5
4
  space: 'cfexampleapi',
6
- access_token: "b4c0n73n7fu1",
5
+ access_token: 'b4c0n73n7fu1',
7
6
  raw_mode: true,
8
7
  )
9
8
 
@@ -12,4 +11,4 @@ p entry.is_a? Contentful::Resource # false
12
11
  p entry.is_a? Contentful::Response # true
13
12
  p entry.status
14
13
  p entry
15
- puts entry.raw
14
+ puts entry.raw
@@ -6,7 +6,6 @@
6
6
  # - a Proc, that returns the Class to use
7
7
  # - a Symbol for a method of the ResourceBuilder object
8
8
 
9
-
10
9
  require 'contentful'
11
10
 
12
11
  class MyBetterArray < Contentful::Array
@@ -18,13 +17,13 @@ end
18
17
 
19
18
  client = Contentful::Client.new(
20
19
  space: 'cfexampleapi',
21
- access_token: "b4c0n73n7fu1",
20
+ access_token: 'b4c0n73n7fu1',
22
21
  resource_mapping: {
23
22
  'Array' => MyBetterArray,
24
- 'Asset' => ->(json_object){
23
+ 'Asset' => ->(_json_object)do
25
24
  # might return different class if some criteria is matched
26
25
  Contentful::Asset
27
- },
26
+ end,
28
27
  }
29
28
  )
30
29
 
@@ -28,6 +28,5 @@ module Contentful
28
28
  false
29
29
  end
30
30
  end
31
-
32
31
  end
33
32
  end
@@ -22,7 +22,7 @@ module Contentful
22
22
  h: options[:h] || options[:height],
23
23
  fm: options[:fm] || options[:format],
24
24
  q: options[:q] || options[:quality]
25
- }.reject { |k, v| v.nil? }
25
+ }.reject { |_k, v| v.nil? }
26
26
 
27
27
  if query.empty?
28
28
  file.url
@@ -4,6 +4,7 @@ require_relative 'resource_builder'
4
4
  require_relative 'sync'
5
5
 
6
6
  require 'http'
7
+ require 'logger'
7
8
 
8
9
  module Contentful
9
10
  # The client object is initialized with a space and a key and then used
@@ -11,21 +12,23 @@ module Contentful
11
12
  # See README for details
12
13
  class Client
13
14
  DEFAULT_CONFIGURATION = {
14
- secure: true,
15
- raise_errors: true,
16
- dynamic_entries: :manual,
17
- api_url: 'cdn.contentful.com',
18
- api_version: 1,
19
- authentication_mechanism: :header,
20
- resource_builder: ResourceBuilder,
21
- resource_mapping: {},
22
- entry_mapping: {},
23
- default_locale: 'en-US',
24
- raw_mode: false,
25
- gzip_encoded: false
15
+ secure: true,
16
+ raise_errors: true,
17
+ dynamic_entries: :manual,
18
+ api_url: 'cdn.contentful.com',
19
+ api_version: 1,
20
+ authentication_mechanism: :header,
21
+ resource_builder: ResourceBuilder,
22
+ resource_mapping: {},
23
+ entry_mapping: {},
24
+ default_locale: 'en-US',
25
+ raw_mode: false,
26
+ gzip_encoded: false,
27
+ logger: false,
28
+ log_level: Logger::INFO
26
29
  }
27
30
 
28
- attr_reader :configuration, :dynamic_entry_cache
31
+ attr_reader :configuration, :dynamic_entry_cache, :logger
29
32
 
30
33
  # Wraps the actual HTTP request
31
34
  def self.get_http(url, query, headers = {})
@@ -36,6 +39,7 @@ module Contentful
36
39
  @configuration = default_configuration.merge(given_configuration)
37
40
  normalize_configuration!
38
41
  validate_configuration!
42
+ setup_logger
39
43
 
40
44
  if configuration[:dynamic_entries] == :auto
41
45
  update_dynamic_entry_cache!
@@ -44,6 +48,11 @@ module Contentful
44
48
  end
45
49
  end
46
50
 
51
+ def setup_logger
52
+ @logger = configuration[:logger]
53
+ logger.level = configuration[:log_level] if logger
54
+ end
55
+
47
56
  # Returns the default configuration
48
57
  def default_configuration
49
58
  DEFAULT_CONFIGURATION.dup
@@ -105,9 +114,9 @@ module Contentful
105
114
 
106
115
  # Returns the headers used for the HTTP requests
107
116
  def request_headers
108
- headers = { 'User-Agent' => "RubyContentfulGem/#{Contentful::VERSION}" }
117
+ headers = {'User-Agent' => "RubyContentfulGem/#{Contentful::VERSION}"}
109
118
  headers['Authorization'] = "Bearer #{configuration[:access_token]}" if configuration[:authentication_mechanism] == :header
110
- headers['Content-Type'] = "application/vnd.contentful.delivery.v#{configuration[:api_version].to_i}+json" if configuration[:api_version]
119
+ headers['Content-Type'] = "application/vnd.contentful.delivery.v#{configuration[:api_version].to_i}+json" if configuration[:api_version]
111
120
  headers['Accept-Encoding'] = 'gzip' if configuration[:gzip_encoded]
112
121
  headers
113
122
  end
@@ -115,7 +124,7 @@ module Contentful
115
124
  # Patches a query hash with the client configurations for queries
116
125
  def request_query(query)
117
126
  if configuration[:authentication_mechanism] == :query_string
118
- query["access_token"] = configuration[:access_token]
127
+ query['access_token'] = configuration[:access_token]
119
128
  end
120
129
 
121
130
  query
@@ -126,16 +135,17 @@ module Contentful
126
135
  # return Response objects instead
127
136
  def get(request, build_resource = true)
128
137
  url = request.absolute? ? request.url : base_url + request.url
138
+ logger.info(request: {url: url, query: request.query, header: request_headers}) if logger
129
139
  response = Response.new(
130
- self.class.get_http(
131
- url,
132
- request_query(request.query),
133
- request_headers
134
- ), request
140
+ self.class.get_http(
141
+ url,
142
+ request_query(request.query),
143
+ request_headers
144
+ ), request
135
145
  )
136
146
 
137
147
  return response if !build_resource || configuration[:raw_mode]
138
-
148
+ logger.debug(response: response) if logger
139
149
  result = configuration[:resource_builder].new(
140
150
  self,
141
151
  response,
@@ -144,7 +154,7 @@ module Contentful
144
154
  configuration[:default_locale]
145
155
  ).run
146
156
 
147
- raise result if result.is_a?(Error) && configuration[:raise_errors]
157
+ fail result if result.is_a?(Error) && configuration[:raise_errors]
148
158
  result
149
159
  end
150
160
 
@@ -152,12 +162,12 @@ module Contentful
152
162
  # See README for details.
153
163
  def update_dynamic_entry_cache!
154
164
  @dynamic_entry_cache = Hash[
155
- content_types(limit: 1000).map do |ct|
156
- [
157
- ct.id.to_sym,
158
- DynamicEntry.create(ct)
159
- ]
160
- end
165
+ content_types(limit: 1000).map do |ct|
166
+ [
167
+ ct.id.to_sym,
168
+ DynamicEntry.create(ct)
169
+ ]
170
+ end
161
171
  ]
162
172
  end
163
173
 
@@ -170,11 +180,10 @@ module Contentful
170
180
  # Create a new synchronisation object
171
181
  # Takes sync options or a sync_url
172
182
  # You will need to call #each_page or #first_page on it
173
- def sync(options = { initial: true })
183
+ def sync(options = {initial: true})
174
184
  Sync.new(self, options)
175
185
  end
176
186
 
177
-
178
187
  private
179
188
 
180
189
  def normalize_configuration!
@@ -184,31 +193,31 @@ module Contentful
184
193
 
185
194
  def validate_configuration!
186
195
  if configuration[:space].empty?
187
- raise ArgumentError, 'You will need to initialize a client with a :space'
196
+ fail ArgumentError, 'You will need to initialize a client with a :space'
188
197
  end
189
198
 
190
199
  if configuration[:access_token].empty?
191
- raise ArgumentError, 'You will need to initialize a client with an :access_token'
200
+ fail ArgumentError, 'You will need to initialize a client with an :access_token'
192
201
  end
193
202
 
194
203
  if configuration[:api_url].empty?
195
- raise ArgumentError, 'The client configuration needs to contain an :api_url'
204
+ fail ArgumentError, 'The client configuration needs to contain an :api_url'
196
205
  end
197
206
 
198
207
  if configuration[:default_locale].empty?
199
- raise ArgumentError, 'The client configuration needs to contain a :default_locale'
208
+ fail ArgumentError, 'The client configuration needs to contain a :default_locale'
200
209
  end
201
210
 
202
211
  unless configuration[:api_version].to_i >= 0
203
- raise ArgumentError, 'The :api_version must be a positive number or nil'
212
+ fail ArgumentError, 'The :api_version must be a positive number or nil'
204
213
  end
205
214
 
206
215
  unless [:header, :query_string].include? configuration[:authentication_mechanism]
207
- raise ArgumentError, 'The authentication mechanism must be :header or :query_string'
216
+ fail ArgumentError, 'The authentication mechanism must be :header or :query_string'
208
217
  end
209
218
 
210
219
  unless [:auto, :manual].include? configuration[:dynamic_entries]
211
- raise ArgumentError, 'The :dynamic_entries mode must be :auto or :manual'
220
+ fail ArgumentError, 'The :dynamic_entries mode must be :auto or :manual'
212
221
  end
213
222
  end
214
223
  end