Module: Junior::Helpers

Included in:
Controller
Defined in:
lib/junior/helpers.rb

Overview

Methods available to routes, before/after filters, and views.

Instance Method Summary collapse

Instance Method Details

#attachment(filename = nil) ⇒ Object

Set the Content-Disposition to “attachment” with the specified filename, instructing the user agents to prompt to save.



80
81
82
83
84
85
86
# File 'lib/junior/helpers.rb', line 80

def attachment(filename=nil)
  response['Content-Disposition'] = 'attachment'
  if filename
    params = '; filename="%s"' % File.basename(filename)
    response['Content-Disposition'] << params
  end
end

#body(value = nil, &block) ⇒ Object

Set or retrieve the response body. When a block is given, evaluation is deferred until the body is read with #each.



21
22
23
24
25
26
27
28
# File 'lib/junior/helpers.rb', line 21

def body(value=nil, &block)
  if block_given?
    def block.each ; yield call ; end
    response.body = block
  else
    response.body = value
  end
end

#content_type(type, params = {}) ⇒ Object

Set the Content-Type of the response body given a media type or file extension.



67
68
69
70
71
72
73
74
75
76
# File 'lib/junior/helpers.rb', line 67

def content_type(type, params={})
  mime_type = self.mime_type( ".#{type}" )
  fail "Unknown media type: %p" % type if mime_type.nil?
  if params.any?
    params = params.collect { |kv| "%s=%s" % kv }.join(', ')
    response['Content-Type'] = [mime_type, params].join(";")
  else
    response['Content-Type'] = mime_type
  end
end

#error(code, body = nil) ⇒ Object

Halt processing and return the error status provided.



38
39
40
41
42
# File 'lib/junior/helpers.rb', line 38

def error(code, body=nil)
  code, body    = 500, code.to_str if code.respond_to? :to_str
  response.body = body unless body.nil?
  halt code
end

#halt(*response) ⇒ Object

Exit the current block, halts any further processing of the request, and returns the specified response.



8
9
10
11
# File 'lib/junior/helpers.rb', line 8

def halt(*response)
  response = response.first if response.length == 1
  throw :halt, response
end

#headers(hash = nil) ⇒ Object

Set multiple response headers with Hash.



50
51
52
53
# File 'lib/junior/helpers.rb', line 50

def headers(hash=nil)
  response.headers.merge! hash if hash
  response.headers
end

#last_modified(time) ⇒ Object

Set the last modified time of the resource (HTTP ‘Last-Modified’ header) and halt if conditional GET matches. The time argument is a Time, DateTime, or other object that responds to to_time.

When the current request includes an ‘If-Modified-Since’ header that matches the time specified, execution is immediately halted with a ‘304 Not Modified’ response.



95
96
97
98
99
100
101
# File 'lib/junior/helpers.rb', line 95

def last_modified(time)
  time = time.to_time if time.respond_to?(:to_time)
  time = time.httpdate if time.respond_to?(:httpdate)
  response['Last-Modified'] = time
  halt 304 if time == request.env['HTTP_IF_MODIFIED_SINCE']
  time
end

#mime_type(type) ⇒ Object

Look up a media type by file extension in Rack’s mime registry.



61
62
63
# File 'lib/junior/helpers.rb', line 61

def mime_type(type)
  Rack::Mime.mime_type(type)
end

#not_found(body = nil) ⇒ Object

Halt processing and return a 404 Not Found.



45
46
47
# File 'lib/junior/helpers.rb', line 45

def not_found(body=nil)
  error 404, body
end

#redirect(uri, *args) ⇒ Object

Halt processing and redirect to the URI provided.



31
32
33
34
35
# File 'lib/junior/helpers.rb', line 31

def redirect(uri, *args)
  status 302
  response['Location'] = uri
  halt(*args)
end

#send_file(path, opts = {}) ⇒ Object

Use the contents of the file at path as the response body.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/junior/helpers.rb', line 104

def send_file(path, opts={})
  stat = File.stat(path)
  last_modified stat.mtime

  content_type mime_type(opts[:type]) ||
    mime_type(File.extname(path)) ||
    response['Content-Type'] ||
    'application/octet-stream'

  response['Content-Length'] ||= (opts[:length] || stat.size).to_s

  if opts[:disposition] == 'attachment' || opts[:filename]
    attachment opts[:filename] || path
  elsif opts[:disposition] == 'inline'
    response['Content-Disposition'] = 'inline'
  end

  halt StaticFile.open(path, 'rb')
rescue Errno::ENOENT
  not_found
end

#sessionObject

Access the underlying Rack session.



56
57
58
# File 'lib/junior/helpers.rb', line 56

def session
  env['rack.session'] ||= {}
end

#status(value = nil) ⇒ Object

Set or retrieve the response status code.



14
15
16
17
# File 'lib/junior/helpers.rb', line 14

def status(value=nil)
  response.status = value if value
  response.status
end