contentful-management 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +37 -8
  3. data/Gemfile +0 -1
  4. data/README.md +130 -0
  5. data/Rakefile +0 -1
  6. data/contentful-management.gemspec +0 -1
  7. data/examples/blog.rb +1 -1
  8. data/lib/contentful/management.rb +0 -1
  9. data/lib/contentful/management/array.rb +0 -1
  10. data/lib/contentful/management/asset.rb +65 -26
  11. data/lib/contentful/management/client.rb +19 -9
  12. data/lib/contentful/management/content_type.rb +43 -28
  13. data/lib/contentful/management/dynamic_entry.rb +15 -15
  14. data/lib/contentful/management/entry.rb +73 -40
  15. data/lib/contentful/management/error.rb +6 -1
  16. data/lib/contentful/management/field.rb +63 -8
  17. data/lib/contentful/management/file.rb +0 -1
  18. data/lib/contentful/management/http_client.rb +0 -1
  19. data/lib/contentful/management/link.rb +0 -1
  20. data/lib/contentful/management/locale.rb +11 -3
  21. data/lib/contentful/management/location.rb +0 -1
  22. data/lib/contentful/management/request.rb +5 -7
  23. data/lib/contentful/management/resource.rb +3 -3
  24. data/lib/contentful/management/resource/array_like.rb +0 -1
  25. data/lib/contentful/management/resource/fields.rb +1 -2
  26. data/lib/contentful/management/resource/refresher.rb +3 -3
  27. data/lib/contentful/management/resource/system_properties.rb +0 -1
  28. data/lib/contentful/management/resource_builder.rb +16 -27
  29. data/lib/contentful/management/response.rb +36 -32
  30. data/lib/contentful/management/space.rb +16 -6
  31. data/lib/contentful/management/space_webhook_methods_factory.rb +0 -4
  32. data/lib/contentful/management/support.rb +0 -1
  33. data/lib/contentful/management/validation.rb +34 -0
  34. data/lib/contentful/management/version.rb +1 -2
  35. data/lib/contentful/management/webhook.rb +17 -10
  36. data/spec/fixtures/vcr_cassettes/content_type/validation/in.yml +588 -0
  37. data/spec/fixtures/vcr_cassettes/content_type/validation/in_add.yml +620 -0
  38. data/spec/fixtures/vcr_cassettes/content_type/validation/in_update.yml +668 -0
  39. data/spec/fixtures/vcr_cassettes/content_type/validation/link_content_type.yml +749 -0
  40. data/spec/fixtures/vcr_cassettes/content_type/validation/link_field.yml +770 -0
  41. data/spec/fixtures/vcr_cassettes/content_type/validation/link_mimetype_group.yml +766 -0
  42. data/spec/fixtures/vcr_cassettes/content_type/validation/multiple_add.yml +854 -0
  43. data/spec/fixtures/vcr_cassettes/content_type/validation/present.yml +808 -0
  44. data/spec/fixtures/vcr_cassettes/content_type/validation/range.yml +683 -0
  45. data/spec/fixtures/vcr_cassettes/content_type/validation/range_update.yml +697 -0
  46. data/spec/fixtures/vcr_cassettes/content_type/validation/regexp.yml +720 -0
  47. data/spec/fixtures/vcr_cassettes/content_type/validation/size.yml +791 -0
  48. data/spec/fixtures/vcr_cassettes/entry/service_unavailable.yml +52 -0
  49. data/spec/fixtures/vcr_cassettes/space/webhook/create.yml +439 -0
  50. data/spec/lib/contentful/management/asset_spec.rb +16 -16
  51. data/spec/lib/contentful/management/client_spec.rb +0 -1
  52. data/spec/lib/contentful/management/content_type_spec.rb +143 -1
  53. data/spec/lib/contentful/management/entry_spec.rb +10 -4
  54. data/spec/lib/contentful/management/locale_spec.rb +0 -1
  55. data/spec/lib/contentful/management/space_spec.rb +9 -1
  56. data/spec/lib/contentful/management/webhook_spec.rb +0 -1
  57. data/spec/spec_helper.rb +0 -1
  58. data/spec/support/vcr.rb +0 -1
  59. metadata +63 -62
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cf3767440a711329cd8a89e79165348a4e7e7c1b
4
+ data.tar.gz: f7a51847bf88d1dd1c7e92814df67b727772ac64
5
+ SHA512:
6
+ metadata.gz: 8bc3c9c2e2421fbadb69cbd4f350e34df803a9e2c8197575ebfabb02504f48f4e99f4e26caea2443e9a4529addd8b220cd3b9241d5ecf19fb071d0ea327a06c1
7
+ data.tar.gz: 518a8413cbe4611e42594c2b2eef3f6b8c129851960c01245b65ed5c14c7aa79ebe9275fe070fe84ad47ba3d9631c467fddef038537d8ccfce10b49c7fb3565e
data/CHANGELOG.md CHANGED
@@ -1,40 +1,69 @@
1
1
  # Change Log
2
+ ## 0.3.1
2
3
 
4
+ ### Added
5
+ * Logging of requests
6
+ * Access to validations in responses
7
+ * Create validations through the API
8
+
9
+ ### Fixed
10
+ * Cleaner and better error handling
11
+
12
+ ### Other
13
+ * Cleaned the code
14
+ * Remove encoding strings from the source code files
3
15
  ## 0.2.1
16
+ ### Fixed
4
17
  * Fix: Use array for symbols in entry fields
5
18
 
6
19
  ## 0.2.0
20
+ ### Fixed
7
21
  * Fix: create entry with multiple locales, skip attributes for not localized fields in content types
8
22
  * Fix: reload Assets
9
- * add optional gzip envoding
23
+
24
+ ### Added
25
+ * Add optional gzip encoding
10
26
 
11
27
  ## 0.1.0
12
- * Support for webhooks
13
- * add image url to asset
14
- * Cleaning code
28
+ ### Added
29
+ * Support for web hooks
30
+ * Image url to asset
31
+
32
+ ### Fixed
15
33
  * Fix: remove implicit processing of assets.
16
34
  * Fix: Gem is modifying nil #17
17
35
  * Fix: rename asset.process_files to asset.process
18
36
 
37
+ ### Other
38
+ * Cleaning code
19
39
 
20
40
  ## 0.0.3
41
+ ### Added:
42
+ * More documentation
43
+
44
+ ### Fixed:
21
45
  * Fix: next_page feature
22
46
  * Fix: create entry with specific locale
23
47
  * Fix: service unavailable error (503)
24
48
  * Fix: reload method on objects
49
+ ### Other
25
50
  * Code cleanup
26
- * More documentation
27
51
 
28
52
  ## 0.0.2
53
+ ### Fixed
29
54
  * Fix: Convert an Entry to a DynamicEntry after being created.
30
55
 
31
56
  ## 0.0.1
32
- * Fix: Headers not properly cleared between requests
33
- * Fix: Create entries with custom identifier
34
- * Code cleanup
57
+ ### Added
35
58
  * Adding rdoc
36
59
  * Adding filter by content_type id
37
60
 
61
+ ### Fixed
62
+ * Fix: Headers not properly cleared between requests
63
+ * Fix: Create entries with custom identifier
64
+
65
+ ### Other
66
+ * Code cleanup
38
67
 
39
68
  ## 0.0.1-pre
40
69
  * alpha pre-release
data/Gemfile CHANGED
@@ -7,7 +7,6 @@ group :development do
7
7
  gem 'pry'
8
8
  end
9
9
 
10
-
11
10
  group :test do
12
11
  gem 'simplecov', require: false
13
12
  end
data/README.md CHANGED
@@ -404,6 +404,119 @@ Checking if the entry is published:
404
404
  my_entry.published?
405
405
  ```
406
406
 
407
+ ### Webhooks
408
+
409
+ Retrieving all webhooks from the space:
410
+
411
+ ```ruby
412
+ webhooks = blog_space.webhooks.all
413
+ ```
414
+ Retrieving one webhook by the webhook-id from the space:
415
+
416
+ ```ruby
417
+ blog_post_webhook = blog_space.webhooks.find(webhook_id)
418
+ ```
419
+
420
+ Creating a webhook
421
+
422
+ ```ruby
423
+ blog_space.webhooks.create(url: 'https://www.example.com', httpBasicUsername: 'username', httpBasicPassword: 'password')
424
+ ```
425
+
426
+ Updating a webhook
427
+
428
+ ```ruby
429
+ blog_post_webhook.update(url: 'https://www.newlink.com')
430
+ ```
431
+
432
+ Destroying webhook:
433
+
434
+ ```ruby
435
+ blog_post_webhook.destroy
436
+ ```
437
+
438
+ ## Validations
439
+
440
+ #### in
441
+
442
+ Takes an array of values and validates that the field value is in this array.
443
+
444
+ ```ruby
445
+ validation_in = Contentful::Management::Validation.new
446
+ validation_in.in = ['foo', 'bar', 'baz']
447
+ blog_post_content_type.fields.create(id: 'valid', name: 'Testing IN', type: 'Text', validations: [validation_in])
448
+ ```
449
+
450
+ #### size
451
+
452
+ Takes optional min and max parameters and validates the size of the array (number of objects in it).
453
+
454
+ ```ruby
455
+ validation_size = Contentful::Management::Validation.new
456
+ validation_size.size = { min: 10, max: 15 }
457
+ blog_post_content_type.fields.create(id: 'valid', name: 'Test In', type: 'Text', validations: [validation_size])
458
+ ```
459
+
460
+ #### range
461
+
462
+ Takes a string that reflects a JS regex and flags, validates against a string. See JS Reference for the parameters.
463
+
464
+ ```ruby
465
+ validation_range = Contentful::Management::Validation.new
466
+ validation_range.range = { min: 100, max: 150 }
467
+ blog_post_content_type.fields.create(id: 'valid', name: 'Range', type: 'Text', validations: [validation_range])
468
+ ```
469
+
470
+ #### regex
471
+
472
+ Takes a string that reflects a JS regex and flags, validates against a string. See [JS Reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) for the parameters.
473
+
474
+ ```ruby
475
+ validation_regexp = Contentful::Management::Validation.new
476
+ validation_regexp.regexp = {pattern: '^such', flags: 'im'}
477
+ blog_post_content_type.fields.create(id: 'valid', name: 'Regex', type: 'Text', validations: [validation_regexp])
478
+ ```
479
+
480
+ #### linkContentType
481
+
482
+ Takes an array of content type ids and validates that the link points to an entry of that content type.
483
+
484
+ ```ruby
485
+ validation_link_content_type = Contentful::Management::Validation.new
486
+ validation_link_content_type.link_content_type = ['post_content_type_id']
487
+ blog_post_content_type.fields.create(id: 'entry', name: 'Regex', type: 'Entry', validations: [validation_link_content_type])
488
+ ```
489
+
490
+ #### linkMimetypeGroup
491
+
492
+ Takes a MimeType group name and validates that the link points to an asset of this group.
493
+
494
+ ```ruby
495
+ validation_link_mimetype_group = Contentful::Management::Validation.new
496
+ validation_link_mimetype_group.link_mimetype_group = 'image'
497
+ content_type.fields.create(id: 'asset', validations: [validation_link_mimetype_group])
498
+ ```
499
+
500
+ #### present
501
+
502
+ Validates that a value is present.
503
+
504
+ ```ruby
505
+ validation_present = Contentful::Management::Validation.new
506
+ validation_present.present = true
507
+ content_type.fields.create(id: 'number', validations: [validation_present])
508
+ ```
509
+
510
+ #### linkField
511
+
512
+ Validates that the property is a link (must not be a valid link, just that it looks like one).
513
+
514
+ ```ruby
515
+ validation_link_field = Contentful::Management::Validation.new
516
+ validation_link_field.link_field = true
517
+ content_type.fields.create(id: 'entry', validations: [validation_link_field])
518
+ ```
519
+
407
520
  ### Pagination
408
521
 
409
522
  ```ruby
@@ -412,6 +525,23 @@ blog_space.assets.all(limit: 5).next_page
412
525
  blog_space.entries.all(limit: 5).next_page
413
526
  ```
414
527
 
528
+ ## Logging
529
+
530
+ Logging is disabled by default, it can be enabled by setting a logger instance and a logging severity.
531
+
532
+ ```ruby
533
+ client = Contentful::Management::Client.new('access_token', logger: logger_instance, log_level: Logger::DEBUG)
534
+ ```
535
+
536
+ Example loggers:
537
+
538
+ ```ruby
539
+ Rails.logger
540
+ Logger.new('logfile.log')
541
+ ```
542
+
543
+ 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.
544
+
415
545
  ## Contributing
416
546
 
417
547
  1. Fork it ( https://github.com/[my-github-username]/contentful-management/fork )
data/Rakefile CHANGED
@@ -1,4 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
1
  require 'bundler/gem_tasks'
3
2
  require 'rubygems'
4
3
  require 'rake'
@@ -1,4 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
1
  lib = File.expand_path('../lib', __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'contentful/management/version'
data/examples/blog.rb CHANGED
@@ -16,7 +16,7 @@ organization = 'organization id'
16
16
 
17
17
  require 'contentful/management'
18
18
 
19
- client = Contentful::Management::Client.new(access_token)
19
+ Contentful::Management::Client.new(access_token)
20
20
 
21
21
  blog_space = Contentful::Management::Space.create(name: 'Blog', organization_id: organization)
22
22
  # blog_space = Contentful::Management::Space.find('zlso53r1zad6')
@@ -1,4 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
1
  require 'contentful/management/version'
3
2
  require 'contentful/management/support'
4
3
  require 'contentful/management/client'
@@ -1,4 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
1
  require_relative 'resource'
3
2
  require_relative 'resource/array_like'
4
3
 
@@ -1,4 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
1
  require_relative 'resource'
3
2
  require_relative 'resource/asset_fields'
4
3
  require_relative 'resource/fields'
@@ -18,7 +17,10 @@ module Contentful
18
17
  # Takes an id of space and an optional hash of query options
19
18
  # Returns a Contentful::Management::Array of Contentful::Management::Asset.
20
19
  def self.all(space_id, query = {})
21
- request = Request.new("/#{ space_id }/assets", query)
20
+ request = Request.new(
21
+ "/#{ space_id }/assets",
22
+ query
23
+ )
22
24
  response = request.get
23
25
  result = ResourceBuilder.new(response, {}, {})
24
26
  result.run
@@ -46,7 +48,10 @@ module Contentful
46
48
  asset.description = attributes[:description] if attributes[:description]
47
49
  asset.file = attributes[:file] if attributes[:file]
48
50
 
49
- request = Request.new("/#{ space_id }/assets/#{ attributes[:id] || ''}", fields: asset.fields_for_query)
51
+ request = Request.new(
52
+ "/#{ space_id }/assets/#{ attributes[:id]}",
53
+ fields: asset.fields_for_query
54
+ )
50
55
  response = attributes[:id].nil? ? request.post : request.put
51
56
  result = ResourceBuilder.new(response, {}, {}).run
52
57
  result.locale = locale if locale
@@ -56,7 +61,12 @@ module Contentful
56
61
  # Processing an Asset file
57
62
  def process_file
58
63
  instance_variable_get(:@fields).keys.each do |locale|
59
- request = Request.new("/#{ space.id }/assets/#{ id }/files/#{ locale }/process", {}, id = nil, version: sys[:version])
64
+ request = Request.new(
65
+ "/#{ space.id }/assets/#{ id }/files/#{ locale }/process",
66
+ {},
67
+ id = nil,
68
+ version: sys[:version]
69
+ )
60
70
  request.put
61
71
  end
62
72
  sys[:version] += 1
@@ -70,7 +80,12 @@ module Contentful
70
80
  self.title = attributes[:title] if attributes[:title]
71
81
  self.description = attributes[:description] if attributes[:description]
72
82
  self.file = attributes[:file] if attributes[:file]
73
- request = Request.new("/#{ space.id }/assets/#{ id }", {fields: fields_for_query}, id = nil, version: sys[:version])
83
+ request = Request.new(
84
+ "/#{ space.id }/assets/#{ id }",
85
+ {fields: fields_for_query},
86
+ id = nil,
87
+ version: sys[:version]
88
+ )
74
89
  response = request.put
75
90
  result = ResourceBuilder.new(response, {}, {}).run
76
91
  refresh_data(result)
@@ -79,11 +94,11 @@ module Contentful
79
94
  # If an asset is a new object gets created in the Contentful, otherwise the existing asset gets updated.
80
95
  # See README for details.
81
96
  def save
82
- if id.nil?
97
+ if id
98
+ update(title: title, description: description, file: file)
99
+ else
83
100
  new_instance = self.class.create(sys[:space].id, fields: instance_variable_get(:@fields))
84
101
  refresh_data(new_instance)
85
- else
86
- update(title: title, description: description, file: file)
87
102
  end
88
103
  end
89
104
 
@@ -103,7 +118,12 @@ module Contentful
103
118
  # Publishes an asset.
104
119
  # Returns a Contentful::Management::Asset.
105
120
  def publish
106
- request = Request.new("/#{ space.id }/assets/#{ id }/published", {}, id = nil, version: sys[:version])
121
+ request = Request.new(
122
+ "/#{ space.id }/assets/#{ id }/published",
123
+ {},
124
+ id = nil,
125
+ version: sys[:version]
126
+ )
107
127
  response = request.put
108
128
  result = ResourceBuilder.new(response, {}, {}).run
109
129
  refresh_data(result)
@@ -112,7 +132,12 @@ module Contentful
112
132
  # Unpublishes an asset.
113
133
  # Returns a Contentful::Management::Asset.
114
134
  def unpublish
115
- request = Request.new("/#{ space.id }/assets/#{ id }/published", {}, id = nil, version: sys[:version])
135
+ request = Request.new(
136
+ "/#{ space.id }/assets/#{ id }/published",
137
+ {},
138
+ id = nil,
139
+ version: sys[:version]
140
+ )
116
141
  response = request.delete
117
142
  result = ResourceBuilder.new(response, {}, {}).run
118
143
  refresh_data(result)
@@ -121,7 +146,12 @@ module Contentful
121
146
  # Archive an asset.
122
147
  # Returns a Contentful::Management::Asset.
123
148
  def archive
124
- request = Request.new("/#{ space.id }/assets/#{ id }/archived", {}, id = nil, version: sys[:version])
149
+ request = Request.new(
150
+ "/#{ space.id }/assets/#{ id }/archived",
151
+ {},
152
+ id = nil,
153
+ version: sys[:version]
154
+ )
125
155
  response = request.put
126
156
  result = ResourceBuilder.new(response, {}, {}).run
127
157
  refresh_data(result)
@@ -130,7 +160,12 @@ module Contentful
130
160
  # Unarchvie an asset.
131
161
  # Returns a Contentful::Management::Asset.
132
162
  def unarchive
133
- request = Request.new("/#{ space.id }/assets/#{ id }/archived", {}, id = nil, version: sys[:version])
163
+ request = Request.new(
164
+ "/#{ space.id }/assets/#{ id }/archived",
165
+ {},
166
+ id = nil,
167
+ version: sys[:version]
168
+ )
134
169
  response = request.delete
135
170
  result = ResourceBuilder.new(response, {}, {}).run
136
171
  refresh_data(result)
@@ -139,13 +174,13 @@ module Contentful
139
174
  # Checks if an asset is published.
140
175
  # Returns true if published.
141
176
  def published?
142
- !sys[:publishedAt].nil?
177
+ sys[:publishedAt] ? true : false
143
178
  end
144
179
 
145
180
  # Checks if an asset is archvied.
146
181
  # Returns true if archived.
147
182
  def archived?
148
- !sys[:archivedAt].nil?
183
+ sys[:archivedAt] ? true : false
149
184
  end
150
185
 
151
186
  # Returns currently supported local or default locale.
@@ -157,11 +192,19 @@ module Contentful
157
192
  def fields_for_query
158
193
  self.class.fields_coercions.keys.each_with_object({}) do |field_name, results|
159
194
  results[field_name] = @fields.each_with_object({}) do |(locale, fields), field_results|
160
- field_results[locale] = field_name == :file ? (fields[field_name] ? fields[field_name].properties : nil) : fields[field_name]
195
+ field_results[locale] = get_value_from(fields, field_name)
161
196
  end
162
197
  end
163
198
  end
164
199
 
200
+ def get_value_from(fields, field_name)
201
+ if field_name == :file
202
+ fields[field_name].properties if fields[field_name]
203
+ else
204
+ fields[field_name]
205
+ end
206
+ end
207
+
165
208
  # Returns the image url of an asset
166
209
  # Allows you to pass in the following options for image resizing:
167
210
  # :width
@@ -171,17 +214,13 @@ module Contentful
171
214
  # See https://www.contentful.com/developers/documentation/content-delivery-api/#image-asset-resizing
172
215
  def image_url(options = {})
173
216
  query = {
174
- w: options[:w] || options[:width],
175
- h: options[:h] || options[:height],
176
- fm: options[:fm] || options[:format],
177
- q: options[:q] || options[:quality]
178
- }.reject { |_k, v| v.nil? }
179
-
180
- if query.empty?
181
- file.url
182
- else
183
- "#{file.url}?#{URI.encode_www_form(query)}"
184
- end
217
+ w: options[:w] || options[:width],
218
+ h: options[:h] || options[:height],
219
+ fm: options[:fm] || options[:format],
220
+ q: options[:q] || options[:quality]
221
+ }.select { |_k, value| value }
222
+
223
+ query.empty? ? file.url : "#{file.url}?#{URI.encode_www_form(query)}"
185
224
  end
186
225
  end
187
226
  end