gather_content-api 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +7 -0
  3. data/README.md +161 -35
  4. data/gather_content-api.gemspec +1 -1
  5. data/lib/gather_content.rb +2 -0
  6. data/lib/gather_content/api.rb +16 -15
  7. data/lib/gather_content/config.rb +9 -0
  8. data/lib/gather_content/config/builder.rb +20 -0
  9. data/lib/gather_content/config/elements.rb +15 -0
  10. data/lib/gather_content/config/elements/base.rb +32 -0
  11. data/lib/gather_content/config/elements/choice_checkbox.rb +32 -0
  12. data/lib/gather_content/config/elements/choice_radio.rb +62 -0
  13. data/lib/gather_content/config/elements/files.rb +17 -0
  14. data/lib/gather_content/config/elements/option.rb +30 -0
  15. data/lib/gather_content/config/elements/other_option.rb +26 -0
  16. data/lib/gather_content/config/elements/section.rb +31 -0
  17. data/lib/gather_content/config/elements/text.rb +32 -0
  18. data/lib/gather_content/config/tab.rb +30 -0
  19. data/lib/gather_content/config/tabs.rb +13 -0
  20. data/lib/gather_content/dsl.rb +15 -0
  21. data/lib/gather_content/dsl/base.rb +28 -0
  22. data/lib/gather_content/dsl/choice_checkbox.rb +19 -0
  23. data/lib/gather_content/dsl/choice_radio.rb +25 -0
  24. data/lib/gather_content/dsl/config.rb +16 -0
  25. data/lib/gather_content/dsl/files.rb +12 -0
  26. data/lib/gather_content/dsl/option.rb +29 -0
  27. data/lib/gather_content/dsl/other_option.rb +18 -0
  28. data/lib/gather_content/dsl/section.rb +24 -0
  29. data/lib/gather_content/dsl/tab.rb +54 -0
  30. data/lib/gather_content/dsl/text.rb +29 -0
  31. data/lib/gather_content/error.rb +1 -1
  32. data/lib/gather_content/version.rb +1 -1
  33. data/spec/gather_content/config/builder_spec.rb +42 -0
  34. data/spec/gather_content/config/elements/choice_checkbox_spec.rb +78 -0
  35. data/spec/gather_content/config/elements/choice_radio_spec.rb +175 -0
  36. data/spec/gather_content/config/elements/files_spec.rb +23 -0
  37. data/spec/gather_content/config/elements/option_spec.rb +88 -0
  38. data/spec/gather_content/config/elements/other_option_spec.rb +124 -0
  39. data/spec/gather_content/config/elements/section_spec.rb +80 -0
  40. data/spec/gather_content/config/elements/text_spec.rb +163 -0
  41. data/spec/gather_content/config/tab_spec.rb +120 -0
  42. data/spec/gather_content/dsl/choice_checkbox_spec.rb +25 -0
  43. data/spec/gather_content/dsl/choice_radio_spec.rb +47 -0
  44. data/spec/gather_content/dsl/files_spec.rb +12 -0
  45. data/spec/gather_content/dsl/section_spec.rb +37 -0
  46. data/spec/gather_content/dsl/tab_spec.rb +107 -0
  47. data/spec/gather_content/dsl/text_spec.rb +48 -0
  48. data/spec/spec_helper.rb +1 -1
  49. data/spec/support/shared_examples/dsl.rb +63 -0
  50. data/spec/support/shared_examples/element.rb +77 -0
  51. metadata +63 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bc4fa4c68863fe799d804ff07fe209eff1e8893b
4
- data.tar.gz: fe246d59f62d044e5fb78fc75204490dd8596dcc
3
+ metadata.gz: b8eb9792ba4691f554b35b13a8bbc8826a46558e
4
+ data.tar.gz: bdc42f9dd191a88289976ba3d4aa0c03e6d29f7a
5
5
  SHA512:
6
- metadata.gz: 1163d48c41a4648d40550e237b4d5f8a58e9b3e63ccf8a526880e1bbf8c1c1cd55588937f28975b947eff64751bd25cfab03e6f13456ac84dd5702e0bb8b0b17
7
- data.tar.gz: affabeb1a6be1740294f12aad415688305dbc08a0f46949a8d60be34ac719bd58647964817d3aa9cbc0c5ff2778bb9a472d452b354af00e4fe0ff608e4680b07
6
+ metadata.gz: f34fa4a9124bae94dfc285dc74debfa767abfa5e222f02b21b21faa5122a878e0cd3fd192e6fec0a0ec8f68cf8c0d89372420158bab4bbe9c4e07e8d7c8f2f29
7
+ data.tar.gz: 4306b9229d0828ba7b8aafe78c3647c3aeb7fb44b263f3056f65c6cf70fb396eb8ed091ed94c85eb2a2cf4a5693c9e32ec5e2aeab56e5a76f0aeb8720db7bb24
@@ -0,0 +1,7 @@
1
+ Copyright 2018 NYU Division of Libraries
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,7 +1,6 @@
1
1
  # GatherContent Ruby Client
2
2
 
3
3
  [![Build Status](https://travis-ci.org/NYULibraries/gather_content-api.svg?branch=master)](https://travis-ci.org/NYULibraries/gather_content-api)
4
- [![Dependency Status](https://gemnasium.com/badges/github.com/NYULibraries/gather_content-api.svg)](https://gemnasium.com/github.com/NYULibraries/gather_content-api)
5
4
  [![Maintainability](https://api.codeclimate.com/v1/badges/ace52f297d3f4160799d/maintainability)](https://codeclimate.com/github/NYULibraries/gather_content-api/maintainability)
6
5
  [![Coverage Status](https://coveralls.io/repos/github/NYULibraries/gather_content-api/badge.svg?branch=master)](https://coveralls.io/github/NYULibraries/gather_content-api?branch=master)
7
6
 
@@ -221,7 +220,8 @@ item["name"]
221
220
 
222
221
  Creates a new Item within a particular Project.
223
222
 
224
- The config object (if supplied) should be a Ruby Hash representation of the [configuration](https://docs.gathercontent.com/reference#the-config-field).
223
+ Use the handy [DSL](#config-dsl) to generate a [config field](https://docs.gathercontent.com/reference#the-config-field).
224
+
225
225
 
226
226
  If successful, will return the newly created item.
227
227
 
@@ -232,22 +232,22 @@ require 'gather_content'
232
232
 
233
233
  project_id = 123456
234
234
 
235
- config = [{
236
- "label": "Content",
237
- "name": "tab1",
238
- "hidden": false,
239
- "elements": [{
240
- "type": "text",
241
- "name": "el1",
242
- "required": false,
243
- "label": "Blog post",
244
- "value": "Hello world",
245
- "microcopy": "",
246
- "limit_type": "words",
247
- "limit": 1000,
248
- "plain_text": false
249
- }]
250
- }]
235
+ config = GatherContent::Config::Builder.build do
236
+ label "Content"
237
+ name "tab1"
238
+ hidden false
239
+
240
+ text do
241
+ name "el1"
242
+ required false
243
+ label "Blog post"
244
+ value "Hello world"
245
+ microcopy ""
246
+ limit_type :words
247
+ limit 1000
248
+ plain_text false
249
+ end
250
+ end
251
251
 
252
252
  begin
253
253
  i = GatherContent::Api::Items.new(project_id)
@@ -269,7 +269,7 @@ end
269
269
 
270
270
  Saves an Item with the newly updated data. It expects a valid configuration structure, otherwise the save request will not be accepted by the API.
271
271
 
272
- The config object should be a Ruby Hash representation of the [configuration](https://docs.gathercontent.com/reference#the-config-field).
272
+ Use the handy [DSL](#config-dsl) to generate a [config field](https://docs.gathercontent.com/reference#the-config-field).
273
273
 
274
274
  ```ruby
275
275
  require 'gather_content'
@@ -277,22 +277,22 @@ require 'gather_content'
277
277
  item_id = 123456
278
278
  item = GatherContent::Api::Item.new(item_id)
279
279
 
280
- config = [{
281
- "label": "Content",
282
- "name": "tab1",
283
- "hidden": false,
284
- "elements": [{
285
- "type": "text",
286
- "name": "el1",
287
- "required": false,
288
- "label": "Blog post",
289
- "value": "Hello world",
290
- "microcopy": "",
291
- "limit_type": "words",
292
- "limit": 1000,
293
- "plain_text": false
294
- }]
295
- }]
280
+ config = GatherContent::Config::Builder.build do
281
+ label "Content"
282
+ name "tab1"
283
+ hidden false
284
+
285
+ text do
286
+ name "el1"
287
+ required false
288
+ label "Blog post"
289
+ value "Hello world"
290
+ microcopy ""
291
+ limit_type :words
292
+ limit 1000
293
+ plain_text false
294
+ end
295
+ end
296
296
 
297
297
  begin
298
298
  item.save(config)
@@ -393,3 +393,129 @@ template["id"]
393
393
  template["name"]
394
394
  => "Blog theme"
395
395
  ```
396
+
397
+ ## Config DSL
398
+
399
+ This gem also includes a handy DSL to make creating configurations simple.
400
+
401
+ ### Getting started
402
+
403
+ Every configuration object starts with some tabs (You can define as many as you like)
404
+
405
+ ```ruby
406
+ config = GatherContent::Config::Builder.build do
407
+ tab do
408
+ name "website" # Required. Must be unique.
409
+ label "Website" # Required
410
+ hidden false
411
+ end
412
+
413
+ tab do
414
+ name "email"
415
+ label "Email"
416
+ hidden: false
417
+ end
418
+ end
419
+ ```
420
+
421
+ ### Tabs
422
+
423
+ Tabs can have sections, text, files, radio buttons and checkboxes
424
+
425
+ ```ruby
426
+ config = GatherContent::Config::Builder.build do
427
+ tab do
428
+ name "website"
429
+ label "Website"
430
+ hidden false
431
+
432
+ section do
433
+ name "main" # Required. Must be unique.
434
+ label "Main Copy" # Required
435
+ subtitle ""
436
+ end
437
+
438
+ text do
439
+ name "title" # Required. Must be unique.
440
+ label "Title" # Required
441
+ required true
442
+ value ""
443
+ microcopy "The title of the page"
444
+ limit_type :words # Can be :words or :chars
445
+ limit 100 # Must be positive
446
+ plain_text false
447
+ end
448
+
449
+ files do
450
+ name: "images" # Required. Must be unique.
451
+ label "Images" # Required
452
+ required false
453
+ microcopy "Upload any images referenced in the post"
454
+ end
455
+
456
+ choice_radio do
457
+ name "post_type"
458
+ label "Post type"
459
+ required true
460
+ microcopy "What type of post is this?"
461
+ option do
462
+ name "regular"
463
+ label "Regular page"
464
+ selected true
465
+ end
466
+ option do
467
+ name "blog"
468
+ label "Blog Post"
469
+ selected false
470
+ end
471
+ end
472
+
473
+ choice_checkbox do
474
+ name "notify"
475
+ label "Notify on publish"
476
+ required true
477
+ microcopy "Who needs to be notified when this post is published?"
478
+
479
+ # This DSL is just regular Ruby, so you can do things like...
480
+ [ 'joe@example.com', 'kathy@example.com', 'sam@example.com' ].each_with_index do |email, index|
481
+ option do
482
+ name "email_#{index}"
483
+ label email
484
+ selected false
485
+ end
486
+ end
487
+ end
488
+ end
489
+ end
490
+ ```
491
+
492
+ ### Radio choices with an "other" option
493
+
494
+ Use other_option to define a user definable "other" option. This block MUST be the last one in the set.
495
+
496
+ If the option is selected, you need to supply a value.
497
+
498
+ ```ruby
499
+ choice_radio do
500
+ name "post_type"
501
+ label "Post type"
502
+ required false
503
+ microcopy "What type of post is this?"
504
+ option do
505
+ name "regular"
506
+ label "Regular page"
507
+ selected false # Only one radio option can be selected at a time.
508
+ end
509
+ option do
510
+ name "blog"
511
+ label "Blog Post"
512
+ selected false
513
+ end
514
+ other_option do
515
+ name "other"
516
+ label "Other"
517
+ selected true
518
+ value "Push notification" # If this option is selected, you need to supply a value.
519
+ end
520
+ end
521
+ ```
@@ -19,7 +19,7 @@ Gem::Specification.new do |gem|
19
19
  gem.licenses = ['MIT']
20
20
 
21
21
  gem.required_ruby_version = '>= 1.9.3'
22
- gem.add_dependency 'rake', '~> 10'
22
+ gem.add_development_dependency 'rake', '~> 10'
23
23
  gem.add_dependency 'rest-client', '~> 2'
24
24
  gem.add_dependency 'faraday', '~> 0'
25
25
  end
@@ -2,4 +2,6 @@ require 'json'
2
2
  require 'faraday'
3
3
  require 'gather_content/version'
4
4
  require 'gather_content/api'
5
+ require 'gather_content/config'
6
+ require 'gather_content/dsl'
5
7
  require 'gather_content/error'
@@ -1,19 +1,20 @@
1
+ require 'gather_content/api/config'
2
+ require 'gather_content/api/base'
3
+ require 'gather_content/api/me'
4
+ require 'gather_content/api/accounts'
5
+ require 'gather_content/api/account'
6
+ require 'gather_content/api/projects'
7
+ require 'gather_content/api/project'
8
+ require 'gather_content/api/items'
9
+ require 'gather_content/api/item'
10
+ require 'gather_content/api/statuses'
11
+ require 'gather_content/api/status'
12
+ require 'gather_content/api/files'
13
+ require 'gather_content/api/file'
14
+ require 'gather_content/api/templates'
15
+ require 'gather_content/api/template'
16
+
1
17
  module GatherContent
2
18
  module Api
3
- autoload :Config, 'gather_content/api/config'
4
- autoload :Base, 'gather_content/api/base'
5
- autoload :Me, 'gather_content/api/me'
6
- autoload :Accounts, 'gather_content/api/accounts'
7
- autoload :Account, 'gather_content/api/account'
8
- autoload :Projects, 'gather_content/api/projects'
9
- autoload :Project, 'gather_content/api/project'
10
- autoload :Items, 'gather_content/api/items'
11
- autoload :Item, 'gather_content/api/item'
12
- autoload :Statuses, 'gather_content/api/statuses'
13
- autoload :Status, 'gather_content/api/status'
14
- autoload :Files, 'gather_content/api/files'
15
- autoload :File, 'gather_content/api/file'
16
- autoload :Templates, 'gather_content/api/templates'
17
- autoload :Template, 'gather_content/api/template'
18
19
  end
19
20
  end
@@ -0,0 +1,9 @@
1
+ require 'gather_content/config/builder'
2
+ require 'gather_content/config/tabs'
3
+ require 'gather_content/config/tab'
4
+ require 'gather_content/config/elements'
5
+
6
+ module GatherContent
7
+ module Config
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ require 'gather_content/dsl/config'
2
+
3
+ module GatherContent
4
+ module Config
5
+ class Builder
6
+ attr_accessor :tabs
7
+
8
+ def initialize
9
+ self.tabs = GatherContent::Config::Tabs.new
10
+ end
11
+
12
+ def self.build(&block)
13
+ instance = self.new
14
+ dsl = GatherContent::DSL::Config.new(instance)
15
+ dsl.instance_eval(&block)
16
+ instance.tabs
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ module GatherContent
2
+ module Config
3
+ module Elements
4
+ autoload :Base, 'gather_content/config/elements/base'
5
+ autoload :ChoiceCheckbox, 'gather_content/config/elements/choice_checkbox'
6
+ autoload :ChoiceRadio, 'gather_content/config/elements/choice_radio'
7
+ autoload :Files, 'gather_content/config/elements/files'
8
+ autoload :Option, 'gather_content/config/elements/option'
9
+ autoload :OtherOption, 'gather_content/config/elements/other_option'
10
+ autoload :Section, 'gather_content/config/elements/section'
11
+ autoload :Tab, 'gather_content/config/elements/tab'
12
+ autoload :Text, 'gather_content/config/elements/text'
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,32 @@
1
+ module GatherContent
2
+ module Config
3
+ module Element
4
+ class Base
5
+ attr_accessor :name, :required, :label, :microcopy
6
+
7
+ def initialize(name = nil, required = nil, label = nil, microcopy = nil)
8
+ @name = name
9
+ @required = required
10
+ @label = label
11
+ @microcopy = microcopy
12
+ end
13
+
14
+ def serialize(options = nil)
15
+ raise ArgumentError, "name is required" unless name.present?
16
+ raise ArgumentError, "label is required" unless label.present?
17
+
18
+ {
19
+ name: name,
20
+ required: !!required,
21
+ label: label,
22
+ microcopy: microcopy
23
+ }
24
+ end
25
+
26
+ def to_json(options = nil)
27
+ serialize.to_json(options)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ require 'gather_content/config/elements/base'
2
+ require 'gather_content/config/elements/option'
3
+
4
+ module GatherContent
5
+ module Config
6
+ module Element
7
+ class ChoiceCheckbox < Base
8
+ attr_accessor :options
9
+
10
+ def initialize(name = "", required = false, label = "", microcopy = "")
11
+ super(name, required, label, microcopy)
12
+ @options = []
13
+ end
14
+
15
+ def serialize(_options = nil)
16
+ cleaned = options.select{ |opt| opt.instance_of?(GatherContent::Config::Element::Option) }
17
+ raise ArgumentError, "You need to supply at least one option" if options.size == 0
18
+ raise ArgumentError, "Options can only be GatherContent::Config::Element::Option" if cleaned.size == 0
19
+
20
+ super.merge({
21
+ type: 'choice_checkbox',
22
+ options: options.map{ |el| el.serialize(_options) }
23
+ })
24
+ end
25
+
26
+ def to_json
27
+ serialize.to_json
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,62 @@
1
+ require 'gather_content/config/elements/base'
2
+ require 'gather_content/config/elements/option'
3
+ require 'gather_content/config/elements/other_option'
4
+
5
+ module GatherContent
6
+ module Config
7
+ module Element
8
+ class ChoiceRadio < Base
9
+ attr_accessor :other_option, :options
10
+
11
+ def initialize(name = "", required = false, label = "", microcopy = "", other_option = false)
12
+ super(name, required, label, microcopy)
13
+ @other_option = other_option
14
+ @options = []
15
+ end
16
+
17
+ def serialize(_options = nil)
18
+ raise ArgumentError, "You need to supply at least one option" if options.size == 0
19
+
20
+ if other_option
21
+ verify_other_option_types!(options)
22
+ else
23
+ verify_option_types!(options)
24
+ end
25
+
26
+ verify_options!(options)
27
+
28
+ super.merge({
29
+ type: 'choice_radio',
30
+ other_option: !!other_option,
31
+ options: options.map{ |el| el.serialize(_options) }
32
+ })
33
+ end
34
+
35
+ def to_json(options = nil)
36
+ serialize.to_json(options)
37
+ end
38
+
39
+
40
+ private
41
+ def verify_other_option_types!(options)
42
+ cleaned = options.select{ |opt| opt.is_a?(GatherContent::Config::Element::Option) }
43
+ last = cleaned.pop
44
+
45
+ raise ArgumentError, "Options can only be GatherContent::Config::Element::Option" if cleaned.size == 0
46
+ raise ArgumentError, "The last option must be a GatherContent::Config::Element::OptionOther" unless last.instance_of?(GatherContent::Config::Element::OtherOption)
47
+ end
48
+
49
+ def verify_option_types!(options)
50
+ cleaned = options.select{ |opt| opt.instance_of?(GatherContent::Config::Element::Option) }
51
+
52
+ raise ArgumentError, "Options can only be GatherContent::Config::Element::Option" if cleaned.size == 0
53
+ end
54
+
55
+ def verify_options!(options)
56
+ selected = options.select{ |opt| opt.selected }
57
+ raise ArgumentError, "You can't select more than one ChoiceRadio Option" if selected.size > 1
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end