Class: Cameleon::Renderer
- Inherits:
-
Object
- Object
- Cameleon::Renderer
- Defined in:
- lib/cameleon/renderer.rb
Class Method Summary collapse
- .render_file(base_path, req, filename = nil) ⇒ Object
- .run_switch(base_path, req, switch_path) ⇒ Object
Instance Method Summary collapse
- #__eval_switch(switch_src) ⇒ Object
- #build_response ⇒ Object
- #delete(&block) ⇒ Object
- #find_default_file ⇒ Object
- #get(&block) ⇒ Object
-
#initialize(base_path, req) ⇒ Renderer
constructor
A new instance of Renderer.
- #json_body ⇒ Object
- #on(*http_methods, &block) ⇒ Object
- #params ⇒ Object
- #post(&block) ⇒ Object
- #put(&block) ⇒ Object
- #render(filename = nil) ⇒ Object
- #set_header(name, value) ⇒ Object
- #status_code(code) ⇒ Object
- #xml_body ⇒ Object
Constructor Details
#initialize(base_path, req) ⇒ Renderer
Returns a new instance of Renderer.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/cameleon/renderer.rb', line 21 def initialize(base_path, req) @base_path = base_path @status_code = 200 @headers = { "Content-Type" => "text/plain" } @req = req @params = Hashie::Mash.new req.params req.body.rewind @body = req.body.read @method = req.env["REQUEST_METHOD"] end |
Class Method Details
.render_file(base_path, req, filename = nil) ⇒ Object
14 15 16 17 18 |
# File 'lib/cameleon/renderer.rb', line 14 def render_file(base_path, req, filename = nil) catch(:read) { Renderer.new(base_path, req).render(filename) } end |
.run_switch(base_path, req, switch_path) ⇒ Object
9 10 11 12 |
# File 'lib/cameleon/renderer.rb', line 9 def run_switch(base_path, req, switch_path) switch_src = File.read switch_path Renderer.new(base_path, req).__eval_switch(switch_src) end |
Instance Method Details
#__eval_switch(switch_src) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/cameleon/renderer.rb', line 34 def __eval_switch(switch_src) catch(:read) { eval switch_src render find_default_file } end |
#build_response ⇒ Object
53 54 55 |
# File 'lib/cameleon/renderer.rb', line 53 def build_response [@status_code, @headers, [@body]] end |
#delete(&block) ⇒ Object
83 84 85 |
# File 'lib/cameleon/renderer.rb', line 83 def delete(&block) on(:delete, &block) end |
#find_default_file ⇒ Object
95 96 97 |
# File 'lib/cameleon/renderer.rb', line 95 def find_default_file Dir.entries(@base_path).find { |f| f =~ /^default/ } end |
#get(&block) ⇒ Object
71 72 73 |
# File 'lib/cameleon/renderer.rb', line 71 def get(&block) on(:get, &block) end |
#json_body ⇒ Object
45 46 47 |
# File 'lib/cameleon/renderer.rb', line 45 def json_body @json_body || @json_body = Hashie::Mash.new(JSON.parse(@body)) end |
#on(*http_methods, &block) ⇒ Object
65 66 67 68 69 |
# File 'lib/cameleon/renderer.rb', line 65 def on(*http_methods, &block) if http_methods.map { |m| m.to_s.upcase }.include? @method yield end end |
#params ⇒ Object
41 42 43 |
# File 'lib/cameleon/renderer.rb', line 41 def params @params end |
#post(&block) ⇒ Object
79 80 81 |
# File 'lib/cameleon/renderer.rb', line 79 def post(&block) on(:post, &block) end |
#put(&block) ⇒ Object
75 76 77 |
# File 'lib/cameleon/renderer.rb', line 75 def put(&block) on(:put, &block) end |
#render(filename = nil) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/cameleon/renderer.rb', line 87 def render(filename = nil) filename = find_default_file unless filename src = File.read File.join(@base_path, filename) erb = Erubis::Eruby.new src @body = erb.result(binding) throw :read, self end |
#set_header(name, value) ⇒ Object
57 58 59 |
# File 'lib/cameleon/renderer.rb', line 57 def set_header(name, value) @headers[name] = value end |
#status_code(code) ⇒ Object
61 62 63 |
# File 'lib/cameleon/renderer.rb', line 61 def status_code(code) @status_code = code end |
#xml_body ⇒ Object
49 50 51 |
# File 'lib/cameleon/renderer.rb', line 49 def xml_body @xml_body || @xml_body = Nokogiri::XML::Document.parse(@body) end |