Class: Rack::Stereoscope
- Inherits:
-
Object
- Object
- Rack::Stereoscope
- Defined in:
- lib/rack/stereoscope.rb
Instance Method Summary collapse
- #build_page(content, request, response) ⇒ Object
- #call(env) ⇒ Object
- #data_to_html(data, builder) ⇒ Object
- #expand_template(request) ⇒ Object
-
#initialize(app) ⇒ Stereoscope
constructor
A new instance of Stereoscope.
- #list_to_html(data, builder) ⇒ Object
- #present_data(request, status, headers, body) ⇒ Object
- #table_to_html(data, builder) ⇒ Object
- #tabular?(data) ⇒ Boolean
- #template_to_html(text, builder) ⇒ Object
- #url?(text) ⇒ Boolean
- #url_template?(text) ⇒ Boolean
- #url_to_html(url, builder) ⇒ Object
Constructor Details
#initialize(app) ⇒ Stereoscope
Returns a new instance of Stereoscope.
41 42 43 |
# File 'lib/rack/stereoscope.rb', line 41 def initialize(app) @app = app end |
Instance Method Details
#build_page(content, request, response) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/rack/stereoscope.rb', line 74 def build_page(content, request, response) this = self mab = Markaby::Builder.new mab.html do head do title request.path end body do h1 "#{response.status} #{request.url}" if !content.to_s.empty? h2 "Response:" case response.content_type when 'application/json' then div do this.data_to_html(JSON.parse(content.join), mab) end when 'text/plain' then p content.join else text Nokogiri::HTML(content.join).css('body').inner_html end else p "(No content)" end h2 "Raw:" tt do raw_content = case response.content_type when 'application/json' JSON.pretty_generate(JSON.parse(content.join)) else content.join end pre raw_content end end end mab.to_s end |
#call(env) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rack/stereoscope.rb', line 45 def call(env) request = Rack::Request.new(env) if Rack::AcceptMediaTypes.new(env['HTTP_ACCEPT']).include?('text/html') status, headers, body = @app.call(env) if request.path == '/__stereoscope_expand_template__' (request) else present_data(request, status, headers, body) end else @app.call(env) end end |
#data_to_html(data, builder) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/rack/stereoscope.rb', line 114 def data_to_html(data, builder) this = self case data when Hash builder.dl do data.each_pair do |key, value| dt do this.data_to_html(key, builder) end dd do this.data_to_html(value, builder) end end end when Array if tabular?(data) table_to_html(data, builder) else list_to_html(data, builder) end when String if url?(data) if url_template?(data) template_to_html(data, builder) else url_to_html(data, builder) end else builder.div do data.split("\n").each do |line| builder.span line builder.br end end end else builder.span do data end end end |
#expand_template(request) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/rack/stereoscope.rb', line 66 def (request) template = Addressable::Template.new(request['__template__']) url = template.(request.params) response = Rack::Response.new response.redirect(url.to_s) response.finish end |
#list_to_html(data, builder) ⇒ Object
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/rack/stereoscope.rb', line 191 def list_to_html(data, builder) this = self builder.ol do data.each do |value| li do this.data_to_html(value, builder) end end end end |
#present_data(request, status, headers, body) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/rack/stereoscope.rb', line 59 def present_data(request, status, headers, body) response = Rack::Response.new("", status, headers) response.write(build_page(body, request, response)) response['Content-Type'] = 'text/html' response.finish end |
#table_to_html(data, builder) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/rack/stereoscope.rb', line 202 def table_to_html(data, builder) this = self builder.table do headers = data.first.keys thead do headers.each do |header| th do this.data_to_html(header, builder) end end end tbody do data.each do |row| tr do row.each do |key, value| td do this.data_to_html(value, builder) end end end end end end end |
#tabular?(data) ⇒ Boolean
162 163 164 165 166 |
# File 'lib/rack/stereoscope.rb', line 162 def tabular?(data) data.kind_of?(Array) && data.all?{|e| e.kind_of?(Hash)} && data[1..-1].all?{|e| e.keys == data.first.keys} end |
#template_to_html(text, builder) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/rack/stereoscope.rb', line 172 def template_to_html(text, builder) template = Addressable::Template.new(text) builder.div(:class => 'url-template-form') do p text form(:method => 'GET', :action => '/__stereoscope_expand_template__') do input(:type => 'hidden', :name => '__template__', :value => text) template.variables.each do |variable| div(:class => 'url-template-variable') do label do text "#{variable}: " input(:type => 'text', :name => variable) end end end input(:type => 'submit') end end end |
#url?(text) ⇒ Boolean
154 155 156 |
# File 'lib/rack/stereoscope.rb', line 154 def url?(text) Addressable::URI.parse(text.to_s).ip_based? end |
#url_template?(text) ⇒ Boolean
158 159 160 |
# File 'lib/rack/stereoscope.rb', line 158 def url_template?(text) !Addressable::Template.new(text.to_s).variables.empty? end |
#url_to_html(url, builder) ⇒ Object
168 169 170 |
# File 'lib/rack/stereoscope.rb', line 168 def url_to_html(url, builder) builder.a(url.to_s, :href => url.to_s) end |