markbates-content_o_matic 0.0.1.20090720170922
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +21 -0
- data/README +3 -0
- data/lib/content_o_matic/content_cache.rb +5 -0
- data/lib/content_o_matic/content_o_matic.rb +62 -0
- data/lib/content_o_matic/errors.rb +6 -0
- data/lib/content_o_matic/response.rb +49 -0
- data/lib/content_o_matic/text_helper.rb +36 -0
- data/lib/content_o_matic.rb +20 -0
- metadata +90 -0
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2009 markbates
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
module ContentOMatic
|
2
|
+
|
3
|
+
class << self
|
4
|
+
|
5
|
+
def logger
|
6
|
+
configatron.content_o_matic.logger
|
7
|
+
end
|
8
|
+
|
9
|
+
def get(slug_url, options = {})
|
10
|
+
if configatron.content_o_matic.retrieve(:cache_results, false)
|
11
|
+
url = url_from_slug(slug_url, options)
|
12
|
+
ContentCache.get(url) do |url|
|
13
|
+
content = get_without_cache(slug_url, options)
|
14
|
+
if content.success?
|
15
|
+
ContentCache.set(url, content)
|
16
|
+
end
|
17
|
+
return content
|
18
|
+
end
|
19
|
+
else
|
20
|
+
return get_without_cache(slug_url, options)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_without_cache(slug_url, options = {})
|
25
|
+
url = url_from_slug(slug_url, options)
|
26
|
+
begin
|
27
|
+
Timeout::timeout(configatron.content_o_matic.response.time_out) do
|
28
|
+
response = Net::HTTP.get_response(URI.parse(url))
|
29
|
+
return ContentOMatic::Response.new(url, response.code, response.body)
|
30
|
+
end
|
31
|
+
rescue Exception => e
|
32
|
+
ContentOMatic.logger.error(e)
|
33
|
+
return ContentOMatic::Response.new(url, '500', e.message)
|
34
|
+
end
|
35
|
+
end # get_without_cache
|
36
|
+
|
37
|
+
def url_from_slug(slug_url, options = {})
|
38
|
+
return slug_url if slug_url.nil?
|
39
|
+
url = slug_url
|
40
|
+
if !slug_url.match(/^([a-zA-Z]+):\/\//)
|
41
|
+
# it's not a 'remote' url - it has no protocol
|
42
|
+
url = File.join(configatron.content_o_matic.url, slug_url)
|
43
|
+
end
|
44
|
+
if options.is_a? Hash
|
45
|
+
opts = options.dup
|
46
|
+
unless opts.nil? || opts.empty?
|
47
|
+
opts.delete(:controller)
|
48
|
+
opts.delete(:action)
|
49
|
+
if url.match(/\?/)
|
50
|
+
url << '&'
|
51
|
+
else
|
52
|
+
url << '?'
|
53
|
+
end
|
54
|
+
url << opts.collect {|k,v| "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}"}.join("&")
|
55
|
+
end
|
56
|
+
end
|
57
|
+
return url
|
58
|
+
end
|
59
|
+
|
60
|
+
end # class << self
|
61
|
+
|
62
|
+
end # ContentOMatic
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module ContentOMatic
|
2
|
+
|
3
|
+
class Response
|
4
|
+
|
5
|
+
attr_accessor :status
|
6
|
+
attr_accessor :body
|
7
|
+
attr_accessor :url
|
8
|
+
attr_accessor :hpricot_doc
|
9
|
+
|
10
|
+
def initialize(url, status, body = '')
|
11
|
+
self.url = url
|
12
|
+
self.status = status.to_i
|
13
|
+
self.body = body
|
14
|
+
if success?
|
15
|
+
self.hpricot_doc = Hpricot(self.body)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def success?
|
20
|
+
self.status == 200
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_s
|
24
|
+
self.success? ? self.body : ''
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_str
|
28
|
+
self.to_s
|
29
|
+
end
|
30
|
+
|
31
|
+
def has_layout?
|
32
|
+
return self.body != self.html_body
|
33
|
+
end
|
34
|
+
|
35
|
+
def html_body
|
36
|
+
unless @__doc_body
|
37
|
+
@__doc_body = self.hpricot_doc.at('body')
|
38
|
+
@__doc_body = @__doc_body.nil? ? self.body : @__doc_body.inner_html
|
39
|
+
end
|
40
|
+
return @__doc_body
|
41
|
+
end
|
42
|
+
|
43
|
+
def exception
|
44
|
+
ContentOMatic::InvalidResponseError.new("URL: '#{self.url}' did not return a valid response! Status: '#{self.status}' Body: '#{self.body}'") unless self.success?
|
45
|
+
end
|
46
|
+
|
47
|
+
end # Response
|
48
|
+
|
49
|
+
end # ContentOMatic
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module ActionView
|
2
|
+
module Helpers
|
3
|
+
module TextHelper
|
4
|
+
|
5
|
+
def content_o_matic(slug, options = {})
|
6
|
+
options = {} if options.nil?
|
7
|
+
comments = true
|
8
|
+
if options.has_key?(:print_comments)
|
9
|
+
comments = options.delete(:print_comments)
|
10
|
+
end
|
11
|
+
text = "<!-- Loading Content: '#{slug}' -->\n" if comments
|
12
|
+
begin
|
13
|
+
html_body = true
|
14
|
+
if options.has_key?(:html_body)
|
15
|
+
html_body = options.delete(:html_body)
|
16
|
+
end
|
17
|
+
res = ContentOMatic.get(slug, options)
|
18
|
+
if res.success?
|
19
|
+
if html_body
|
20
|
+
text << res.html_body
|
21
|
+
else
|
22
|
+
text << res.body
|
23
|
+
end
|
24
|
+
else
|
25
|
+
raise res.exception
|
26
|
+
end
|
27
|
+
rescue Exception => e
|
28
|
+
text << "<!-- Error: #{e.message} -->" if comments
|
29
|
+
end
|
30
|
+
text << "\n<!-- Loaded Content: '#{slug}' -->" if comments
|
31
|
+
text
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'hpricot'
|
2
|
+
require 'configatron'
|
3
|
+
require 'cachetastic'
|
4
|
+
require 'logger'
|
5
|
+
|
6
|
+
configatron.content_o_matic.set_default(:url, 'http://www.example.com')
|
7
|
+
configatron.content_o_matic.response.set_default(:time_out, 30)
|
8
|
+
configatron.content_o_matic.set_default(:cache_results, false)
|
9
|
+
|
10
|
+
if defined?(RAILS_DEFAULT_LOGGER)
|
11
|
+
configatron.content_o_matic.set_default(:logger, RAILS_DEFAULT_LOGGER)
|
12
|
+
else
|
13
|
+
path = File.join(FileUtils.pwd, 'log')
|
14
|
+
FileUtils.mkdir_p(path)
|
15
|
+
configatron.content_o_matic.set_default(:logger, ::Logger.new(File.join(path, 'content_o_matic.log')))
|
16
|
+
end
|
17
|
+
|
18
|
+
Dir.glob(File.join(File.dirname(__FILE__), 'content_o_matic', '**/*.rb')).each do |f|
|
19
|
+
require File.expand_path(f)
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: markbates-content_o_matic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.20090720170922
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- markbates
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-20 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hpricot
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: configatron
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: cachetastic
|
37
|
+
type: :runtime
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
description: "content_o_matic was developed by: markbates"
|
46
|
+
email: ""
|
47
|
+
executables: []
|
48
|
+
|
49
|
+
extensions: []
|
50
|
+
|
51
|
+
extra_rdoc_files:
|
52
|
+
- README
|
53
|
+
- LICENSE
|
54
|
+
files:
|
55
|
+
- lib/content_o_matic/content_cache.rb
|
56
|
+
- lib/content_o_matic/content_o_matic.rb
|
57
|
+
- lib/content_o_matic/errors.rb
|
58
|
+
- lib/content_o_matic/response.rb
|
59
|
+
- lib/content_o_matic/text_helper.rb
|
60
|
+
- lib/content_o_matic.rb
|
61
|
+
- README
|
62
|
+
- LICENSE
|
63
|
+
has_rdoc: false
|
64
|
+
homepage: ""
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
|
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
|
+
version:
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: "0"
|
81
|
+
version:
|
82
|
+
requirements: []
|
83
|
+
|
84
|
+
rubyforge_project: magrathea
|
85
|
+
rubygems_version: 1.2.0
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: content_o_matic
|
89
|
+
test_files: []
|
90
|
+
|