renderful 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 838224d26adb2fc0cdd709b1bda084a4d0c0ee5c091207791587f3c0a8e176be
4
+ data.tar.gz: d23fc8b35ddeddbaa23acad085e8b945f6a5983c737cea1e90c1d46bd7e88da9
5
+ SHA512:
6
+ metadata.gz: 35427988e04a3197a4dac2080d410d9708ebd3fa12aec1f509a2bd9838f303825733a0e7214578d0c7aca97aba067811d537a461719a0d0d83b2ed2c2f3f1600
7
+ data.tar.gz: e1a364c1e599c1d0df64015175d5dd9e41b969448e0a58679b66de16d9d0fe196a5acfba841efe46f344d775ae0fe0def75f7f6cff12ef1cffff90137b616e1d
@@ -0,0 +1,32 @@
1
+ version: 2
2
+ jobs:
3
+ build:
4
+ docker:
5
+ - image: circleci/ruby:2.6.2
6
+ environment:
7
+ BUNDLE_JOBS: 3
8
+ BUNDLE_RETRY: 3
9
+ BUNDLE_PATH: vendor/bundle
10
+ steps:
11
+ - checkout
12
+ - restore_cache:
13
+ keys:
14
+ - renderful-v1-{{ checksum "Gemfile.lock" }}
15
+ - renderful-v1-
16
+ - run:
17
+ name: Bundle Install
18
+ command: bundle check || bundle install
19
+ - save_cache:
20
+ key: renderful-v1-{{ checksum "Gemfile.lock" }}
21
+ paths:
22
+ - vendor/bundle
23
+ - run:
24
+ name: Run rspec in parallel
25
+ command: |
26
+ bundle exec rspec --profile 10 \
27
+ --format RspecJunitFormatter \
28
+ --out test_results/rspec.xml \
29
+ --format progress \
30
+ $(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)
31
+ - store_test_results:
32
+ path: test_results
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ .rspec_status
data/.hound.yml ADDED
@@ -0,0 +1,5 @@
1
+ fail_on_violations: true
2
+
3
+ rubocop:
4
+ config_file: .rubocop.yml
5
+ version: 0.64.0
@@ -0,0 +1,166 @@
1
+ # Relaxed.Ruby.Style
2
+ ## Version 2.2
3
+
4
+ Style/Alias:
5
+ Enabled: false
6
+ StyleGuide: https://relaxed.ruby.style/#stylealias
7
+
8
+ Style/AsciiComments:
9
+ Enabled: false
10
+ StyleGuide: https://relaxed.ruby.style/#styleasciicomments
11
+
12
+ Style/BeginBlock:
13
+ Enabled: false
14
+ StyleGuide: https://relaxed.ruby.style/#stylebeginblock
15
+
16
+ Style/BlockDelimiters:
17
+ Enabled: false
18
+ StyleGuide: https://relaxed.ruby.style/#styleblockdelimiters
19
+
20
+ Style/CommentAnnotation:
21
+ Enabled: false
22
+ StyleGuide: https://relaxed.ruby.style/#stylecommentannotation
23
+
24
+ Style/Documentation:
25
+ Enabled: false
26
+ StyleGuide: https://relaxed.ruby.style/#styledocumentation
27
+
28
+ Layout/DotPosition:
29
+ Enabled: false
30
+ StyleGuide: https://relaxed.ruby.style/#layoutdotposition
31
+
32
+ Style/DoubleNegation:
33
+ Enabled: false
34
+ StyleGuide: https://relaxed.ruby.style/#styledoublenegation
35
+
36
+ Style/EndBlock:
37
+ Enabled: false
38
+ StyleGuide: https://relaxed.ruby.style/#styleendblock
39
+
40
+ Style/FormatString:
41
+ Enabled: false
42
+ StyleGuide: https://relaxed.ruby.style/#styleformatstring
43
+
44
+ Style/IfUnlessModifier:
45
+ Enabled: false
46
+ StyleGuide: https://relaxed.ruby.style/#styleifunlessmodifier
47
+
48
+ Style/Lambda:
49
+ Enabled: false
50
+ StyleGuide: https://relaxed.ruby.style/#stylelambda
51
+
52
+ Style/ModuleFunction:
53
+ Enabled: false
54
+ StyleGuide: https://relaxed.ruby.style/#stylemodulefunction
55
+
56
+ Style/MultilineBlockChain:
57
+ Enabled: false
58
+ StyleGuide: https://relaxed.ruby.style/#stylemultilineblockchain
59
+
60
+ Style/NegatedIf:
61
+ Enabled: false
62
+ StyleGuide: https://relaxed.ruby.style/#stylenegatedif
63
+
64
+ Style/NegatedWhile:
65
+ Enabled: false
66
+ StyleGuide: https://relaxed.ruby.style/#stylenegatedwhile
67
+
68
+ Style/ParallelAssignment:
69
+ Enabled: false
70
+ StyleGuide: https://relaxed.ruby.style/#styleparallelassignment
71
+
72
+ Style/PercentLiteralDelimiters:
73
+ Enabled: false
74
+ StyleGuide: https://relaxed.ruby.style/#stylepercentliteraldelimiters
75
+
76
+ Style/PerlBackrefs:
77
+ Enabled: false
78
+ StyleGuide: https://relaxed.ruby.style/#styleperlbackrefs
79
+
80
+ Style/Semicolon:
81
+ Enabled: false
82
+ StyleGuide: https://relaxed.ruby.style/#stylesemicolon
83
+
84
+ Style/SignalException:
85
+ Enabled: false
86
+ StyleGuide: https://relaxed.ruby.style/#stylesignalexception
87
+
88
+ Style/SingleLineBlockParams:
89
+ Enabled: false
90
+ StyleGuide: https://relaxed.ruby.style/#stylesinglelineblockparams
91
+
92
+ Style/SingleLineMethods:
93
+ Enabled: false
94
+ StyleGuide: https://relaxed.ruby.style/#stylesinglelinemethods
95
+
96
+ Layout/SpaceBeforeBlockBraces:
97
+ Enabled: false
98
+ StyleGuide: https://relaxed.ruby.style/#layoutspacebeforeblockbraces
99
+
100
+ Layout/SpaceInsideParens:
101
+ Enabled: false
102
+ StyleGuide: https://relaxed.ruby.style/#layoutspaceinsideparens
103
+
104
+ Style/SpecialGlobalVars:
105
+ Enabled: false
106
+ StyleGuide: https://relaxed.ruby.style/#stylespecialglobalvars
107
+
108
+ Style/StringLiterals:
109
+ Enabled: false
110
+ StyleGuide: https://relaxed.ruby.style/#stylestringliterals
111
+
112
+ Style/TrailingCommaInArguments:
113
+ Enabled: false
114
+ StyleGuide: https://relaxed.ruby.style/#styletrailingcommainarguments
115
+
116
+ Style/TrailingCommaInArrayLiteral:
117
+ Enabled: false
118
+ StyleGuide: https://relaxed.ruby.style/#styletrailingcommainarrayliteral
119
+
120
+ Style/TrailingCommaInHashLiteral:
121
+ Enabled: false
122
+ StyleGuide: https://relaxed.ruby.style/#styletrailingcommainhashliteral
123
+
124
+ Style/WhileUntilModifier:
125
+ Enabled: false
126
+ StyleGuide: https://relaxed.ruby.style/#stylewhileuntilmodifier
127
+
128
+ Style/WordArray:
129
+ Enabled: false
130
+ StyleGuide: https://relaxed.ruby.style/#stylewordarray
131
+
132
+ Lint/AmbiguousRegexpLiteral:
133
+ Enabled: false
134
+ StyleGuide: https://relaxed.ruby.style/#lintambiguousregexpliteral
135
+
136
+ Lint/AssignmentInCondition:
137
+ Enabled: false
138
+ StyleGuide: https://relaxed.ruby.style/#lintassignmentincondition
139
+
140
+ Metrics/AbcSize:
141
+ Enabled: false
142
+
143
+ Metrics/BlockNesting:
144
+ Enabled: false
145
+
146
+ Metrics/ClassLength:
147
+ Enabled: false
148
+
149
+ Metrics/ModuleLength:
150
+ Enabled: false
151
+
152
+ Metrics/CyclomaticComplexity:
153
+ Enabled: false
154
+
155
+ Metrics/LineLength:
156
+ Enabled: false
157
+
158
+ Metrics/MethodLength:
159
+ Enabled: false
160
+
161
+ Metrics/ParameterLists:
162
+ Enabled: false
163
+
164
+ Metrics/PerceivedComplexity:
165
+ Enabled: false
166
+
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ inherit_from:
2
+ - https://relaxed.ruby.style/rubocop.yml
3
+
4
+ require: rubocop-rspec
5
+
6
+ Metrics/BlockLength:
7
+ Enabled: false
8
+
9
+ Layout/IndentHash:
10
+ EnforcedStyle: consistent
11
+
12
+ RSpec/ExampleLength:
13
+ Enabled: false
14
+
15
+ Layout/ClosingParenthesisIndentation:
16
+ Enabled: false
17
+
18
+ Layout/FirstParameterIndentation:
19
+ EnforcedStyle: consistent
20
+
21
+ RSpec/NestedGroups:
22
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project
6
+ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ [Unreleased]: https://github.com/nebulab/renderful
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in renderful.gemspec
8
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,190 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ renderful (0.1.0)
5
+ contentful (~> 2.11)
6
+ rails (~> 5.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actioncable (5.2.3)
12
+ actionpack (= 5.2.3)
13
+ nio4r (~> 2.0)
14
+ websocket-driver (>= 0.6.1)
15
+ actionmailer (5.2.3)
16
+ actionpack (= 5.2.3)
17
+ actionview (= 5.2.3)
18
+ activejob (= 5.2.3)
19
+ mail (~> 2.5, >= 2.5.4)
20
+ rails-dom-testing (~> 2.0)
21
+ actionpack (5.2.3)
22
+ actionview (= 5.2.3)
23
+ activesupport (= 5.2.3)
24
+ rack (~> 2.0)
25
+ rack-test (>= 0.6.3)
26
+ rails-dom-testing (~> 2.0)
27
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
28
+ actionview (5.2.3)
29
+ activesupport (= 5.2.3)
30
+ builder (~> 3.1)
31
+ erubi (~> 1.4)
32
+ rails-dom-testing (~> 2.0)
33
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
34
+ activejob (5.2.3)
35
+ activesupport (= 5.2.3)
36
+ globalid (>= 0.3.6)
37
+ activemodel (5.2.3)
38
+ activesupport (= 5.2.3)
39
+ activerecord (5.2.3)
40
+ activemodel (= 5.2.3)
41
+ activesupport (= 5.2.3)
42
+ arel (>= 9.0)
43
+ activestorage (5.2.3)
44
+ actionpack (= 5.2.3)
45
+ activerecord (= 5.2.3)
46
+ marcel (~> 0.3.1)
47
+ activesupport (5.2.3)
48
+ concurrent-ruby (~> 1.0, >= 1.0.2)
49
+ i18n (>= 0.7, < 2)
50
+ minitest (~> 5.1)
51
+ tzinfo (~> 1.1)
52
+ addressable (2.6.0)
53
+ public_suffix (>= 2.0.2, < 4.0)
54
+ arel (9.0.0)
55
+ ast (2.4.0)
56
+ builder (3.2.3)
57
+ concurrent-ruby (1.1.5)
58
+ contentful (2.12.0)
59
+ http (> 0.8, < 4.0)
60
+ multi_json (~> 1)
61
+ crass (1.0.4)
62
+ diff-lcs (1.3)
63
+ domain_name (0.5.20180417)
64
+ unf (>= 0.0.5, < 1.0.0)
65
+ erubi (1.8.0)
66
+ globalid (0.4.2)
67
+ activesupport (>= 4.2.0)
68
+ http (3.3.0)
69
+ addressable (~> 2.3)
70
+ http-cookie (~> 1.0)
71
+ http-form_data (~> 2.0)
72
+ http_parser.rb (~> 0.6.0)
73
+ http-cookie (1.0.3)
74
+ domain_name (~> 0.5)
75
+ http-form_data (2.1.1)
76
+ http_parser.rb (0.6.0)
77
+ i18n (1.6.0)
78
+ concurrent-ruby (~> 1.0)
79
+ jaro_winkler (1.5.2)
80
+ loofah (2.2.3)
81
+ crass (~> 1.0.2)
82
+ nokogiri (>= 1.5.9)
83
+ mail (2.7.1)
84
+ mini_mime (>= 0.1.1)
85
+ marcel (0.3.3)
86
+ mimemagic (~> 0.3.2)
87
+ method_source (0.9.2)
88
+ mimemagic (0.3.3)
89
+ mini_mime (1.0.1)
90
+ mini_portile2 (2.4.0)
91
+ minitest (5.11.3)
92
+ multi_json (1.13.1)
93
+ nio4r (2.3.1)
94
+ nokogiri (1.10.2)
95
+ mini_portile2 (~> 2.4.0)
96
+ parallel (1.17.0)
97
+ parser (2.6.2.1)
98
+ ast (~> 2.4.0)
99
+ powerpack (0.1.2)
100
+ public_suffix (3.0.3)
101
+ rack (2.0.7)
102
+ rack-test (1.1.0)
103
+ rack (>= 1.0, < 3)
104
+ rails (5.2.3)
105
+ actioncable (= 5.2.3)
106
+ actionmailer (= 5.2.3)
107
+ actionpack (= 5.2.3)
108
+ actionview (= 5.2.3)
109
+ activejob (= 5.2.3)
110
+ activemodel (= 5.2.3)
111
+ activerecord (= 5.2.3)
112
+ activestorage (= 5.2.3)
113
+ activesupport (= 5.2.3)
114
+ bundler (>= 1.3.0)
115
+ railties (= 5.2.3)
116
+ sprockets-rails (>= 2.0.0)
117
+ rails-dom-testing (2.0.3)
118
+ activesupport (>= 4.2.0)
119
+ nokogiri (>= 1.6)
120
+ rails-html-sanitizer (1.0.4)
121
+ loofah (~> 2.2, >= 2.2.2)
122
+ railties (5.2.3)
123
+ actionpack (= 5.2.3)
124
+ activesupport (= 5.2.3)
125
+ method_source
126
+ rake (>= 0.8.7)
127
+ thor (>= 0.19.0, < 2.0)
128
+ rainbow (3.0.0)
129
+ rake (10.5.0)
130
+ redis (4.1.0)
131
+ rspec (3.8.0)
132
+ rspec-core (~> 3.8.0)
133
+ rspec-expectations (~> 3.8.0)
134
+ rspec-mocks (~> 3.8.0)
135
+ rspec-core (3.8.0)
136
+ rspec-support (~> 3.8.0)
137
+ rspec-expectations (3.8.2)
138
+ diff-lcs (>= 1.2.0, < 2.0)
139
+ rspec-support (~> 3.8.0)
140
+ rspec-mocks (3.8.0)
141
+ diff-lcs (>= 1.2.0, < 2.0)
142
+ rspec-support (~> 3.8.0)
143
+ rspec-support (3.8.0)
144
+ rspec_junit_formatter (0.4.1)
145
+ rspec-core (>= 2, < 4, != 2.12.0)
146
+ rubocop (0.64.0)
147
+ jaro_winkler (~> 1.5.1)
148
+ parallel (~> 1.10)
149
+ parser (>= 2.5, != 2.5.1.1)
150
+ powerpack (~> 0.1)
151
+ rainbow (>= 2.2.2, < 4.0)
152
+ ruby-progressbar (~> 1.7)
153
+ unicode-display_width (~> 1.4.0)
154
+ rubocop-rspec (1.32.0)
155
+ rubocop (>= 0.60.0)
156
+ ruby-progressbar (1.10.0)
157
+ sprockets (3.7.2)
158
+ concurrent-ruby (~> 1.0)
159
+ rack (> 1, < 3)
160
+ sprockets-rails (3.2.1)
161
+ actionpack (>= 4.0)
162
+ activesupport (>= 4.0)
163
+ sprockets (>= 3.0.0)
164
+ thor (0.20.3)
165
+ thread_safe (0.3.6)
166
+ tzinfo (1.2.5)
167
+ thread_safe (~> 0.1)
168
+ unf (0.1.4)
169
+ unf_ext
170
+ unf_ext (0.0.7.5)
171
+ unicode-display_width (1.4.1)
172
+ websocket-driver (0.7.0)
173
+ websocket-extensions (>= 0.1.0)
174
+ websocket-extensions (0.1.3)
175
+
176
+ PLATFORMS
177
+ ruby
178
+
179
+ DEPENDENCIES
180
+ bundler (~> 1.17)
181
+ rake (~> 10.0)
182
+ redis (~> 4.1)
183
+ renderful!
184
+ rspec (~> 3.0)
185
+ rspec_junit_formatter (~> 0.4.1)
186
+ rubocop (= 0.64.0)
187
+ rubocop-rspec (= 1.32.0)
188
+
189
+ BUNDLED WITH
190
+ 1.17.2
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Nebulab Srls
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,279 @@
1
+ # Renderful
2
+
3
+ [![CircleCI](https://circleci.com/gh/bestmadeco/renderful.svg?style=svg)](https://circleci.com/gh/bestmadeco/renderful)
4
+
5
+ Welcome! Renderful is a rendering engine for [Contentful](https://www.contentful.com) spaces. It
6
+ allows you to map your content types to Ruby objects that take care of rendering your content.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'renderful'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ ```console
19
+ $ bundle
20
+ ```
21
+
22
+ Or install it yourself as:
23
+
24
+ ```console
25
+ $ gem install renderful
26
+ ```
27
+
28
+ Once you have installed the gem, you can configure it like this:
29
+
30
+ ```ruby
31
+ contentful = Contentful::Client.new(
32
+ space: 'CONTENTFUL_SPACE_ID',
33
+ access_token: 'CONTENTFUL_ACCESS_TOKEN',
34
+ )
35
+
36
+ renderful = Renderful.new(
37
+ contentful: contentful,
38
+ renderers: {
39
+ 'jumbotron' => JumbotronRenderer,
40
+ }
41
+ )
42
+ ```
43
+
44
+ ## Usage
45
+
46
+ Suppose you have the `jumbotron` content type in your Contentful space. This content type has the
47
+ `title` and `content` fields, both strings.
48
+
49
+ Let's create the `app/renderers/jumbotron_renderer.rb` file:
50
+
51
+ ```ruby
52
+ class JumbotronRenderer < Renderful::Renderer
53
+ def render
54
+ <<~HTML
55
+ <div class="jumbotron">
56
+ <h1 class="display-4"><%= entry.title %></h1>
57
+ <p class="lead"><%= entry.content %></p>
58
+ </div>
59
+ HTML
60
+ end
61
+ end
62
+ ```
63
+
64
+ You can now render this component by retrieving it from Contentful and rendering it with Renderful:
65
+
66
+ ```ruby
67
+ entry = contentful.entry('jumbotron_entry_id')
68
+ renderful.render(entry)
69
+ ```
70
+
71
+ ### Rich text rendering
72
+
73
+ If you have rich-text fields, you can leverage Contentful's [rich_text_renderer](https://github.com/contentful/rich-text-renderer.rb)
74
+ along with a custom local variable:
75
+
76
+ ```ruby
77
+ class TextBlockRenderer < Renderful::Renderer::Rails
78
+ def html_body
79
+ RichTextRenderer::Renderer.new.render(entry.body)
80
+ end
81
+
82
+ def locals
83
+ { html_body: html_body }
84
+ end
85
+ end
86
+ ```
87
+
88
+ Then, just reference the `html_body` variable as usual:
89
+
90
+ ```erb
91
+ <%# app/views/renderful/_text_block.html.erb %>
92
+ <%= raw html_body %>
93
+ ```
94
+
95
+ ### Nested components
96
+
97
+ What if you want to have a `Grid` component that can contain references to other components? It's
98
+ actually quite simple! Simply create a _References_ field for your content, then recursively render
99
+ all of the content entries contained in that field:
100
+
101
+ ```ruby
102
+ # app/components/grid.rb
103
+ class Grid < Renderful::Renderer
104
+ # This will define a `resolved_blocks` method that reads external references
105
+ # from the `blocks` fields and turns them into Contentful::Entry instances
106
+ resolve :blocks
107
+
108
+ def render
109
+ entries = blocks.map do |block|
110
+ # `client` can be used to access the Renderful::Client instance
111
+ <<~HTML
112
+ <div class="grid-entry">
113
+ #{client.render(block)}
114
+ </div>
115
+ HTML
116
+ end
117
+
118
+ <<~HTML
119
+ <div class="grid">#{entries}</div>
120
+ HTML
121
+ end
122
+ end
123
+ ```
124
+
125
+ ### Caching
126
+
127
+ You can easily cache the output of your renderers by passing a `cache` key when instantiating the
128
+ client. The value of this key should be an object that responds to the following methods:
129
+
130
+ - `#read(key)`
131
+ - `#write(key, value)`
132
+ - `#delete(key)`
133
+ - `#exist?(key)`
134
+
135
+ A Redis cache implementation is included out of the box. Here's an example:
136
+
137
+ ```ruby
138
+ renderful = Renderful.new(
139
+ contentful: contentful,
140
+ cache: Renderful::Cache::Redis.new(Redis.new(url: 'redis://localhost:6379')),
141
+ renderers: {
142
+ 'jumbotron' => JumbotronRenderer
143
+ }
144
+ )
145
+ ```
146
+
147
+ If you are using Rails and want to use the Rails cache store for Renderful, you can simply pass
148
+ `Rails.cache`, which responds to the expected interface:
149
+
150
+ ```ruby
151
+ renderful = Renderful.new(
152
+ contentful: contentful,
153
+ cache: Rails.cache,
154
+ renderers: {
155
+ 'jumbotron' => JumbotronRenderer
156
+ }
157
+ )
158
+ ```
159
+
160
+ #### Cache invalidation
161
+
162
+ The best way to invalidate the cache is through [Contentful webhooks](https://www.contentful.com/developers/docs/concepts/webhooks/).
163
+
164
+ Renderful ships with a framework-agnostic webhook processor you can use to automatically invalidate
165
+ the cache for all updated content:
166
+
167
+ ```ruby
168
+ Renderful::CacheInvalidator.new(renderful).process_webhook(json_body)
169
+ ```
170
+
171
+ This is how you could use it in a Rails controller:
172
+
173
+ ```ruby
174
+ class ContentfulWebhooksController < ApplicationController
175
+ def create
176
+ Renderful::CacheInvalidator.new(RenderfulClient).process_webhook(request.raw_post)
177
+ head :no_content
178
+ end
179
+ end
180
+ ```
181
+
182
+ ### Rails integration
183
+
184
+ If you are using Ruby on Rails and you want to use ERB instead of including HTML in your renderers,
185
+ you can inherit from the Rails renderer:
186
+
187
+ ```ruby
188
+ class JumbotronRenderer < Renderful::Renderer::Rails
189
+ end
190
+ ```
191
+
192
+ Then, create an `app/views/renderful/_jumbotron.html.erb` partial:
193
+
194
+ ```erb
195
+ <div class="jumbotron">
196
+ <h1 class="display-4"><%= entry.title %></h1>
197
+ <p class="lead"><%= entry.content %></p>
198
+ </div>
199
+ ```
200
+
201
+ As you can see, you can access the Contentful entry via the `entry` local variable.
202
+
203
+ #### Custom renderer
204
+
205
+ The Rails renderer uses `ActionController::Base.renderer` by default, but this prevents you from
206
+ using your own helpers in components. If you want to use a different renderer instead, you can
207
+ override the `renderer` method:
208
+
209
+ ```ruby
210
+ class JumbotronRenderer < Renderful::Renderer::Rails
211
+ def renderer
212
+ ApplicationController.renderer
213
+ end
214
+ end
215
+ ```
216
+
217
+ #### Custom locals
218
+
219
+ If you want, you can also add your own locals:
220
+
221
+ ```ruby
222
+ class JumbotronRenderer < Renderful::Renderer::Rails
223
+ def locals
224
+ italian_title = entry.title.gsub(/hello/, 'ciao')
225
+ { italian_title: italian_title }
226
+ end
227
+ end
228
+ ```
229
+
230
+ You would then access them like regular locals:
231
+
232
+ ```erb
233
+ <div class="jumbotron">
234
+ <h1 class="display-4">
235
+ <%= entry.title %>
236
+ (<%= italian_title %>)
237
+ </h1>
238
+ <p class="lead"><%= entry.content %></p>
239
+ </div>
240
+ ```
241
+
242
+ #### Resolution in ERB views
243
+
244
+ If you need to render resolved fields (as in our `Grid` example), you can use `renderer` and
245
+ `client` to access the `Renderful::Renderer` and `Renderful::Client` objects:
246
+
247
+ ```erb
248
+ <%# app/views/renderful/_grid.html.erb %>
249
+ <div class="grid">
250
+ <% renderer.blocks.each do |block| %>
251
+ <div class="grid-entry">
252
+ <%= client.render(block) %>
253
+ </div>
254
+ <% end %>
255
+ </div>
256
+ ```
257
+
258
+ ## Development
259
+
260
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run
261
+ the tests. You can also run `bin/console` for an interactive prompt that will allow you to
262
+ experiment.
263
+
264
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new
265
+ version, update the version number in `version.rb`, and then run `bundle exec rake release`, which
266
+ will create a git tag for the version, push git commits and tags, and push the `.gem` file to
267
+ [rubygems.org](https://rubygems.org).
268
+
269
+ ## Contributing
270
+
271
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bestmadeco/renderful.
272
+
273
+ ## License
274
+
275
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
276
+
277
+ ## Credits
278
+
279
+ Renderful is sponsored and maintained by [Bolt Threads Inc.](https://www.boltthreads.com).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "renderful"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Renderful
4
+ class Cache
5
+ class Redis < Cache
6
+ attr_reader :redis
7
+
8
+ def initialize(redis)
9
+ @redis = redis
10
+ end
11
+
12
+ def exist?(key)
13
+ redis.exists(key)
14
+ end
15
+
16
+ def read(key)
17
+ redis.get(key)
18
+ end
19
+
20
+ def write(key, value)
21
+ redis.set(key, value)
22
+ end
23
+
24
+ def delete(key)
25
+ redis.del(key)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Renderful
4
+ class Cache
5
+ def exist?(_key)
6
+ raise NotImplementedError
7
+ end
8
+
9
+ def read(_key)
10
+ raise NotImplementedError
11
+ end
12
+
13
+ def write(_key, _value)
14
+ raise NotImplementedError
15
+ end
16
+
17
+ def delete(_key)
18
+ raise NotImplementedError
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Renderful
4
+ class CacheInvalidator
5
+ attr_reader :client
6
+
7
+ def initialize(client)
8
+ @client = client
9
+ end
10
+
11
+ def process_webhook(body)
12
+ return unless client.cache
13
+
14
+ params = body.is_a?(String) ? JSON.parse(body) : body
15
+
16
+ client.cache.delete(client.cache_key_for(
17
+ content_type_id: params['sys']['contentType']['sys']['id'],
18
+ entry_id: params['sys']['id'],
19
+ ))
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Renderful
4
+ class Client
5
+ attr_reader :contentful, :renderers, :cache
6
+
7
+ def initialize(contentful:, renderers:, cache: nil)
8
+ @contentful = contentful
9
+ @renderers = renderers
10
+ @cache = cache
11
+ end
12
+
13
+ def render(entry)
14
+ renderer = renderers[entry.content_type.id]
15
+ fail(NoRendererError, entry) unless renderer
16
+
17
+ return cache.read(cache_key_for(entry)) if cache&.exist?(cache_key_for(entry))
18
+
19
+ renderer.new(entry, client: self).render.tap do |output|
20
+ cache&.write(cache_key_for(entry), output)
21
+ end
22
+ end
23
+
24
+ def cache_key_for(entry)
25
+ if entry.respond_to?(:content_type)
26
+ cache_key_for(
27
+ content_type_id: entry.content_type.id,
28
+ entry_id: entry.id,
29
+ )
30
+ else
31
+ "contentful/#{entry.fetch(:content_type_id)}/#{entry.fetch(:entry_id)}"
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Renderful
4
+ class NoRendererError < StandardError
5
+ attr_reader :entry
6
+
7
+ def initialize(entry, *args)
8
+ @entry = entry
9
+
10
+ super "Cannot find renderer for content type #{entry.content_type.id}", *args
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Renderful
4
+ class Renderer
5
+ class Rails < Renderer
6
+ def render
7
+ renderer.render(partial: view, locals: locals.merge(default_locals))
8
+ end
9
+
10
+ private
11
+
12
+ def renderer
13
+ ActionController::Base.renderer
14
+ end
15
+
16
+ def locals
17
+ {}
18
+ end
19
+
20
+ def view
21
+ "renderful/#{entry.content_type.id.demodulize.underscore}"
22
+ end
23
+
24
+ def default_locals
25
+ { entry: entry, client: client, renderer: self }
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Renderful
4
+ class Renderer
5
+ attr_reader :entry, :client
6
+
7
+ class << self
8
+ def resolve(field)
9
+ define_method(field) do
10
+ resolve(entry.send(field))
11
+ end
12
+ end
13
+ end
14
+
15
+ def initialize(entry, client:)
16
+ @entry = entry
17
+ @client = client
18
+ end
19
+
20
+ def render
21
+ raise NotImplementedError
22
+ end
23
+
24
+ private
25
+
26
+ def resolve(reference)
27
+ if reference.is_a?(Enumerable)
28
+ reference.map(&method(:resolve))
29
+ elsif reference.is_a?(Contentful::Link)
30
+ reference.resolve(client.contentful)
31
+ else
32
+ reference
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Renderful
4
+ VERSION = "0.1.0"
5
+ end
data/lib/renderful.rb ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'contentful'
4
+
5
+ require 'renderful/no_renderer_error'
6
+ require 'renderful/cache'
7
+ require 'renderful/cache/redis'
8
+ require 'renderful/cache_invalidator'
9
+ require 'renderful/client'
10
+ require 'renderful/renderer'
11
+ require 'renderful/renderer/rails'
12
+ require 'renderful/version'
13
+
14
+ module Renderful
15
+ end
data/renderful.gemspec ADDED
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'renderful/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'renderful'
9
+ spec.version = Renderful::VERSION
10
+ spec.authors = ['Alessandro Desantis']
11
+ spec.email = ['desa.alessandro@gmail.com']
12
+
13
+ spec.summary = 'Render your Contentful space!'
14
+ spec.homepage = 'https://github.com/nebulab/renderful'
15
+ spec.license = 'MIT'
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://github.com/nebulab/renderful'
19
+ spec.metadata['changelog_uri'] = 'https://github.com/nebulab/renderful/blob/master/README.md'
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_dependency 'contentful', '~> 2.11'
31
+ spec.add_dependency 'rails', '~> 5.0'
32
+
33
+ spec.add_development_dependency 'bundler', '~> 1.17'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'redis', '~> 4.1'
36
+ spec.add_development_dependency 'rspec', '~> 3.0'
37
+ spec.add_development_dependency 'rspec_junit_formatter', '~> 0.4.1'
38
+ # These are locked because we need to use Hound's versions
39
+ spec.add_development_dependency 'rubocop', '0.64.0'
40
+ spec.add_development_dependency 'rubocop-rspec', '1.32.0'
41
+ end
metadata ADDED
@@ -0,0 +1,195 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: renderful
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alessandro Desantis
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: contentful
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.11'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.17'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.17'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: redis
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec_junit_formatter
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.4.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.4.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '='
116
+ - !ruby/object:Gem::Version
117
+ version: 0.64.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '='
123
+ - !ruby/object:Gem::Version
124
+ version: 0.64.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop-rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '='
130
+ - !ruby/object:Gem::Version
131
+ version: 1.32.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '='
137
+ - !ruby/object:Gem::Version
138
+ version: 1.32.0
139
+ description:
140
+ email:
141
+ - desa.alessandro@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".circleci/config.yml"
147
+ - ".gitignore"
148
+ - ".hound.yml"
149
+ - ".rubocop-https---relaxed-ruby-style-rubocop-yml"
150
+ - ".rubocop.yml"
151
+ - CHANGELOG.md
152
+ - Gemfile
153
+ - Gemfile.lock
154
+ - MIT-LICENSE
155
+ - README.md
156
+ - Rakefile
157
+ - bin/console
158
+ - bin/setup
159
+ - lib/renderful.rb
160
+ - lib/renderful/cache.rb
161
+ - lib/renderful/cache/redis.rb
162
+ - lib/renderful/cache_invalidator.rb
163
+ - lib/renderful/client.rb
164
+ - lib/renderful/no_renderer_error.rb
165
+ - lib/renderful/renderer.rb
166
+ - lib/renderful/renderer/rails.rb
167
+ - lib/renderful/version.rb
168
+ - renderful.gemspec
169
+ homepage: https://github.com/nebulab/renderful
170
+ licenses:
171
+ - MIT
172
+ metadata:
173
+ homepage_uri: https://github.com/nebulab/renderful
174
+ source_code_uri: https://github.com/nebulab/renderful
175
+ changelog_uri: https://github.com/nebulab/renderful/blob/master/README.md
176
+ post_install_message:
177
+ rdoc_options: []
178
+ require_paths:
179
+ - lib
180
+ required_ruby_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ required_rubygems_version: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ requirements: []
191
+ rubygems_version: 3.0.3
192
+ signing_key:
193
+ specification_version: 4
194
+ summary: Render your Contentful space!
195
+ test_files: []