Module: Otto::RequestHelpers

Defined in:
lib/otto.rb

Instance Method Summary collapse

Instance Method Details

#absolute_suri(host = current_server_name) ⇒ Object



346
347
348
349
# File 'lib/otto.rb', line 346

def absolute_suri host=current_server_name
  prefix = local? ? 'http://' : 'https://'
  [prefix, host, request_path].join
end

#client_ipaddressObject

HTTP_X_FORWARDED_FOR is from the ELB (non-https only) and it can take the form: 74.121.244.2, 10.252.130.147 HTTP_X_REAL_IP is from nginx REMOTE_ADDR is from thin There’s no way to get the client IP address in HTTPS.



314
315
316
317
# File 'lib/otto.rb', line 314

def client_ipaddress
  env['HTTP_X_FORWARDED_FOR'].to_s.split(/,\s*/).first ||
  env['HTTP_X_REAL_IP'] || env['REMOTE_ADDR']
end


361
362
363
# File 'lib/otto.rb', line 361

def cookie name
  cookies[name.to_s]
end

#cookie?(name) ⇒ Boolean

Returns:

  • (Boolean)


365
366
367
# File 'lib/otto.rb', line 365

def cookie? name
  !cookie(name).to_s.empty?
end

#current_absolute_uriObject



369
370
371
372
# File 'lib/otto.rb', line 369

def current_absolute_uri
  prefix = secure? && !local? ? 'https://' : 'http://'
  [prefix, http_host, request_path].join
end

#current_serverObject



323
324
325
# File 'lib/otto.rb', line 323

def current_server
  [current_server_name, env['SERVER_PORT']].join(':')
end

#current_server_nameObject



327
328
329
# File 'lib/otto.rb', line 327

def current_server_name
  env['SERVER_NAME']
end

#http_hostObject



331
332
333
# File 'lib/otto.rb', line 331

def http_host
  env['HTTP_HOST']
end

#local?Boolean

Returns:

  • (Boolean)


351
352
353
# File 'lib/otto.rb', line 351

def local?
  Otto.env?(:dev, :development) && client_ipaddress == '127.0.0.1' 
end

#request_methodObject



319
320
321
# File 'lib/otto.rb', line 319

def request_method
  env['REQUEST_METHOD']
end

#request_pathObject



334
335
336
# File 'lib/otto.rb', line 334

def request_path
  env['REQUEST_PATH']
end

#request_uriObject



338
339
340
# File 'lib/otto.rb', line 338

def request_uri
  env['REQUEST_URI']
end

#root_pathObject



342
343
344
# File 'lib/otto.rb', line 342

def root_path
  env['SCRIPT_NAME']
end

#secure?Boolean

Returns:

  • (Boolean)


355
356
357
358
359
# File 'lib/otto.rb', line 355

def secure?
  # X-Scheme is set by nginx
  # X-FORWARDED-PROTO is set by elastic load balancer
  (env['HTTP_X_FORWARDED_PROTO'] == 'https' || env['HTTP_X_SCHEME'] == "https")
end

#user_agentObject



305
306
307
# File 'lib/otto.rb', line 305

def user_agent
  env['HTTP_USER_AGENT'] || '[no-user-agent]'
end