Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prismic integration #17

Merged
merged 3 commits into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ jobs:
- renderful-v1-{{ .Branch }}
- renderful-v1-
- run:
name: Bundle Install
name: Bundle install
command: bundle check || bundle install
- run:
name: Appraisal install
command: bundle exec appraisal install
- save_cache:
key: renderful-v1-{{ .Branch }}-{{ .Revision }}
Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,40 @@ You can now render your Contentful entries via Renderful:
RenderfulClient.render('your_entry_id')
```

### Prismic

In order to integrate with Prismic, you will first need to add the `prismic.io` gem to your Gemfile:

```ruby
gem 'prismic.io'
```

Now make sure to install it:

```console
$ bundle install
```

Finally, initialize Renderful with the Prismic provider:

```ruby
RenderfulClient = Renderful::Client.new(
provider: Renderful::Provider::Prismic.new(
prismic: Prismic.api('https://yourendpoint.prismic.io/api', 'your_access_token')
)
)
```

You can now render your Prismic documents via Renderful:

```ruby
RenderfulClient.render('your_entry_id')
```

NOTE: Due to limitations in Prismic's API, cache invalidation for Prismic will invalidate all your
components. Depending on how often you update your content, you may want to disable caching entirely
if you are using Prismic.

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run
Expand Down
1 change: 1 addition & 0 deletions lib/renderful.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'renderful/content_entry'
require 'renderful/provider/base'
require 'renderful/provider/contentful'
require 'renderful/provider/prismic'
require 'renderful/client'
require 'renderful/component/base'
require 'renderful/component/rails'
Expand Down
6 changes: 5 additions & 1 deletion lib/renderful/cache/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ def write(_key, _value)
raise NotImplementedError
end

def delete(_key)
def delete(*_keys)
raise NotImplementedError
end

def delete_matched(_pattern)
raise NotImplementedError
end

Expand Down
6 changes: 5 additions & 1 deletion lib/renderful/cache/null.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ def write(key, value)
# noop
end

def delete(key)
def delete(*keys)
# noop
end

def delete_matched(pattern)
# noop
end

Expand Down
9 changes: 7 additions & 2 deletions lib/renderful/cache/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ def write(key, value)
redis.set(key, value)
end

def delete(key)
redis.del(key)
def delete(*keys)
redis.del(*keys)
end

def delete_matched(pattern)
keys = redis.scan_each(match: pattern).to_a
delete(*keys)
end

def fetch(key)
Expand Down
14 changes: 8 additions & 6 deletions lib/renderful/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ def initialize(provider:, components:, cache: Cache::Null)
end

def render(entry_id)
content_entry = ContentEntry.new(provider: provider, id: entry_id)

cache.fetch(content_entry.cache_key) do
content_entry.hydrate
cache.fetch(ContentEntry.build_cache_key(provider, id: entry_id)) do
content_entry = provider.find_entry(entry_id)
component_for_entry(content_entry).render
end
end

def invalidate_cache_from_webhook(body)
provider.cache_keys_to_invalidate(body).each do |cache_key|
cache.delete(cache_key)
result = provider.cache_keys_to_invalidate(body)

cache.delete(*result[:keys])

result[:patterns].each do |pattern|
cache.delete_matched(pattern)
end
end

Expand Down
17 changes: 7 additions & 10 deletions lib/renderful/content_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ module Renderful
class ContentEntry
attr_reader :provider, :id, :content_type, :fields

class << self
def build_cache_key(provider, id: nil)
['renderful', provider.cache_prefix, id || '*'].join('/')
end
end

def initialize(provider:, id:, content_type: nil, fields: {})
@provider = provider
@id = id
Expand All @@ -12,16 +18,7 @@ def initialize(provider:, id:, content_type: nil, fields: {})
end

def cache_key
"renderful/#{provider.cache_prefix}/#{id}"
end

def hydrate
other_entry = provider.find_entry(id)

@content_type = other_entry.content_type
@fields = other_entry.fields

self
self.class.build_cache_key(provider, id: id)
end
end
end
22 changes: 10 additions & 12 deletions lib/renderful/provider/contentful.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ def find_entry(entry_id)
def cache_keys_to_invalidate(webhook_body)
params = webhook_body.is_a?(String) ? JSON.parse(webhook_body) : webhook_body

modified_entry = ContentEntry.new(
provider: self,
content_type: params['sys']['contentType']['sys']['id'],
id: params['sys']['id'],
)

entries_to_invalidate = [modified_entry] + entries_linking_to(modified_entry)

entries_to_invalidate.map(&:cache_key)
keys_to_invalidate = [ContentEntry.build_cache_key(self, id: params['sys']['id'])]
keys_to_invalidate += contentful.entries(links_to_entry: params['sys']['id']).map do |entry|
ContentEntry.build_cache_key(self, id: entry.id)
end

{
keys: keys_to_invalidate,
patterns: [],
}
end

private
Expand All @@ -42,9 +42,7 @@ def wrap_entry(entry)
)
end

def entries_linking_to(entry)
contentful.entries(links_to_entry: entry.id).map(&method(:wrap_entry))
end
def entries_linking_to(entry_id); end

def contentful
options[:contentful]
Expand Down
43 changes: 43 additions & 0 deletions lib/renderful/provider/prismic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

module Renderful
module Provider
class Prismic < Base
def initialize(options)
super

fail ArgumentError, 'prismic option is required!' unless prismic
end

def cache_prefix
:prismic
end

def find_entry(entry_id)
wrap_entry(prismic.getByID(entry_id))
end

def cache_keys_to_invalidate(_webhook_body)
{
keys: [],
patterns: ['renderful/prismic/*'],
}
end

private

def wrap_entry(entry)
ContentEntry.new(
provider: self,
id: entry.id,
content_type: entry.type,
fields: entry.fragments,
)
end

def prismic
options[:prismic]
end
end
end
end
1 change: 1 addition & 0 deletions renderful.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'combustion', '~> 1.1'
spec.add_development_dependency 'contentful', '~> 2.11'
spec.add_development_dependency 'gem-release', '~> 2.1'
spec.add_development_dependency 'prismic.io', '~> 1.7'
spec.add_development_dependency 'rails', ['>= 5.0.0', '< 7']
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'redis', '~> 4.1'
Expand Down
6 changes: 6 additions & 0 deletions spec/renderful/cache/null_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
end
end

describe '#delete_matched' do
it 'is a no-op' do
expect { cache.delete_matched('key*') }.not_to raise_error
end
end

describe '#fetch' do
it 'always yields' do
expect(cache.fetch('key') { 'value' }).to eq('value')
Expand Down
20 changes: 17 additions & 3 deletions spec/renderful/cache/redis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,24 @@
end

describe '#delete' do
it 'deletes the key from Redis' do
cache.delete('key')
it 'deletes the keys from Redis' do
cache.delete('key1', 'key2')

expect(redis).to have_received(:del).with('key')
expect(redis).to have_received(:del).with('key1', 'key2')
end
end

describe '#delete_matched' do
before do
allow(redis).to receive(:scan_each)
.with(match: 'key*')
.and_return(%w[key1 key2])
end

it 'deletes all keys from Redis that match the given pattern' do
cache.delete_matched('key*')

expect(redis).to have_received(:del).with('key1', 'key2')
end
end

Expand Down
14 changes: 10 additions & 4 deletions spec/renderful/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
end

context 'when the output has not been cached' do
let(:entry) { instance_double('ContentEntry', content_type: 'testContentType', fields: {}) }
let(:entry) { instance_double('Renderful::ContentEntry', content_type: 'testContentType', fields: {}) }

before do
allow(cache).to receive(:fetch).with(an_instance_of(String)) { |_, &block| block.call }
Expand All @@ -60,7 +60,7 @@
.and_return(entry)

allow(component_klass).to receive(:new)
.with(an_instance_of(Renderful::ContentEntry), client: client)
.with(entry, client: client)
.and_return(instance_double('Renderful::Component::Base', render: 'render_output'))
end

Expand Down Expand Up @@ -92,13 +92,19 @@
before do
allow(provider).to receive(:cache_keys_to_invalidate)
.with(payload)
.and_return(%w[key1])
.and_return(keys: %w[provider:key1 provider:key2], patterns: %w[provider:*])
end

it 'invalidates the cache keys returned by the provider' do
client.invalidate_cache_from_webhook(payload)

expect(cache).to have_received(:delete).with('key1')
expect(cache).to have_received(:delete).with('provider:key1', 'provider:key2')
end

it 'invalidates the pattern returned by the provider' do
client.invalidate_cache_from_webhook(payload)

expect(cache).to have_received(:delete_matched).with('provider:*')
end
end
end
27 changes: 0 additions & 27 deletions spec/renderful/content_entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,4 @@
expect(subject.cache_key).to eq('renderful/dummy_provider/content_entry_id')
end
end

describe '#hydrate' do
before do
allow(provider).to receive(:find_entry).with(id).and_return(described_class.new(
provider: provider,
id: id,
content_type: 'new_content_type',
fields: { 'new' => 'fields' },
))
end

it 'hydrates the content entry with the content type from the CMS' do
subject.hydrate

expect(subject.content_type).to eq('new_content_type')
end

it 'hydrates the content entry with the fields from the CMS' do
subject.hydrate

expect(subject.fields).to eq('new' => 'fields')
end

it 'returns self' do
expect(subject.hydrate).to eq(subject)
end
end
end
24 changes: 18 additions & 6 deletions spec/renderful/provider/contentful_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,31 @@
id: 'linking_entry_id',
)])

allow(Renderful::ContentEntry).to receive(:new).with(a_hash_including(id: '6hxhiF6EcKklWANW9BBlVY'))
.and_return(instance_double('ContentEntry', id: '6hxhiF6EcKklWANW9BBlVY', cache_key: 'cache/6hxhiF6EcKklWANW9BBlVY'))
allow(Renderful::ContentEntry).to receive(:build_cache_key)
.with(subject, id: '6hxhiF6EcKklWANW9BBlVY')
.and_return('cache/6hxhiF6EcKklWANW9BBlVY')

allow(Renderful::ContentEntry).to receive(:new).with(a_hash_including(id: 'linking_entry_id'))
.and_return(instance_double('ContentEntry', id: 'linking_entry_id', cache_key: 'cache/linking_entry_id'))
allow(Renderful::ContentEntry).to receive(:build_cache_key)
.with(subject, id: 'linking_entry_id')
.and_return('cache/linking_entry_id')
end

it 'returns the cache key for the invalidated entry' do
expect(subject.cache_keys_to_invalidate(payload)).to include('cache/6hxhiF6EcKklWANW9BBlVY')
result = subject.cache_keys_to_invalidate(payload)

expect(result[:keys]).to include('cache/6hxhiF6EcKklWANW9BBlVY')
end

it 'returns the cache keys for any entries linking to the invalidated one' do
expect(subject.cache_keys_to_invalidate(payload)).to include('cache/linking_entry_id')
result = subject.cache_keys_to_invalidate(payload)

expect(result[:keys]).to include('cache/linking_entry_id')
end

it 'does not invalidate any patterns' do
result = subject.cache_keys_to_invalidate(payload)

expect(result[:patterns]).to eq([])
end
end
end
Loading