Class: Radiant::Helper::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/radiant_helper_page.rb

Class Method Summary collapse

Class Method Details

.render(params) ⇒ Object

Raises:

  • (RuntimeError)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/radiant_helper_page.rb', line 32

def render(params)
  slug = params[:slug] || params["slug"]
  raise RuntimeError.new("You MUST define a slug parameter!") if slug.blank?
  res = Radiant::Helper::Page.retrieve(slug, params)
  if res.success?
    opts = {:text => res.body}
    unless res.has_layout?
      cms_layouts = Radiant::Helper.cms_layouts
      opts[:layout] = cms_layouts[slug] || Radiant::Helper.default_cms_layout
    end
    return opts
  end
  raise res.exception
end

.retrieve(slug, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/radiant_helper_page.rb', line 8

def retrieve(slug, options = {})
  begin
    url = File.join(Radiant::Helper.radiant_server_url, slug)
    if options.is_a? Hash
      opts = options.dup 
      unless opts.nil? && opts.empty?
        opts.delete(:controller)
        opts.delete(:action)
        opts.delete(:slug)
        url << "?"
        url << opts.collect {|k,v| "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}"}.join("&")
      end
    end
    # pp url
    response = Net::HTTP.get_response(URI.parse(url))
    return Radiant::Helper::Response.new(url, response.code, response.body)
  rescue Radiant::Helper::InvalidConfigurationError => e
    raise e
  rescue Exception => e
    Radiant::Helper.logger.error(e)
    return Radiant::Helper::Response.new(url, 404)
  end
end