Class: Goliath::Rack::Formatters::HTML

Inherits:
Object
  • Object
show all
Defined in:
lib/goliath/rack/formatters/html.rb

Overview

A simple HTML formatter. This doesn’t try to be fancy and just turns Hashes into tables, Arrays into ordered lists and strings are output as HTML escaped strings.

Examples:

use Goliath::Rack::Formatters::HTML

Instance Method Summary (collapse)

Constructor Details

- (Goliath::Rack::Formatters::HTML) initialize(app)

Called by the framework to create the formatter.

Parameters:

  • app

    The application



17
18
19
# File 'lib/goliath/rack/formatters/html.rb', line 17

def initialize(app)
  @app = app
end

Instance Method Details

- (Object) array_to_html(content)



73
74
75
76
77
78
# File 'lib/goliath/rack/formatters/html.rb', line 73

def array_to_html(content)
  html_string = '<ol>\n'
  content.each { |value| html_string += "<li>#{to_html(value)}</li>\n" }
  html_string +="</ol>\n"
  html_string
end

- (Object) call(env)



21
22
23
24
25
26
27
28
29
# File 'lib/goliath/rack/formatters/html.rb', line 21

def call(env)
  async_cb = env['async.callback']
  env['async.callback'] = Proc.new do |status, headers, body|
    async_cb.call(post_process(status, headers, body))
  end

  status, headers, body = @app.call(env)
  post_process(status, headers, body)
end

- (Object) hash_to_html(content)



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/goliath/rack/formatters/html.rb', line 59

def hash_to_html(content)
  html_string = "<table>\n"
  if content.key?('meta')
    html_string += "<tr><td>meta</td><td>\n"
    html_string += to_html(content['meta'])
    html_string += "</td></tr>\n"
    content.delete('meta')
  end

  content.each_pair { |key, value| html_string += "<tr><td>#{to_html(key)}</td><td>#{to_html(value)}</td></tr>\n" }
  html_string += "</table>\n"
  html_string
end


84
85
86
# File 'lib/goliath/rack/formatters/html.rb', line 84

def html_footer
  "</body></html>"
end

- (Object) html_header



80
81
82
# File 'lib/goliath/rack/formatters/html.rb', line 80

def html_header
  "<html><body>"
end

- (Boolean) html_response?(headers)

Returns:

  • (Boolean)


36
37
38
# File 'lib/goliath/rack/formatters/html.rb', line 36

def html_response?(headers)
  headers['Content-Type'] =~ %r{^text/html}
end

- (Object) post_process(status, headers, body)



31
32
33
34
# File 'lib/goliath/rack/formatters/html.rb', line 31

def post_process(status, headers, body)
  body = [to_html(body, false)] if html_response?(headers)
  [status, headers, body]
end

- (Object) string_to_html(content)



55
56
57
# File 'lib/goliath/rack/formatters/html.rb', line 55

def string_to_html(content)
  ::Rack::Utils.escape_html(content.to_s)
end

- (Object) to_html(content, fragment = true)



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/goliath/rack/formatters/html.rb', line 40

def to_html(content, fragment=true)
  html_string = ''
  html_string += html_header unless fragment

  html_string += case(content.class.to_s)
  when "Hash" then hash_to_html(content)
  when "Array" then array_to_html(content)
  when "String" then string_to_html(content)
  else string_to_html(content)
  end

  html_string += html_footer unless fragment
  html_string
end