Class: Rufus::Sixjo::Context

Inherits:
Object
  • Object
show all
Includes:
Erb
Defined in:
lib/rufus/sixjo.rb

Overview

The context in which an HTTP request is handled

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Erb

#erb

Constructor Details

#initialize(app, env) ⇒ Context

Returns a new instance of Context.



249
250
251
252
253
254
255
256
257
# File 'lib/rufus/sixjo.rb', line 249

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

#applicationObject (readonly)

Returns the value of attribute application.



246
247
248
# File 'lib/rufus/sixjo.rb', line 246

def application
  @application
end

#requestObject (readonly)

Returns the value of attribute request.



247
248
249
# File 'lib/rufus/sixjo.rb', line 247

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



247
248
249
# File 'lib/rufus/sixjo.rb', line 247

def response
  @response
end

Class Method Details

.service(app, block, helpers, env) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/rufus/sixjo.rb', line 259

def self.service (app, block, helpers, env)

  r = self.new(app, env)

  helpers.each { |h| r.instance_eval &h }

  r.metaclass.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.body = [] if env['REQUEST_METHOD'] == 'HEAD'
    #
    # remove body in case of HEAD

  r.response.finish
end

Instance Method Details

#h(text) ⇒ Object



298
299
300
# File 'lib/rufus/sixjo.rb', line 298

def h (text)
  Rack::Utils.escape_html(text)
end

#metaclassObject



240
241
242
243
244
# File 'lib/rufus/sixjo.rb', line 240

def metaclass
  class << self
    self
  end
end

#paramsObject



294
295
296
# File 'lib/rufus/sixjo.rb', line 294

def params
  @params
end

#redirect(path, status = 303, body = nil) ⇒ Object



302
303
304
305
306
307
# File 'lib/rufus/sixjo.rb', line 302

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



312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/rufus/sixjo.rb', line 312

def set_etag (t, weak=false)

  t = '"%s"' % t
  t = weak ? "W/#{t}" : t

  @response.header['ETag'] = t

  etags = @request.etags

  if etags.include?(t) or etags.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



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/rufus/sixjo.rb', line 330

def set_last_modified (t)

  #t = Time.local(*t.to_a) # flatten milliseconds
  #@response.header['Last-Modified'] = t.httpdate

  t = t.httpdate
  @response.header['Last-Modified'] = t

  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
  if sin == t
    throw(:done, 304) if @request.get? or @request.head?
    throw(:done, 412) # precondition failed
  end
end