class Rack::Info
Constants
- CONTENT_TYPE_HEADER
- HTML_CONTENT_TYPE
- VERSION
Attributes
app[R]
config[R]
metadata[R]
Public Class Methods
header_name(str)
click to toggle source
# File lib/rack/info.rb, line 14 def self.header_name(str) "X-" + str.to_s.split(/[-_ ]/).map(&:capitalize).join("-") end
header_value(obj)
click to toggle source
# File lib/rack/info.rb, line 18 def self.header_value(obj) obj.to_s end
new(app, hsh_or_config = {})
click to toggle source
# File lib/rack/info.rb, line 24 def initialize(app, hsh_or_config = {}) @app = app @config = Config.from(hsh_or_config) @data_headers = to_headers(@config.data) end
Public Instance Methods
call(env)
click to toggle source
# File lib/rack/info.rb, line 30 def call(env) return app.call(env) unless config.enabled?(env) return json_rsp if config.path == env["PATH_INFO"] status, headers, body = @app.call(env) headers.merge!(@data_headers) if config.add_headers?(env, [status, headers, body]) if html?(headers) && config.add_html?(env, [status, headers, body]) body = add_html(body) end [status, headers, body] end
Private Instance Methods
add_html(body)
click to toggle source
# File lib/rack/info.rb, line 60 def add_html(body) content = "" body.each {|ea| content << ea} new_html_content = config.html_formatter.format(config.data) [ content.sub(config.insert_html_after) {|match| match + new_html_content } ] end
html?(headers)
click to toggle source
# File lib/rack/info.rb, line 55 def html?(headers) headers[CONTENT_TYPE_HEADER] && headers[CONTENT_TYPE_HEADER].start_with?(HTML_CONTENT_TYPE) end
json_rsp()
click to toggle source
# File lib/rack/info.rb, line 51 def json_rsp [200, {"Content-Type" => "application/json"}, [MultiJson.dump(config.data)]] end
to_headers(hsh)
click to toggle source
# File lib/rack/info.rb, line 45 def to_headers(hsh) Hash[@config.data.map do |k, v| [self.class.header_name(k), self.class.header_value(v)] end] end