Class: Lux::Response

Inherits:
Object show all
Defined in:
lib/lux/response/lib/file.rb,
lib/lux/response/response.rb,
lib/lux/response/lib/flash.rb,
lib/lux/response/lib/header.rb

Defined Under Namespace

Classes: File, Flash, Header

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.



13
14
15
16
17
# File 'lib/lux/response/response.rb', line 13

def initialize
  @render_start = Time.monotonic
  @headers      = Lux::Response::Header.new
  @max_age      = 0
end

Instance Attribute Details

#content_type(in_type = nil) ⇒ Object

Returns the value of attribute content_type.



11
12
13
# File 'lib/lux/response/response.rb', line 11

def content_type
  @content_type
end

#cookiesObject

Returns the value of attribute cookies.



11
12
13
# File 'lib/lux/response/response.rb', line 11

def cookies
  @cookies
end

#headersObject

Returns the value of attribute headers.



11
12
13
# File 'lib/lux/response/response.rb', line 11

def headers
  @headers
end

#max_ageObject

define in seconds, how long should page be accessible in client cache if defined, cache becomes public and can be cache by proxies use with care.only for static resources and



10
11
12
# File 'lib/lux/response/response.rb', line 10

def max_age
  @max_age
end

#render_startObject (readonly)

define in seconds, how long should page be accessible in client cache if defined, cache becomes public and can be cache by proxies use with care.only for static resources and



10
11
12
# File 'lib/lux/response/response.rb', line 10

def render_start
  @render_start
end

#status(num = nil) ⇒ Object Also known as: status=

Returns the value of attribute status.



11
12
13
# File 'lib/lux/response/response.rb', line 11

def status
  @status
end

Instance Method Details

#auth(realm: nil, message: nil) ⇒ Object

auth { |user, pass| [user, pass] == [‘foo’, ‘bar’] }



203
204
205
206
207
208
209
210
211
# File 'lib/lux/response/response.rb', line 203

def auth realm: nil, message: nil
  if auth = current.request.env['HTTP_AUTHORIZATION']
    credentials = auth.to_s.split('Basic ', 2)[1].unpack("m*").first.split(':', 2)
    return true if yield *credentials
  end

  header('WWW-Authenticate', 'Basic realm="%s"' % realm.or('default'))
  body message || 'HTTP 401 Authorization needed', status: 401
end

#body(data = nil, opts = nil) ⇒ Object Also known as: body=

response.body ‘foo’ response.body ‘foo’, status: 400, content_type: :js response.body { ‘foo’ } response.body(…) { ‘foo’ }



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

def body data = nil, opts = nil
  if block_given?
    # block can override data set
    opts = data || {}
    @body = yield @body
    @body
  elsif data
    unless @body
      opts ||= {}
      opts.is!(Hash).each {|k,v| self.send k, *v }
      @body = data
    end
    throw :done
  else
    @body
  end
end

#body?Boolean

Returns:



113
114
115
# File 'lib/lux/response/response.rb', line 113

def body?
  !!@body
end

#cached?Boolean

Returns:



243
244
245
# File 'lib/lux/response/response.rb', line 243

def cached?
  @max_age > 0
end

#currentObject



19
20
21
# File 'lib/lux/response/response.rb', line 19

def current
  Lux.current
end

#early_hints(link = nil, type = nil) ⇒ Object

http 103



46
47
48
49
50
# File 'lib/lux/response/response.rb', line 46

def early_hints link=nil, type=nil
  @early_hints ||= []
  @early_hints.push [link, type] if type && !@early_hints.include?(link)
  @early_hints
end

#etag(*args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lux/response/response.rb', line 52

def etag *args
  unless @headers['etag']
    key = '"%s"' % Lux.cache.generate_key(current.request.url, args)
    key = 'W/%s' % key unless max_age > 0
    @headers['etag'] = key
  end

  if !status && !current.no_cache?(true) && current.request.env['HTTP_IF_NONE_MATCH'] == @headers['etag']
    if Lux.env.no_cache?
      Lux.log ' * etag match (skiping for env.no_cache)' unless current.nav.format
    else
      Lux.log ' * etag match'
      body 'not-modified', status: 304
      true
    end
  else
    false
  end
end

#flash(message = nil) ⇒ Object



136
137
138
139
# File 'lib/lux/response/response.rb', line 136

def flash message = nil
  @flash ||= Flash.new current.session[:lux_flash]
  message ? @flash.error(message) : @flash
end

#halt(status = nil, msg = nil) ⇒ Object



85
86
87
88
# File 'lib/lux/response/response.rb', line 85

def halt status = nil, msg = nil
  @status = status || 400
  @body   = msg if msg
end

#header(*args) ⇒ Object

header header ‘x-foo’, ‘bar’ header @hash



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lux/response/response.rb', line 26

def header *args
  if args.first
    if args.first.class == Hash
      args.each{|k,v| header k, v.to_s if k && v }
    else
      key = args.first.to_s.downcase
      @headers[key] = args[1].to_s if args[1] != :_
      @headers[key]
    end
  end

  @headers
end

#is_bot?Boolean

Returns:



247
248
249
# File 'lib/lux/response/response.rb', line 247

def is_bot?
  current.request.user_agent.to_s.include?('Googlebot')
end

#permanent_redirect_to(where) ⇒ Object



198
199
200
# File 'lib/lux/response/response.rb', line 198

def permanent_redirect_to where
  redirect_to where, status: 301
end

#public?Boolean

Returns:



239
240
241
# File 'lib/lux/response/response.rb', line 239

def public?
  @headers['cache-control'].to_s.include?('public')
end

#rack(klass) ⇒ Object



233
234
235
236
237
# File 'lib/lux/response/response.rb', line 233

def rack klass
  data = klass.call current.env
  @headers.merge data[1]
  body data[2].first, status:data[0]
end

#redirect_to(where, opts = {}) ⇒ Object

redirect_to ‘/foo’ redirect_to :back, info: ‘bar …’



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/lux/response/response.rb', line 147

def redirect_to where, opts = {}
  Lux.log { ' Redirected to "%s" from: %s' % [where, Lux.app_caller] }

  opts = { info: opts } if opts.is_a?(String)

  if where == :self
    where = current.request.path
  elsif where == :back
    where  = current.request.env['HTTP_REFERER'].or('/')
  elsif where[0,1] == '?'
    where  = "#{current.request.path}#{where}"
  # elsif !where.include?('://')
  #   where = current.host + where
  end

  if where.start_with?('/') || opts.delete(:redirect_tracker)
    redirect_var = Lux.config[:redirect_var] || :_r
    url = Url.new where
    url[redirect_var] = current.request.params[redirect_var].to_i + 1

    where = if opts.delete(:silent)
      url.delete redirect_var
      url.to_s
    else
      url[redirect_var].to_i > 3 ? '/' : url.to_s
    end
  end

  @status = opts.delete(:status) || 302
  opts.map { |k,v| flash.send(k, v) }

  @body = "    <html>\n      <head>\n        <title>redirecting</title>\n      </head>\n      <body>\n        <p>redirecting to \#{where}</p>\n        <p>\#{opts.values.join(\"\\n\")}</p>\n        <script>location.href = '\#{where}'</script>\n      </body>\n    </html>\n  PAGE\n\n  @headers['location'] = where\n  @headers['access-control-expose-headers'] ||= 'Location'\n\n  # if we do not have this here, controller will proceed executing code after redirect_to\n  throw :done\nend\n"

#renderObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/lux/response/response.rb', line 213

def render
  write_response_body
  write_response_header

  @status ||= 200

  Lux.log do
    log_data  = " #{@status}, #{@data.to_s.length}, #{(@body.bytesize.to_f/1024).round(1)}kb, #{@headers['x-lux-speed']}"
    log_data += " (#{current.request.url})" if current.nav.format

    [200, 304].include?(@status) ? log_data : log_data.magenta
  end

  if current.request.request_method == 'HEAD'
    @body = ''
  end

  [@status, @headers.to_h, [@body]]
end

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



141
142
143
# File 'lib/lux/response/response.rb', line 141

def send_file file, opts = {}
  ::Lux::Response::File.new(opts.merge(file: file)).send
end