Module: Otto::RequestHelpers

Defined in:
lib/otto.rb

Instance Method Summary collapse

Instance Method Details

#absolute_suri(host = current_server_name) ⇒ Object



377
378
379
380
# File 'lib/otto.rb', line 377

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

#ajax?Boolean

Returns:

  • (Boolean)


393
394
395
# File 'lib/otto.rb', line 393

def ajax?
  env['HTTP_X_REQUESTED_WITH'].to_s.downcase == 'xmlhttprequest'
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.



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

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


396
397
398
# File 'lib/otto.rb', line 396

def cookie name
  cookies[name.to_s]
end

#cookie?(name) ⇒ Boolean

Returns:

  • (Boolean)


399
400
401
# File 'lib/otto.rb', line 399

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

#current_absolute_uriObject



402
403
404
405
# File 'lib/otto.rb', line 402

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

#current_serverObject



359
360
361
# File 'lib/otto.rb', line 359

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

#current_server_nameObject



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

def current_server_name
  env['SERVER_NAME']
end

#http_hostObject



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

def http_host
  env['HTTP_HOST']
end

#local?Boolean

Returns:

  • (Boolean)


381
382
383
384
385
386
# File 'lib/otto.rb', line 381

def local?
  Otto.env?(:dev, :development) &&
   (client_ipaddress == '127.0.0.1' ||
   !client_ipaddress.match(/^10\.0\./).nil? ||
   !client_ipaddress.match(/^192\.168\./).nil?)
end

#request_methodObject



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

def request_method
  env['REQUEST_METHOD']
end

#request_pathObject



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

def request_path
  env['REQUEST_PATH']
end

#request_uriObject



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

def request_uri
  env['REQUEST_URI']
end

#root_pathObject



374
375
376
# File 'lib/otto.rb', line 374

def root_path
  env['SCRIPT_NAME']
end

#secure?Boolean

Returns:

  • (Boolean)


387
388
389
390
391
# File 'lib/otto.rb', line 387

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



344
345
346
# File 'lib/otto.rb', line 344

def user_agent
  env['HTTP_USER_AGENT']
end