Class: Httpdoc::Rendering::ControllerContext

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(renderer, controller) ⇒ ControllerContext

Returns a new instance of ControllerContext.



26
27
28
29
# File 'lib/httpdoc/rendering.rb', line 26

def initialize(renderer, controller)
  @renderer = renderer
  @controller = controller
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



117
118
119
# File 'lib/httpdoc/rendering.rb', line 117

def controller
  @controller
end

Instance Method Details

#base_urlObject



59
60
61
62
63
# File 'lib/httpdoc/rendering.rb', line 59

def base_url
  controller_url = @controller.url
  controller_url ||= "/"
  return URI.join(@renderer.base_url, controller_url).to_s
end

#doc_fragment_to_html(s) ⇒ Object



49
50
51
52
# File 'lib/httpdoc/rendering.rb', line 49

def doc_fragment_to_html(s)
  s = s.gsub("\n", ' ')
  RedCloth.new(s).to_html
end

#escape_html(h) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/httpdoc/rendering.rb', line 65

def escape_html(h)
  return nil unless h
  h = h.dup
  h.gsub!("&", "&")
  h.gsub!("<", "&lt;")
  h.gsub!(">", "&gt;")
  h.gsub!("\n", "<br/>")
  h
end

#expand_url_with_subtitutions(url) ⇒ Object



54
55
56
57
# File 'lib/httpdoc/rendering.rb', line 54

def expand_url_with_subtitutions(url)
  url = [base_url, url].join("/") unless url =~ /^\//
  return URI.join(@renderer.base_url, url).to_s.gsub(/:([\w_]+)/, '<strong>&lt;\1&gt;</strong>')
end

#format_request(req) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/httpdoc/rendering.rb', line 75

def format_request(req)
  envelope, body = req.split("\n\n")
  lines = envelope.split("\n")
  first_line, header_lines = lines[0], (lines[1..-1] || [])
  result = "<div class='request'>"
  result << "<strong class='request_first_line'>#{h(first_line)}</strong><br/>"
  result << header_lines.map { |h|
    name, value = h.split(":\s*")
    "<span class='request_header_line'><strong class='request_header_name'>#{escape_html(name)}" <<
      "</strong>: <span class='request_header_value'>#{escape_html(value)}</span></span>"
  }.join("<br/>")
  if body and body != ''
    result << "<br/>"
    result << "<div class='request_body'>#{escape_html(body.strip)}</div>"
  end
  result << "</div>"
  result
end

#format_response(res) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/httpdoc/rendering.rb', line 94

def format_response(res)
  envelope, body = res.split("\n\n")
  lines = envelope.split("\n")
  first_line, header_lines = lines[0], (lines[1..-1] || [])
  result = "<div class='response'>"
  result << "<strong class='response_first_line'>#{h(first_line)}</strong><br/>"
  result << header_lines.map { |h|
    name, value = h.scan(/(.+):\s*(.+)/)[0]
    "<span class='response_header_line'><strong class='response_header_name'>#{escape_html(name)}" <<
      "</strong>: <span class='response_header_value'>#{escape_html(value)}</span></span>"
  }.join("<br/>")
  if body and body != ''
    result << "<br/><br/>"
    result << "<div class='response_body'>#{escape_html(body)}</div>"
  end
  result << "</div>"
  result
end

#get_bindingObject



113
114
115
# File 'lib/httpdoc/rendering.rb', line 113

def get_binding
  return binding
end

#h(s) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/httpdoc/rendering.rb', line 31

def h(s)
  s ||= ''
  return s.gsub(/#\{(.*)\}/) {
    name = $1
    case name
      when /^[A-Z0-9_]+$/
        value = @controller.constants[name]
        raise UndefinedConstantError.new(name) unless value
        value = eval(value)
      when "base_url"
        value = base_url.gsub(/\/$/, '')
      else
        raise UndefinedVariableError, name
    end
    value
  }
end