radiant_helper 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,93 @@
1
+ puts ""
2
+ puts %{
3
+
4
+ *** View Level Usage ***
5
+
6
+ So you've got a Radiant CMS and you want to include it as part of your app, eh?
7
+
8
+ Well, just follow along and I'll help you intergrate CMS backed sections to your dynamic pages!
9
+
10
+ 1.) Configuration
11
+
12
+ First we need to do a little configuration. We need to know a few details about where your Radiant server lives.
13
+
14
+ If you are using the application_configuration gem (and why wouldn't you be?) simply the set the following parameter:
15
+ radiant_server_url: http://www.myawesomeradiantserver.com
16
+
17
+ If you are not using the application_configuration (loser), then you can configure this parameter by setting the constant 'RADIANT_SERVER_URL'.
18
+
19
+ Now your Rails application has all it needs to start including dynamic sections of pages from your Radiant CMS.
20
+
21
+ 2.) Usage
22
+
23
+ Ok, so now we know where to find Radiant we can start incorporating it into your Rails application.
24
+
25
+ Example: some_page.rhtml
26
+ <html>
27
+ <head>
28
+ <title>My Awesome Rails App!</title>
29
+ </head>
30
+ <body>
31
+ Welcome: <%= current_user.display_name %>!
32
+
33
+ <%= load_radiant_page("my-news-area") %>
34
+
35
+ You last logged in at: <%= current_user.last_login_time %>
36
+
37
+ </body>
38
+ </html>
39
+
40
+ Did you see the tag '<%= load_radiant_page("my-news-area") %>'? If you did, good for you! That's the magic that drives this whole thing!
41
+
42
+ The 'load_radiant_page' method takes one required parameter, and an optional second parameter. The first parameter is the slug name for the Radiant CMS page you are trying to access. It will join this with the 'radiant_server_url' parameter you set up earlier. Woah! That was easy!!
43
+
44
+ The optional second parameter is a Hash. Any values you pass in on this Hash will be converted to query string parameters and passed on to the Radiant CMS page you are trying to access.
45
+
46
+ *** Controller Level Usage ***
47
+
48
+ Ok, so we've talked about how to use Radiant to drive small sections of your page, well, what about if you want to use Radiant to drive the main content, or even the entire page from Radiant? Well, we got you covered!
49
+
50
+ Let's say you have a controller that's entirely dedicated to this task. The controller could look as simple as:
51
+
52
+ class CmsController < ApplicationController
53
+
54
+ action_requires :slug
55
+
56
+ def index
57
+ render(Radiant::Helper::Page.render(params))
58
+ end
59
+
60
+ end
61
+
62
+ And your routes.rb would have the following entry:
63
+
64
+ map.connect "cms/:slug", :controller => "cms", :action => "index"
65
+
66
+ The Radiant::Helper::Page.render method takes a Hash, which will mostly like be your params Hash, although it does not have to. What it DOES need to contain though is a :slug parameter which tells the Radiant::Helper::Page.render method which slug in the Radiant CMS it is going to try and access. Any other entries in the params Hash, with the exception of :action, :controller, and :slug, will be passed on as query string parameters to the Radiant slug you are trying to access.
67
+
68
+ When the Radiant::Helper::Page.render method gets back the results from Radiant if the result is not a success (200) it will raise a Radiant::Helper::InvalidResponseError. Assuming the response is a success the next thing the Radiant::Helper::Page.render method will do is to determine if it already has a layout, and if not, which layout it should have.
69
+
70
+ Let's look at this a little more closely, shall we?
71
+
72
+ If we get a response back from Radiant that contains a <html> and a </html> tag, the Radiant::Helper::Page.render method assumes that it is using a Radiant layout, and will NOT add a layout from you Rails app on top of that.
73
+
74
+ If the response does NOT contain these tags then the Radiant::Helper::Page.render method will try and determine which layout it should wrap the response with. This is done using two different configuration variables.
75
+
76
+ Example:
77
+ cms_layouts:
78
+ about: my_test_layout
79
+ foo: another_layout
80
+ default_cms_layout: application
81
+
82
+ The previous example configuration was for the application_configuration gem, if you are using constants set these config parameters using 'CMS_LAYOUTS' and 'DEFAULT_CMS_LAYOUT'.
83
+
84
+ The 'default_cms_layout' config parameter is the layout that will get used if no other layout is defined for a particular slug. This is pretty simple and straight forward.
85
+
86
+ The 'cms_layouts' config parameter is a Hash that maps a slug to the Rails layout you wish to use for that slug.
87
+
88
+ *** That's it! ***
89
+
90
+ Enjoy.
91
+
92
+ }.strip
93
+ puts ""
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ #require 'radiant_helper'
@@ -0,0 +1,62 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'radiant_helper_errors'
4
+ require 'radiant_helper_page'
5
+ require 'radiant_helper_response'
6
+ require 'singleton'
7
+ module Radiant
8
+ module Helper
9
+
10
+ def self.cms_layouts
11
+ get_config_param(:cms_layouts) || {}
12
+ end
13
+
14
+ def self.radiant_server_url
15
+ get_config_param(:radiant_server_url)
16
+ end
17
+
18
+ def self.default_cms_layout
19
+ get_config_param(:default_cms_layout)
20
+ end
21
+
22
+ def self.logger
23
+ if const_defined?("RAILS_DEFAULT_LOGGER")
24
+ return RAILS_DEFAULT_LOGGER
25
+ else
26
+ return Radiant::Helper::RLogger.instance.logger
27
+ end
28
+ end
29
+
30
+ private
31
+ def self.get_config_param(config_name)
32
+ config_name = config_name.to_s
33
+ if self.respond_to?(:app_config)
34
+ p = app_config.send(config_name)
35
+ if p.nil?
36
+ raise Radiant::Helper::InvalidConfigurationError.new(config_name)
37
+ end
38
+ return p
39
+ else
40
+ if const_defined?(config_name.upcase)
41
+ return eval(config_name.upcase)
42
+ else
43
+ raise Radiant::Helper::InvalidConfigurationError.new(config_name)
44
+ end
45
+ end
46
+ end
47
+
48
+ class RLogger
49
+ include Singleton
50
+
51
+ attr_accessor :logger
52
+
53
+ def initialize
54
+ self.logger = Logger.new(STDOUT)
55
+ end
56
+
57
+ end
58
+
59
+ end # Helper
60
+ end # Radiant
61
+
62
+ require 'radiant_helper_text_helper'
@@ -0,0 +1,25 @@
1
+ module Radiant
2
+ module Helper
3
+
4
+ class InvalidResponseError < Exception
5
+ end # InvalidResponseError
6
+
7
+ class InvalidConfigurationError < Exception
8
+
9
+ def initialize(config_name)
10
+ super(%{
11
+ Radiant::Helper was not initialized correctly!
12
+
13
+ If you are using the application_configuration gem then set the config param '#{config_name.downcase}'
14
+ equal to the host of your Radiant server.
15
+
16
+ If you are not using this gem (why aren't you?) then you can configure this setting by
17
+ setting a constant called: '#{config_name.upcase}'.
18
+ }.strip)
19
+ end
20
+
21
+ end # InvalidConfigurationError
22
+
23
+
24
+ end # Helper
25
+ end # Radiant
@@ -0,0 +1,52 @@
1
+ module Radiant
2
+ module Helper
3
+
4
+ class Page
5
+
6
+ class << self
7
+
8
+ def retrieve(slug, options = {})
9
+ begin
10
+ url = File.join(Radiant::Helper.radiant_server_url, slug)
11
+ if options.is_a? Hash
12
+ opts = options.dup
13
+ unless opts.nil? && opts.empty?
14
+ opts.delete(:controller)
15
+ opts.delete(:action)
16
+ opts.delete(:slug)
17
+ url << "?"
18
+ url << opts.collect {|k,v| "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}"}.join("&")
19
+ end
20
+ end
21
+ # pp url
22
+ response = Net::HTTP.get_response(URI.parse(url))
23
+ return Radiant::Helper::Response.new(url, response.code, response.body)
24
+ rescue Radiant::Helper::InvalidConfigurationError => e
25
+ raise e
26
+ rescue Exception => e
27
+ Radiant::Helper.logger.error(e)
28
+ return Radiant::Helper::Response.new(url, 404)
29
+ end
30
+ end
31
+
32
+ def render(params)
33
+ slug = params[:slug] || params["slug"]
34
+ raise RuntimeError.new("You MUST define a slug parameter!") if slug.blank?
35
+ res = Radiant::Helper::Page.retrieve(slug, params)
36
+ if res.success?
37
+ opts = {:text => res.body}
38
+ unless res.has_layout?
39
+ cms_layouts = Radiant::Helper.cms_layouts
40
+ opts[:layout] = cms_layouts[slug] || Radiant::Helper.default_cms_layout
41
+ end
42
+ return opts
43
+ end
44
+ raise res.exception
45
+ end
46
+
47
+ end
48
+
49
+ end # Page
50
+
51
+ end # Helper
52
+ end # Radiant
@@ -0,0 +1,45 @@
1
+ module Radiant
2
+ module Helper
3
+
4
+ class Response
5
+
6
+ attr_accessor :status
7
+ attr_accessor :body
8
+ attr_accessor :url
9
+
10
+ def initialize(url, status, body = "")
11
+ self.url = url
12
+ self.status = status.to_i
13
+ self.body = body
14
+ end
15
+
16
+ def success?
17
+ self.status == 200
18
+ end
19
+
20
+ def to_s
21
+ self.success? ? self.body : ""
22
+ end
23
+
24
+ def to_str
25
+ self.to_s
26
+ end
27
+
28
+ def has_layout?
29
+ ivar_cache("has_layout") do
30
+ unless self.body.nil?
31
+ b = self.body.downcase
32
+ return ((b.match("<html>") ? true : false) && (b.match("</html>") ? true : false))
33
+ end
34
+ return false
35
+ end
36
+ end
37
+
38
+ def exception
39
+ Radiant::Helper::InvalidResponseError.new("URL: #{self.url} did not return a valid response! Status: #{self.status}") unless self.success?
40
+ end
41
+
42
+ end # Response
43
+
44
+ end # Helper
45
+ end # Radiant
@@ -0,0 +1,23 @@
1
+ module ActionView
2
+ module Helpers
3
+ module TextHelper
4
+
5
+ def load_radiant_page(slug, options = {})
6
+ text = "<!-- Loading cms slug: '#{slug}' -->\n"
7
+ begin
8
+ res = Radiant::Helper::Page.retrieve(slug, request)
9
+ if res.success?
10
+ text << res.body
11
+ else
12
+ raise res.exception
13
+ end
14
+ rescue Exception => e
15
+ text << "<!-- Error: #{e.message} -->"
16
+ end
17
+ text << "\n<!-- Loaded cms slug: '#{slug}' -->"
18
+ text
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ --- !ruby/object:RubyForgeConfig
2
+ gem_name: radiant_helper
3
+ package: radiant_helper
4
+ project: magrathea
5
+ version: 1.0.3
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: radiant_helper
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.3
7
+ date: 2007-10-16 00:00:00 -04:00
8
+ summary: radiant_helper
9
+ require_paths:
10
+ - lib
11
+ - lib
12
+ - lib
13
+ - lib/tasks
14
+ email:
15
+ homepage:
16
+ rubyforge_project: magrathea
17
+ description: "radiant_helper was developed by: markbates"
18
+ autorequire:
19
+ - radiant_helper_text_helper
20
+ - radiant_helper_response
21
+ - radiant_helper_page
22
+ - radiant_helper_errors
23
+ - radiant_helper
24
+ default_executable:
25
+ bindir: bin
26
+ has_rdoc: false
27
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
28
+ requirements:
29
+ - - ">"
30
+ - !ruby/object:Gem::Version
31
+ version: 0.0.0
32
+ version:
33
+ platform: ruby
34
+ signing_key:
35
+ cert_chain:
36
+ post_install_message:
37
+ authors:
38
+ - markbates
39
+ files:
40
+ - init.rb
41
+ - lib/radiant_helper.rb
42
+ - lib/radiant_helper_errors.rb
43
+ - lib/radiant_helper_page.rb
44
+ - lib/radiant_helper_response.rb
45
+ - lib/radiant_helper_text_helper.rb
46
+ - lib/tasks/rubyforge_config.yml
47
+ test_files: []
48
+
49
+ rdoc_options: []
50
+
51
+ extra_rdoc_files: []
52
+
53
+ executables:
54
+ - radiant_helper
55
+ extensions: []
56
+
57
+ requirements: []
58
+
59
+ dependencies: []
60
+