Skip to content

Commit

Permalink
Add YAML front matter parsing for response files
Browse files Browse the repository at this point in the history
  • Loading branch information
knuton committed Jan 8, 2012
1 parent 39a57d6 commit 0f73fa3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
6 changes: 4 additions & 2 deletions lib/second_mate/combined_logger.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,7 @@
module SecondMate module SecondMate


class CombinedLogger < Rack::CommonLogger class CombinedLogger < Rack::CommonLogger
FORMAT = %{%s - %s [%s] "%s %s%s %s" %d %s %0.4f "%s"\n} FORMAT = %{%s - %s [%s] "%s %s%s %s" %d %s %0.4f "%s" "%s"\n}


def log(env, status, header, began_at) def log(env, status, header, began_at)
now = Time.now now = Time.now
Expand All @@ -19,8 +19,10 @@ def log(env, status, header, began_at)
status.to_s[0..3], status.to_s[0..3],
length, length,
now - began_at, now - began_at,
header['second_mate.response_file'] || 'NONE' header.delete('second_mate.response_file') || 'NONE',
"YAML Frontmatter: #{header.delete('second_mate.yaml_frontmatter') || 'No'}"
] ]

end end


end end
Expand Down
36 changes: 32 additions & 4 deletions lib/second_mate/response.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,13 +4,41 @@ module SecondMate


class Response < Rack::Response class Response < Rack::Response


attr_accessor :body, :params, :status, :header

def initialize(body=[], params={}, status=200, header={}) def initialize(body=[], params={}, status=200, header={})
super render_template(body, params), status, header @body = body
@params = params
@status = status
@header = header

process_yaml
render_template

super self.body, self.status, self.header
end

private
def process_yaml
if self.body =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
self.body = self.body[($1.size + $2.size)..-1]
begin
data = YAML.load($1)

# Use specified HTTP status
self.status = data['status'] if data['status']
# Fill header information
data['header'].each { |field, value| self.header[field] = value } if data['header'].kind_of? Hash
self.header['second_mate.yaml_frontmatter'] = 'Yes'
rescue => e
self.header['second_mate.yaml_frontmatter'] = 'Error'
end
end
end end


def render_template(template, params) def render_template
erb = ERB.new template erb = ERB.new @body
erb.result binding @body = erb.result binding
end end


end end
Expand Down
23 changes: 23 additions & 0 deletions test/test_response.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'test/unit'

require 'second_mate'

class TestResponse < Test::Unit::TestCase

def setup
@response = SecondMate::Response.new(
"---\nstatus: 201\nheader:\n Foo: Baz\n---\nBody",
{},
200,
{'Foo' => 'Bar'}
)
end

def test_yaml_frontmatter
response = @response.finish
assert_equal ['Body'], response.last.body
assert_equal 'Baz', response[1]['Foo']
end

end

0 comments on commit 0f73fa3

Please sign in to comment.