Class: Expressr::Response

Inherits:
Noder::HTTP::Response
  • Object
show all
Defined in:
lib/expressr/response.rb

Constant Summary collapse

DEFAULT_STATUS =
200

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Response

Returns a new instance of Response.



10
11
12
13
14
# File 'lib/expressr/response.rb', line 10

def initialize(env)
  super(env)
  @request = env[:request]
  @cookies = {}
end

Instance Attribute Details

#cookiesObject

Returns the value of attribute cookies.



7
8
9
# File 'lib/expressr/response.rb', line 7

def cookies
  @cookies
end

#localsObject

Returns the value of attribute locals.



7
8
9
# File 'lib/expressr/response.rb', line 7

def locals
  @locals
end

#requestObject (readonly)

Returns the value of attribute request.



8
9
10
# File 'lib/expressr/response.rb', line 8

def request
  @request
end

#statusObject

Returns the value of attribute status.



7
8
9
# File 'lib/expressr/response.rb', line 7

def status
  @status
end

Instance Method Details

#attachment(filename = nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/expressr/response.rb', line 125

def attachment(filename=nil)
  content_disposition = 'attachment'
  if filename
    basename = File.basename(filename)
    content_disposition += %Q|; filename="#{basename}"|
    content_type = MIME::Types.type_for(filename).first.to_s
    type(content_type)
  end
  set_header('Content-Disposition', content_disposition)
end


40
41
42
# File 'lib/expressr/response.rb', line 40

def clear_cookie(name, options={})
  cookie(name, '', options)
end


34
35
36
37
38
# File 'lib/expressr/response.rb', line 34

def cookie(name, value, options={})
  options = { 'name' => name, 'value' => value }.merge(options)
  @cookies[name] = CGI::Cookie::new(options)
  set_cookies
end

#download(path, filename = nil) ⇒ Object



149
150
151
152
# File 'lib/expressr/response.rb', line 149

def download(path, filename=nil)
  attachment(filename || path)
  send_file(path, bypass_headers: true)
end

#format(hash) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/expressr/response.rb', line 116

def format(hash)
  hash.each do |content_type, proc|
    content_type = Utils.standardize_content_type(content_type)
    if request.accepts(content_type)
      proc.call(@request, self)
    end
  end
end

#get(key) ⇒ Object



30
31
32
# File 'lib/expressr/response.rb', line 30

def get(key)
  get_header(key)
end

#json(status_or_body, body = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/expressr/response.rb', line 78

def json(status_or_body, body=nil)
  if status_or_body.is_a?(Integer)
    status = status_or_body
  else
    status = DEFAULT_STATUS
    body = status_or_body
  end
  type('application/json')
  write_head(status)
  write(Expressr::JSON.encode(body))
  self.end
end

#jsonp(status_or_body, body = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/expressr/response.rb', line 91

def jsonp(status_or_body, body=nil)
  callback_name = Expressr::App.settings['jsonp callback name']
  callback = params[callback_name]
  if callback.nil?
    json(status_or_body, body)
    return
  end
  if status_or_body.is_a?(Integer)
    status = status_or_body
  else
    status = DEFAULT_STATUS
    body = status_or_body
  end
  type('application/javascript')
  write_head(status)
  body = Expressr::JSON.encode(body)
  body = "#{callback}(#{body});"
  write(body)
  self.end
end


154
155
156
157
# File 'lib/expressr/response.rb', line 154

def links(links)
  value = links.map { |key, url| %Q|<#{url}>; rel="#{key}"| }.join(', ')
  set_header('Link', value)
end

#location(url) ⇒ Object



56
57
58
# File 'lib/expressr/response.rb', line 56

def location(url)
  set_header('Location', url)
end

#out(status_or_content = nil, content = nil) ⇒ Object

Equivalent of Express.js’s send



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/expressr/response.rb', line 61

def out(status_or_content=nil, content=nil)
  if status_or_content.is_a?(Integer)
    status = status_or_content
  else
    status = DEFAULT_STATUS
    content = status_or_content || ''
  end
  if content.is_a?(Hash) || content.is_a?(Array)
    json(status, content)
  else
    type('text/html') unless get('Content-Type')
    write_head(status)
    write(content)
    self.end
  end
end

#redirect(status_or_url, url = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/expressr/response.rb', line 44

def redirect(status_or_url, url=nil)
  if url
    status = status_or_url
  else
    status = 302
    url = status_or_url
  end
  write_head(status)
  location(url)
  self.end
end

#render(view, locals = nil, &block) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/expressr/response.rb', line 163

def render(view, locals=nil, &block)
  body = nil
  begin
    body = Renderer.new.render(view, locals)
  rescue Exception => e
    if block
      block.call(e)
    else
      raise
    end
  end
  out(body)
end

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



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/expressr/response.rb', line 136

def send_file(path, options={})
  root = nil
  if options[:root] && !path.start_with?('/')
    root = Pathname.new(options[:root])
  elsif App.settings['root']
    root = App.settings['root']
  end
  path = root.join(path).to_s if root
  attachment(path) unless options[:bypass_headers]
  write(read_file(path))
  self.end
end

#set(key_or_hash, value = nil) ⇒ Object

Defining #status(status) conflicts with EventMachine::DelegatedHttpResponse’s #status



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/expressr/response.rb', line 18

def set(key_or_hash, value=nil)
  if key_or_hash.is_a?(Hash)
    hash = key_or_hash
    hash.each do |key, value|
      set_header(key, value)
    end
  else
    key = key_or_hash
    set_header(key, value)
  end
end

#type(value) ⇒ Object



112
113
114
# File 'lib/expressr/response.rb', line 112

def type(value)
  set_header('Content-Type', value)
end