Class: Rufus::Sixjo::Context
Overview
The context in which an HTTP request is handled
Instance Attribute Summary collapse
-
#application ⇒ Object
readonly
Returns the value of attribute application.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Class Method Summary collapse
Instance Method Summary collapse
- #h(text) ⇒ Object
-
#initialize(app, env) ⇒ Context
constructor
A new instance of Context.
- #metaclass ⇒ Object
- #params ⇒ Object
- #redirect(path, status = 303, body = nil) ⇒ Object
-
#set_etag(t, weak = false) ⇒ Object
throws 304 as needed.
-
#set_last_modified(t) ⇒ Object
throws 304 as needed.
Methods included from Erb
Constructor Details
#initialize(app, env) ⇒ Context
Returns a new instance of Context.
227 228 229 230 231 232 233 234 235 |
# File 'lib/rufus/sixjo.rb', line 227 def initialize (app, env) @application = app @request = env.delete('_rack_request') || Rack::Request.new(env) @response = Rack::Response.new @params = @request.params.merge(@request.env['_ROUTE_PARAMS']) @params = @params.inject({}) { |r, (k, v)| r[k.to_sym] = v; r } end |
Instance Attribute Details
#application ⇒ Object (readonly)
Returns the value of attribute application.
224 225 226 |
# File 'lib/rufus/sixjo.rb', line 224 def application @application end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
225 226 227 |
# File 'lib/rufus/sixjo.rb', line 225 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
225 226 227 |
# File 'lib/rufus/sixjo.rb', line 225 def response @response end |
Class Method Details
.service(app, block, helpers, env) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/rufus/sixjo.rb', line 237 def self.service (app, block, helpers, env) r = self.new(app, env) helpers.each { |h| r.instance_eval &h } r..instance_eval { define_method :call, &block } begin caught = catch :done do r.response.body = r.call || [] nil end if caught caught = Array(caught) r.response.status = caught[0] r.response.body = caught[1] end rescue Exception => e r.response.status = 500 r.response.content_type = 'text/plain' r.response.body = e.to_s + "\n" + e.backtrace.join("\n") end r.response.finish end |
Instance Method Details
#h(text) ⇒ Object
272 273 274 |
# File 'lib/rufus/sixjo.rb', line 272 def h (text) Rack::Utils.escape_html(text) end |
#metaclass ⇒ Object
218 219 220 221 222 |
# File 'lib/rufus/sixjo.rb', line 218 def class << self self end end |
#params ⇒ Object
268 269 270 |
# File 'lib/rufus/sixjo.rb', line 268 def params @params end |
#redirect(path, status = 303, body = nil) ⇒ Object
276 277 278 279 280 281 |
# File 'lib/rufus/sixjo.rb', line 276 def redirect (path, status=303, body=nil) @response.status = status @response.location = path @response.body = body || "#{status} redirecting to #{path}" throw :done end |
#set_etag(t, weak = false) ⇒ Object
throws 304 as needed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/rufus/sixjo.rb', line 286 def set_etag (t, weak=false) t = '"%s"' % t t = weak ? "W/#{t}" : t @response.header['ETag'] = t = @request. if .include?(t) or .include?('*') throw(:done, 304) if @request.get? or @request.head? throw(:done, 412) # precondition failed end end |
#set_last_modified(t) ⇒ Object
throws 304 as needed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/rufus/sixjo.rb', line 304 def set_last_modified (t) t = Time.local(*t.to_a) # flatten milliseconds @response.header['Last-Modified'] = t.httpdate sin = @request.env['HTTP_IF_MODIFIED_SINCE'] return unless sin # taken from the "Ruby Cookbook" by # Lucas Carlson and Leonard Richardson # sin = DateTime.parse(sin) sin = sin.new_offset(DateTime.now.offset - sin.offset) sin = Time.local( sin.year, sin.month, sin.day, sin.hour, sin.min, sin.sec, 0) if sin >= t throw(:done, 304) if @request.get? or @request.head? throw(:done, 412) # precondition failed end end |