contentful_rails 0.0.4
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 +7 -0
- data/MIT-LICENSE +20 -0
- data/Rakefile +34 -0
- data/app/controllers/contentful_rails/webhooks_controller.rb +45 -0
- data/app/helpers/contentful_rails/markdown_helper.rb +10 -0
- data/config/routes.rb +11 -0
- data/lib/contentful_rails/cached_timestamps.rb +24 -0
- data/lib/contentful_rails/development_constraint.rb +5 -0
- data/lib/contentful_rails/engine.rb +36 -0
- data/lib/contentful_rails/version.rb +3 -0
- data/lib/contentful_rails.rb +23 -0
- data/lib/tasks/contentful_rails_tasks.rake +4 -0
- data/test/contentful_rails_test.rb +7 -0
- data/test/integration/navigation_test.rb +10 -0
- data/test/test_helper.rb +16 -0
- metadata +89 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: acff54ce8df7277286e663152c3456dd299d9f6e
|
|
4
|
+
data.tar.gz: fa9169f868f67296d300a09a7a0951f33b9c5c11
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a99d3187b7e5f7160230c9b1b21f21cfba259a53d4d8b98e1b109a4916da637d46e3d6682ace25ef2c6882d4d3ce9ef280e6d003ba65668644a3269d3d743ed9
|
|
7
|
+
data.tar.gz: ac698630aae0f9b76f5d6c4c5042a8888dfbde5bd7be2876f2a1ced92299bf4760dcdae98f078a6c4163122794cb30a6f7735698b82cd2f7b679d30d94bed6e7
|
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright 2014 Error Creative Studio
|
|
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/Rakefile
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
begin
|
|
2
|
+
require 'bundler/setup'
|
|
3
|
+
rescue LoadError
|
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
require 'rdoc/task'
|
|
8
|
+
|
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
|
11
|
+
rdoc.title = 'ContentfulRails'
|
|
12
|
+
rdoc.options << '--line-numbers'
|
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
|
|
18
|
+
load 'rails/tasks/engine.rake'
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
Bundler::GemHelper.install_tasks
|
|
23
|
+
|
|
24
|
+
require 'rake/testtask'
|
|
25
|
+
|
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
|
27
|
+
t.libs << 'lib'
|
|
28
|
+
t.libs << 'test'
|
|
29
|
+
t.pattern = 'test/**/*_test.rb'
|
|
30
|
+
t.verbose = false
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
task default: :test
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
class ContentfulRails::WebhooksController < ActionController::Base
|
|
2
|
+
if ContentfulRails.configuration.authenticate_webhooks
|
|
3
|
+
http_basic_authenticate_with name: ContentfulRails.configuration.webhooks_username,
|
|
4
|
+
password: ContentfulRails.configuration.webhooks_password
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
skip_before_filter :verify_authenticity_token, :only => [:create]
|
|
8
|
+
|
|
9
|
+
#this is where we receive a webhook, via a POST
|
|
10
|
+
def create
|
|
11
|
+
# The only things we need to handle in here (for now at least) are entries.
|
|
12
|
+
# If there's been an update or a deletion, we just remove the cached timestamp.
|
|
13
|
+
# The updated_at method which is included in ContentfulModel::Base in this gem
|
|
14
|
+
# will check the cache first before making the call to the API.
|
|
15
|
+
|
|
16
|
+
# We can then just use normal Rails russian doll caching without expensive API calls.
|
|
17
|
+
request.format = :json
|
|
18
|
+
update_type = request.headers['HTTP_X_CONTENTFUL_TOPIC']
|
|
19
|
+
content_type_id = params[:sys][:contentType][:sys][:id]
|
|
20
|
+
item_id = params[:sys][:id]
|
|
21
|
+
cache_key = "contentful_timestamp/#{content_type_id}/#{item_id}"
|
|
22
|
+
|
|
23
|
+
#delete the cache entry
|
|
24
|
+
if update_type =~ %r(Entry)
|
|
25
|
+
Rails.cache.delete(cache_key)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
#must return an ok
|
|
29
|
+
render nothing: true
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def debug
|
|
33
|
+
render text: "Debug method works ok"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def webhook_params
|
|
39
|
+
params.permit!
|
|
40
|
+
params.require(:sys).permit(:id, contentType: [sys: [:id]])
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
module ContentfulRails
|
|
2
|
+
module MarkdownHelper
|
|
3
|
+
def parse_markdown(content)
|
|
4
|
+
@markdown ||= Redcarpet::Markdown.new(
|
|
5
|
+
Redcarpet::Render::HTML, autolink: true, space_after_headers: true, fenced_code_blocks: true
|
|
6
|
+
)
|
|
7
|
+
@markdown.render(content).html_safe
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
end
|
data/config/routes.rb
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module ContentfulRails
|
|
2
|
+
#A module to prepend into ContentfulModel::Base which will allow the model instance
|
|
3
|
+
#to check the cache for its timestamp before making an expensive API call
|
|
4
|
+
module CachedTimestamps
|
|
5
|
+
def self.included(base)
|
|
6
|
+
base.class_eval do
|
|
7
|
+
alias_method_chain :updated_at, :caching
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def updated_at_with_caching
|
|
12
|
+
Rails.cache.fetch(self.timestamp_cache_key) do
|
|
13
|
+
updated_at_without_caching
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def timestamp_cache_key
|
|
18
|
+
"contentful_timestamp/#{self.class.content_type_id}/#{self.id}"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
end
|
|
24
|
+
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
module ContentfulRails
|
|
2
|
+
class Engine < ::Rails::Engine
|
|
3
|
+
|
|
4
|
+
isolate_namespace ContentfulRails
|
|
5
|
+
|
|
6
|
+
#Iterate through all models which inherit from ContentfulModel::Base
|
|
7
|
+
#and add an entry mapping for them, so calls to the Contentful API return
|
|
8
|
+
#the appropriate classes.
|
|
9
|
+
initializer "add_entry_mappings_for_contentful_models" do
|
|
10
|
+
if defined?(ContentfulModel)
|
|
11
|
+
Rails.application.eager_load!
|
|
12
|
+
ContentfulModel::Base.descendents.each do |klass|
|
|
13
|
+
klass.send(:add_entry_mapping)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
initializer "add_contentful_mime_type" do
|
|
19
|
+
Mime::Type.register "application/json", :json, ["application/vnd.contentful.management.v1+json"]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
config.to_prepare do
|
|
23
|
+
if defined?(::ContentfulModel)
|
|
24
|
+
ContentfulModel::Base.send(:include, ContentfulRails::CachedTimestamps)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
#if we're at the end of initialization and there's no config object,
|
|
29
|
+
#set one up with the default options (i.e. an empty proc)
|
|
30
|
+
config.after_initialize do
|
|
31
|
+
if ContentfulRails.configuration.nil?
|
|
32
|
+
ContentfulRails.configure {}
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
require "contentful_rails/engine"
|
|
2
|
+
require "contentful_rails/development_constraint"
|
|
3
|
+
require "contentful_rails/cached_timestamps"
|
|
4
|
+
require 'redcarpet'
|
|
5
|
+
|
|
6
|
+
module ContentfulRails
|
|
7
|
+
class << self
|
|
8
|
+
attr_accessor :configuration
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.configure
|
|
12
|
+
self.configuration ||= Configuration.new
|
|
13
|
+
yield(configuration)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class Configuration
|
|
17
|
+
attr_accessor :authenticate_webhooks, :webhooks_username, :webhooks_password
|
|
18
|
+
|
|
19
|
+
def initialize
|
|
20
|
+
@authenticate = true
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
data/test/test_helper.rb
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Configure Rails Environment
|
|
2
|
+
ENV["RAILS_ENV"] = "test"
|
|
3
|
+
|
|
4
|
+
require File.expand_path("../../test/dummy/config/environment.rb", __FILE__)
|
|
5
|
+
ActiveRecord::Migrator.migrations_paths = [File.expand_path("../../test/dummy/db/migrate", __FILE__)]
|
|
6
|
+
require "rails/test_help"
|
|
7
|
+
|
|
8
|
+
Rails.backtrace_cleaner.remove_silencers!
|
|
9
|
+
|
|
10
|
+
# Load support files
|
|
11
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
|
12
|
+
|
|
13
|
+
# Load fixtures from the engine
|
|
14
|
+
if ActiveSupport::TestCase.method_defined?(:fixture_path=)
|
|
15
|
+
ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
|
|
16
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: contentful_rails
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.4
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Error Creative Studio
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2014-12-31 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rails
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - '>='
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 4.1.8
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - '>='
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 4.1.8
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: redcarpet
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ~>
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '3.2'
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ~>
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '3.2'
|
|
41
|
+
description: A gem to help with hooking Contentful into your Rails application
|
|
42
|
+
email:
|
|
43
|
+
- hosting@errorstudio.co.uk
|
|
44
|
+
executables: []
|
|
45
|
+
extensions: []
|
|
46
|
+
extra_rdoc_files: []
|
|
47
|
+
files:
|
|
48
|
+
- MIT-LICENSE
|
|
49
|
+
- Rakefile
|
|
50
|
+
- app/controllers/contentful_rails/webhooks_controller.rb
|
|
51
|
+
- app/helpers/contentful_rails/markdown_helper.rb
|
|
52
|
+
- config/routes.rb
|
|
53
|
+
- lib/contentful_rails.rb
|
|
54
|
+
- lib/contentful_rails/cached_timestamps.rb
|
|
55
|
+
- lib/contentful_rails/development_constraint.rb
|
|
56
|
+
- lib/contentful_rails/engine.rb
|
|
57
|
+
- lib/contentful_rails/version.rb
|
|
58
|
+
- lib/tasks/contentful_rails_tasks.rake
|
|
59
|
+
- test/contentful_rails_test.rb
|
|
60
|
+
- test/integration/navigation_test.rb
|
|
61
|
+
- test/test_helper.rb
|
|
62
|
+
homepage: https://github.com/errorstudio/contentful_rails
|
|
63
|
+
licenses:
|
|
64
|
+
- MIT
|
|
65
|
+
metadata: {}
|
|
66
|
+
post_install_message:
|
|
67
|
+
rdoc_options: []
|
|
68
|
+
require_paths:
|
|
69
|
+
- lib
|
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - '>='
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
|
+
requirements:
|
|
77
|
+
- - '>='
|
|
78
|
+
- !ruby/object:Gem::Version
|
|
79
|
+
version: '0'
|
|
80
|
+
requirements: []
|
|
81
|
+
rubyforge_project:
|
|
82
|
+
rubygems_version: 2.2.2
|
|
83
|
+
signing_key:
|
|
84
|
+
specification_version: 4
|
|
85
|
+
summary: A gem to help with hooking Contentful into your Rails application
|
|
86
|
+
test_files:
|
|
87
|
+
- test/contentful_rails_test.rb
|
|
88
|
+
- test/integration/navigation_test.rb
|
|
89
|
+
- test/test_helper.rb
|