pagelime-rack 0.2.3 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -9
- data/VERSION +1 -1
- data/lib/pagelime-rack.rb +5 -4
- data/lib/pagelime.rb +18 -8
- data/lib/pagelime/cache_engine.rb +68 -0
- data/lib/pagelime/clients/xml_processor.rb +93 -0
- data/lib/pagelime/clients/xml_s3_storage.rb +55 -0
- data/lib/pagelime/configuration.rb +4 -22
- data/lib/pagelime/storage_engine.rb +23 -0
- data/lib/rack/pagelime.rb +1 -1
- data/pagelime-rack.gemspec +6 -4
- metadata +6 -4
- data/lib/pagelime/s3_client.rb +0 -93
- data/lib/pagelime/xml_processor.rb +0 -99
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 962d445dd40119959ee05bf58956c5e1f35fcdf2
|
4
|
+
data.tar.gz: ffacd3eb81878110192113a19baaf5c8c900bce4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1035b00408158783bd980eaf86e8a23842eb3d582ef119f17ae5cb25bc44ddd044c514d6b721a87106a34265830fd817268856c2d346e85dc425797d9f4c5fbe
|
7
|
+
data.tar.gz: 93d89782ea417292705d445866eb11d920a6785c4b892f51cc9c478c6fd8aac4f0226967505ba6f346ca9cc50a5b299f37d34b342f25e333b4d3904c39e43e4c
|
data/README.md
CHANGED
@@ -43,16 +43,17 @@ Make sure that the "Integration Method" for your site on the advanced tab is set
|
|
43
43
|
|
44
44
|
Set up your Environment variables: *(Skip if using Heroku add-on)*
|
45
45
|
|
46
|
-
ENV['PAGELIME_ACCOUNT_KEY']
|
47
|
-
ENV['PAGELIME_ACCOUNT_SECRET']
|
46
|
+
ENV['PAGELIME_ACCOUNT_KEY'] = "..."
|
47
|
+
ENV['PAGELIME_ACCOUNT_SECRET'] = "..."
|
48
48
|
ENV['PAGELIME_RACK_API_VERSION'] = "1.0"
|
49
49
|
|
50
|
-
|
50
|
+
Optionally, enable caching:
|
51
51
|
|
52
52
|
Pagelime.configure do |config|
|
53
|
-
|
54
|
-
config.
|
55
|
-
|
53
|
+
# object that responds to `fetch` and `delete`
|
54
|
+
config.cache = ...
|
55
|
+
# options passed to `fetch(key, options = {}, &block)`
|
56
|
+
config.cache_fetch_options = { ... }
|
56
57
|
end
|
57
58
|
|
58
59
|
### Step 4: Make pages editable
|
@@ -65,6 +66,19 @@ Create some editable regions in your views like so:
|
|
65
66
|
|
66
67
|
*The ID and the class are required for the CMS to work*
|
67
68
|
|
69
|
+
### Step 5: Edit your pages!
|
70
|
+
|
71
|
+
#### For Heroku users
|
72
|
+
|
73
|
+
If you're using the Pagelime Heroku add-on, go to the Heroku admin for your app and under the "Resources" tab you will see the Pagelime add-on listed.
|
74
|
+
Click on the add-on name and you will be redirected to the Pagelime CMS editor.
|
75
|
+
From there you can edit any page in your Rack app!
|
76
|
+
|
77
|
+
#### For Pagelime.com users
|
78
|
+
|
79
|
+
If you have a standalone Pagelime account, simply go to [pagelime.com](http://pagelime.com) and edit your site as usual (see Step 2).
|
80
|
+
|
81
|
+
|
68
82
|
Sinatra Sample
|
69
83
|
--------------
|
70
84
|
|
@@ -72,8 +86,8 @@ Sinatra Sample
|
|
72
86
|
require 'pagelime-rack'
|
73
87
|
|
74
88
|
configure :development do
|
75
|
-
ENV['PAGELIME_ACCOUNT_KEY']
|
76
|
-
ENV['PAGELIME_ACCOUNT_SECRET']
|
89
|
+
ENV['PAGELIME_ACCOUNT_KEY'] = "..."
|
90
|
+
ENV['PAGELIME_ACCOUNT_SECRET'] = "..."
|
77
91
|
ENV['PAGELIME_RACK_API_VERSION'] = "1.0"
|
78
92
|
end
|
79
93
|
|
@@ -81,7 +95,7 @@ Sinatra Sample
|
|
81
95
|
|
82
96
|
get '/page/about' do
|
83
97
|
content_type "text/html"
|
84
|
-
return
|
98
|
+
return '<div id="content" class="cms-editable">Hello World!</div>'
|
85
99
|
end
|
86
100
|
|
87
101
|
Copyright (c) 2013 Pagelime LLC, released under the MIT license
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/lib/pagelime-rack.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
require_relative 'pagelime'
|
2
|
-
require_relative 'pagelime/
|
3
|
-
require_relative 'pagelime/xml_processor'
|
2
|
+
require_relative 'pagelime/clients/xml_s3_storage'
|
3
|
+
require_relative 'pagelime/clients/xml_processor'
|
4
4
|
require_relative 'rack/pagelime'
|
5
5
|
|
6
6
|
Pagelime.configure do |config|
|
7
|
-
config.
|
8
|
-
config.
|
7
|
+
#config.cache = ...
|
8
|
+
config.storage = Pagelime::Clients::XmlS3Storage.new
|
9
|
+
config.processor = Pagelime::Clients::XmlProcessor.new
|
9
10
|
end
|
data/lib/pagelime.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
require_relative 'pagelime/configuration'
|
2
|
+
require_relative 'pagelime/storage_engine'
|
3
|
+
require_relative 'pagelime/cache_engine'
|
2
4
|
|
3
5
|
module Pagelime
|
4
6
|
module ClassMethods
|
@@ -7,20 +9,28 @@ module Pagelime
|
|
7
9
|
config.configure(&block)
|
8
10
|
end
|
9
11
|
|
10
|
-
def
|
11
|
-
|
12
|
+
def process_page(html, page_path)
|
13
|
+
config.processor.process_document(storage, html, page_path)
|
12
14
|
end
|
13
15
|
|
14
|
-
def
|
15
|
-
config.
|
16
|
+
def process_region(html, page_path)
|
17
|
+
config.processor.process_fragment(storage, html, page_path)
|
18
|
+
end
|
19
|
+
|
20
|
+
def storage
|
21
|
+
@storage ||= StorageEngine.new(config, cache)
|
16
22
|
end
|
17
23
|
|
18
|
-
def
|
19
|
-
config
|
24
|
+
def cache
|
25
|
+
@cache ||= CacheEngine.new(config)
|
20
26
|
end
|
21
27
|
|
22
|
-
def
|
23
|
-
config.
|
28
|
+
def config
|
29
|
+
@config ||= Configuration.new
|
30
|
+
end
|
31
|
+
|
32
|
+
def logger
|
33
|
+
config.logger
|
24
34
|
end
|
25
35
|
end
|
26
36
|
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'base64'
|
2
|
+
|
3
|
+
module Pagelime
|
4
|
+
class CacheEngine
|
5
|
+
|
6
|
+
def initialize(config)
|
7
|
+
@config = config
|
8
|
+
end
|
9
|
+
|
10
|
+
### CMS-specific methods
|
11
|
+
|
12
|
+
def fetch_path(page_path, &block)
|
13
|
+
cache_key = generate_region_cache_key(page_path)
|
14
|
+
|
15
|
+
fetch(cache_key, fetch_options, &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def fetch_shared(&block)
|
19
|
+
cache_key = static_shared_cache_key
|
20
|
+
|
21
|
+
fetch(cache_key, fetch_options, &block)
|
22
|
+
end
|
23
|
+
|
24
|
+
def clear_page(page_path)
|
25
|
+
cache_key = generate_region_cache_key(page_path)
|
26
|
+
|
27
|
+
delete cache_key
|
28
|
+
end
|
29
|
+
|
30
|
+
def clear_shared
|
31
|
+
cache_key = static_shared_cache_key
|
32
|
+
|
33
|
+
delete cache_key
|
34
|
+
end
|
35
|
+
|
36
|
+
### Generic cache methods
|
37
|
+
|
38
|
+
def fetch(key, options = {}, &block)
|
39
|
+
if @config.cache
|
40
|
+
@config.cache.fetch(key, fetch_options, &block)
|
41
|
+
else
|
42
|
+
yield
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def delete(key)
|
47
|
+
@config.cache.delete(key) if @config.cache
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def generate_region_cache_key(page_path)
|
53
|
+
if @config.generate_region_cache_key.respond_to? :call
|
54
|
+
@config.generate_region_cache_key.call(page_path)
|
55
|
+
else
|
56
|
+
"pagelime:cms:page:#{Base64.encode64(page_path)}"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def static_shared_cache_key
|
61
|
+
@config.static_shared_cache_key || "pagelime:cms:shared"
|
62
|
+
end
|
63
|
+
|
64
|
+
def fetch_options
|
65
|
+
@config.cache_fetch_options || {}
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require "nokogiri"
|
2
|
+
|
3
|
+
module Pagelime
|
4
|
+
module Clients
|
5
|
+
class XmlProcessor
|
6
|
+
|
7
|
+
def process_document(storage, html, page_path = false)
|
8
|
+
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Document HTML: #{html.inspect}"
|
9
|
+
|
10
|
+
doc = Nokogiri::HTML::Document.parse(html)
|
11
|
+
|
12
|
+
# return original HTML if nil returned
|
13
|
+
output = parse_document(storage, doc, page_path) || html
|
14
|
+
|
15
|
+
if html == output
|
16
|
+
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Document output: UNCHANGED!"
|
17
|
+
else
|
18
|
+
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Document output: #{output.inspect}"
|
19
|
+
end
|
20
|
+
|
21
|
+
output
|
22
|
+
end
|
23
|
+
|
24
|
+
def process_fragment(storage, html, page_path = false)
|
25
|
+
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Fragment HTML: #{html.inspect}"
|
26
|
+
|
27
|
+
doc = Nokogiri::HTML::DocumentFragment.parse(html)
|
28
|
+
|
29
|
+
# return original HTML if nil returned
|
30
|
+
output = parse_document(storage, doc, page_path) || html
|
31
|
+
|
32
|
+
if html == output
|
33
|
+
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Fragment output: UNCHANGED!"
|
34
|
+
else
|
35
|
+
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Fragment output: #{output.inspect}"
|
36
|
+
end
|
37
|
+
|
38
|
+
output
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def parse_document(storage, doc, page_path = false)
|
44
|
+
editable_content = storage.fetch_path(page_path)
|
45
|
+
shared_content = storage.fetch_shared
|
46
|
+
|
47
|
+
unless editable_content || shared_content
|
48
|
+
::Pagelime.logger.warn "PAGELIME CMS RACK PLUGIN: Content not returned from storage"
|
49
|
+
return nil
|
50
|
+
end
|
51
|
+
|
52
|
+
# use nokogiri to replace contents
|
53
|
+
editable_regions = doc.css(".cms-editable")
|
54
|
+
shared_regions = doc.css(".cms-shared")
|
55
|
+
|
56
|
+
patch_regions editable_regions, editable_content
|
57
|
+
patch_regions shared_regions, shared_content
|
58
|
+
|
59
|
+
doc.to_html
|
60
|
+
end
|
61
|
+
|
62
|
+
def patch_regions(editable_regions, xml_content)
|
63
|
+
|
64
|
+
::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: parsing xml"
|
65
|
+
|
66
|
+
editable_regions.each do |div|
|
67
|
+
|
68
|
+
# Grab content ID
|
69
|
+
content_id = div["id"]
|
70
|
+
soap = Nokogiri::XML::Document.parse(xml_content)
|
71
|
+
nodes = soap.css("EditableRegion[@ElementID=\"#{content_id}\"]")
|
72
|
+
|
73
|
+
::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: looking for region: #{content_id}"
|
74
|
+
::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: regions found: #{nodes.count}"
|
75
|
+
|
76
|
+
if nodes.any?
|
77
|
+
new_content = nodes[0].css("Html")[0].content
|
78
|
+
|
79
|
+
::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: NEW CONTENT: #{new_content.inspect}"
|
80
|
+
|
81
|
+
if new_content
|
82
|
+
# div.content = "Replaced content"
|
83
|
+
div.replace new_content
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require "base64"
|
2
|
+
require "net/http"
|
3
|
+
|
4
|
+
module Pagelime
|
5
|
+
module Clients
|
6
|
+
class XmlS3Storage
|
7
|
+
|
8
|
+
def initialize(options = {})
|
9
|
+
@options = {
|
10
|
+
:account_key => ENV['PAGELIME_ACCOUNT_KEY'],
|
11
|
+
:account_secret => ENV['PAGELIME_ACCOUNT_SECRET'],
|
12
|
+
:api_version => ENV['PAGELIME_RACK_API_VERSION']
|
13
|
+
}.merge(options)
|
14
|
+
end
|
15
|
+
|
16
|
+
def fetch_shared
|
17
|
+
|
18
|
+
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: NO SHARED CACHE... loading XML"
|
19
|
+
|
20
|
+
content = request_content("/cms_assets/heroku/#{@options[:account_key]}/shared-regions.xml")
|
21
|
+
|
22
|
+
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Shared content: #{content.inspect}"
|
23
|
+
|
24
|
+
content
|
25
|
+
end
|
26
|
+
|
27
|
+
def fetch_path(page_path)
|
28
|
+
|
29
|
+
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: NO '#{page_path}' CACHE... loading XML"
|
30
|
+
|
31
|
+
content = request_content("/cms_assets/heroku/#{@options[:account_key]}/pages#{page_path}.xml")
|
32
|
+
|
33
|
+
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Content: #{content.inspect}"
|
34
|
+
|
35
|
+
content
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def request_content(url)
|
41
|
+
response = http.get(url)
|
42
|
+
|
43
|
+
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: S3 response code: #{response.code.inspect}"
|
44
|
+
|
45
|
+
# only return the body if response code 200-399
|
46
|
+
response.body if (200...400).include?(response.code.to_f)
|
47
|
+
end
|
48
|
+
|
49
|
+
def http
|
50
|
+
@http ||= Net::HTTP::new('s3.amazonaws.com', 80)
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -4,38 +4,20 @@ module Pagelime
|
|
4
4
|
class Configuration
|
5
5
|
|
6
6
|
# only allow getter access unless using configure block
|
7
|
-
attr_accessor :
|
8
|
-
|
7
|
+
attr_accessor :logger, :storage, :cache, :processor
|
8
|
+
attr_accessor :generate_region_cache_key, :static_shared_cache_key, :cache_fetch_options
|
9
9
|
|
10
10
|
# pass in a configure block to write new values
|
11
11
|
def initialize(defaults = {}, &block)
|
12
|
-
@
|
13
|
-
@account_secret = ENV['PAGELIME_ACCOUNT_SECRET']
|
14
|
-
@api_version = ENV['PAGELIME_RACK_API_VERSION']
|
15
|
-
@logger = Logger.new(STDOUT)
|
12
|
+
@logger = Logger.new(STDOUT)
|
16
13
|
|
17
14
|
configure(&block)
|
18
15
|
end
|
19
16
|
|
20
17
|
def configure(&block)
|
21
|
-
if block_given?
|
22
|
-
# pass self to configuration block
|
23
|
-
yield(self)
|
24
|
-
|
25
|
-
# recreate instances in case classes were updated
|
26
|
-
rebuild!
|
27
|
-
end
|
18
|
+
yield(self) if block_given?
|
28
19
|
|
29
20
|
self
|
30
21
|
end
|
31
|
-
|
32
|
-
def rebuild!
|
33
|
-
if client_class.nil? || processor_class.nil?
|
34
|
-
raise "You must specify client_class and processor_class"
|
35
|
-
end
|
36
|
-
|
37
|
-
@client = client_class.new(account_key, account_secret, api_version)
|
38
|
-
@processor = processor_class.new(@client)
|
39
|
-
end
|
40
22
|
end
|
41
23
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Pagelime
|
2
|
+
class StorageEngine
|
3
|
+
|
4
|
+
attr_reader :cache
|
5
|
+
|
6
|
+
def initialize(config, cache_engine)
|
7
|
+
@config = config
|
8
|
+
@cache = cache_engine# || CacheEngine.new(config)
|
9
|
+
end
|
10
|
+
|
11
|
+
def fetch_path(page_path)
|
12
|
+
@cache.fetch_path page_path do
|
13
|
+
@config.storage.fetch_path(page_path)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def fetch_shared
|
18
|
+
@cache.fetch_shared do
|
19
|
+
@config.storage.fetch_shared
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/rack/pagelime.rb
CHANGED
@@ -32,7 +32,7 @@ module Rack
|
|
32
32
|
::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Processing For Path: #{req.path}"
|
33
33
|
::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Processing Body (size:#{body_content.length})"
|
34
34
|
|
35
|
-
body = ::Pagelime.
|
35
|
+
body = ::Pagelime.process_page(body_content, req.path)
|
36
36
|
|
37
37
|
headers['content-length'] = body.length.to_s
|
38
38
|
|
data/pagelime-rack.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "pagelime-rack"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Emil Anticevic", "Joel Van Horn"]
|
12
|
-
s.date = "2013-09-
|
12
|
+
s.date = "2013-09-11"
|
13
13
|
s.description = "The Pagelime Rack Middleware will process outgoing HTML, look for editable areas, and replace the content with the appropriate HTML from the Pagelime CDN and cache it in memory if possible."
|
14
14
|
s.email = "emil@pagelime.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -26,9 +26,11 @@ Gem::Specification.new do |s|
|
|
26
26
|
"VERSION",
|
27
27
|
"lib/pagelime-rack.rb",
|
28
28
|
"lib/pagelime.rb",
|
29
|
+
"lib/pagelime/cache_engine.rb",
|
30
|
+
"lib/pagelime/clients/xml_processor.rb",
|
31
|
+
"lib/pagelime/clients/xml_s3_storage.rb",
|
29
32
|
"lib/pagelime/configuration.rb",
|
30
|
-
"lib/pagelime/
|
31
|
-
"lib/pagelime/xml_processor.rb",
|
33
|
+
"lib/pagelime/storage_engine.rb",
|
32
34
|
"lib/rack/pagelime.rb",
|
33
35
|
"pagelime-rack.gemspec",
|
34
36
|
"test/helper.rb",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pagelime-rack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Emil Anticevic
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-09-
|
12
|
+
date: 2013-09-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
@@ -114,9 +114,11 @@ files:
|
|
114
114
|
- VERSION
|
115
115
|
- lib/pagelime-rack.rb
|
116
116
|
- lib/pagelime.rb
|
117
|
+
- lib/pagelime/cache_engine.rb
|
118
|
+
- lib/pagelime/clients/xml_processor.rb
|
119
|
+
- lib/pagelime/clients/xml_s3_storage.rb
|
117
120
|
- lib/pagelime/configuration.rb
|
118
|
-
- lib/pagelime/
|
119
|
-
- lib/pagelime/xml_processor.rb
|
121
|
+
- lib/pagelime/storage_engine.rb
|
120
122
|
- lib/rack/pagelime.rb
|
121
123
|
- pagelime-rack.gemspec
|
122
124
|
- test/helper.rb
|
data/lib/pagelime/s3_client.rb
DELETED
@@ -1,93 +0,0 @@
|
|
1
|
-
require "base64"
|
2
|
-
require "net/http"
|
3
|
-
|
4
|
-
module Pagelime
|
5
|
-
class S3Client
|
6
|
-
|
7
|
-
module ClassMethods
|
8
|
-
def default_format
|
9
|
-
:xml
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
extend ClassMethods
|
14
|
-
|
15
|
-
attr_reader :account_key, :account_secret, :api_version
|
16
|
-
|
17
|
-
def initialize(account_key, account_secret, api_version)
|
18
|
-
# reference config object to ensure we have the latest credentials, etc
|
19
|
-
@account_key = account_key
|
20
|
-
@account_secret = account_secret
|
21
|
-
@api_version = api_version
|
22
|
-
|
23
|
-
#raise "WARNING: Account key, secret, and API version were not specified!" unless configured?
|
24
|
-
end
|
25
|
-
|
26
|
-
def configured?
|
27
|
-
!(account_key.nil?)# || account_secret.nil? || api_version.nil?)
|
28
|
-
end
|
29
|
-
|
30
|
-
# def cms_api_signature(req)
|
31
|
-
# secret = account_secret
|
32
|
-
# signature = Base64.encode64(OpenSSL::HMAC.digest('sha1', secret, req).to_s)
|
33
|
-
# return signature
|
34
|
-
# end
|
35
|
-
|
36
|
-
def fetch_shared(format = self.class.default_format)
|
37
|
-
|
38
|
-
# TODO: check cache (see the rails plugin for info)
|
39
|
-
|
40
|
-
::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: NO SHARED CACHE... loading #{format}"
|
41
|
-
|
42
|
-
content = request_content("/cms_assets/heroku/#{account_key}/shared-regions.#{format}")
|
43
|
-
|
44
|
-
# ::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: shared XML: #{content}"
|
45
|
-
|
46
|
-
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Shared content: #{content.inspect}"
|
47
|
-
|
48
|
-
content
|
49
|
-
end
|
50
|
-
|
51
|
-
def fetch(page_path, format = self.class.default_format)
|
52
|
-
|
53
|
-
# TODO: Should element_ids be used anywhere?
|
54
|
-
|
55
|
-
# TODO: Should page_key be used anywhere?
|
56
|
-
page_key = Base64.encode64(page_path)
|
57
|
-
|
58
|
-
# TODO: check cache (see the rails plugin for info)
|
59
|
-
|
60
|
-
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: NO '#{page_path}' CACHE... loading #{format}"
|
61
|
-
|
62
|
-
content = request_content("/cms_assets/heroku/#{account_key}/pages#{page_path}.#{format}")
|
63
|
-
|
64
|
-
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Content: #{content.inspect}"
|
65
|
-
|
66
|
-
content
|
67
|
-
end
|
68
|
-
|
69
|
-
def clear(page_path, format = self.class.default_format)
|
70
|
-
Pagelime.logger.warn "PAGELIME CMS RACK PLUGIN: #{self.class.name}##{__method__} is not implemented!"
|
71
|
-
end
|
72
|
-
|
73
|
-
def clear_shared(format = self.class.default_format)
|
74
|
-
Pagelime.logger.warn "PAGELIME CMS RACK PLUGIN: #{self.class.name}##{__method__} is not implemented!"
|
75
|
-
end
|
76
|
-
|
77
|
-
private
|
78
|
-
|
79
|
-
def request_content(url)
|
80
|
-
response = http.get(url)
|
81
|
-
|
82
|
-
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: S3 response code: #{response.code.inspect}"
|
83
|
-
|
84
|
-
# only return the body if response code 200-399
|
85
|
-
response.body if (200...400).include?(response.code.to_f)
|
86
|
-
end
|
87
|
-
|
88
|
-
def http
|
89
|
-
@http ||= Net::HTTP::new('s3.amazonaws.com', 80)
|
90
|
-
end
|
91
|
-
|
92
|
-
end
|
93
|
-
end
|
@@ -1,99 +0,0 @@
|
|
1
|
-
require "nokogiri"
|
2
|
-
|
3
|
-
module Pagelime
|
4
|
-
class XmlProcessor
|
5
|
-
|
6
|
-
attr_reader :client
|
7
|
-
attr_reader :format
|
8
|
-
|
9
|
-
def initialize(client)
|
10
|
-
@client = client
|
11
|
-
@format = :xml
|
12
|
-
end
|
13
|
-
|
14
|
-
def process_document(html, page_path = false)
|
15
|
-
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Document HTML: #{html.inspect}"
|
16
|
-
|
17
|
-
doc = Nokogiri::HTML::Document.parse(html)
|
18
|
-
|
19
|
-
# return original HTML if nil returned
|
20
|
-
output = parse_document(doc, page_path) || html
|
21
|
-
|
22
|
-
if html == output
|
23
|
-
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Document output: UNCHANGED!"
|
24
|
-
else
|
25
|
-
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Document output: #{output.inspect}"
|
26
|
-
end
|
27
|
-
|
28
|
-
output
|
29
|
-
end
|
30
|
-
|
31
|
-
def process_fragment(html, page_path = false)
|
32
|
-
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Fragment HTML: #{html.inspect}"
|
33
|
-
|
34
|
-
doc = Nokogiri::HTML::DocumentFragment.parse(html)
|
35
|
-
|
36
|
-
# return original HTML if nil returned
|
37
|
-
output = parse_document(doc, page_path) || html
|
38
|
-
|
39
|
-
if html == output
|
40
|
-
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Fragment output: UNCHANGED!"
|
41
|
-
else
|
42
|
-
Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Fragment output: #{output.inspect}"
|
43
|
-
end
|
44
|
-
|
45
|
-
output
|
46
|
-
end
|
47
|
-
|
48
|
-
private
|
49
|
-
|
50
|
-
# options = { :page_path => nil, :html => "", :fragment => true }
|
51
|
-
def parse_document(doc, page_path = false)
|
52
|
-
|
53
|
-
unless client.configured?
|
54
|
-
::Pagelime.logger.warn "PAGELIME CMS RACK PLUGIN: Environment variables not configured"
|
55
|
-
return nil
|
56
|
-
end
|
57
|
-
|
58
|
-
# use nokogiri to replace contents
|
59
|
-
editable_regions = doc.css(".cms-editable")
|
60
|
-
shared_regions = doc.css(".cms-shared")
|
61
|
-
|
62
|
-
patch_regions editable_regions, client.fetch(page_path, format)
|
63
|
-
patch_regions shared_regions, client.fetch_shared(format)
|
64
|
-
|
65
|
-
return doc.to_html
|
66
|
-
|
67
|
-
end
|
68
|
-
|
69
|
-
def patch_regions(editable_regions, xml_content)
|
70
|
-
|
71
|
-
::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: parsing xml"
|
72
|
-
|
73
|
-
editable_regions.each do |div|
|
74
|
-
|
75
|
-
# Grab client ID
|
76
|
-
client_id = div["id"]
|
77
|
-
soap = Nokogiri::XML::Document.parse(xml_content)
|
78
|
-
nodes = soap.css("EditableRegion[@ElementID=\"#{client_id}\"]")
|
79
|
-
|
80
|
-
::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: looking for region: #{client_id}"
|
81
|
-
::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: regions found: #{nodes.count}"
|
82
|
-
|
83
|
-
if nodes.any?
|
84
|
-
new_content = nodes[0].css("Html")[0].content
|
85
|
-
|
86
|
-
::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: NEW CONTENT: #{new_content.inspect}"
|
87
|
-
|
88
|
-
if new_content
|
89
|
-
# div.content = "Replaced content"
|
90
|
-
div.replace new_content
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
end
|
95
|
-
|
96
|
-
end
|
97
|
-
|
98
|
-
end
|
99
|
-
end
|