Module: Otto::RequestHelpers

Defined in:
lib/otto/helpers/request.rb

Instance Method Summary collapse

Instance Method Details

#absolute_suri(host = current_server_name) ⇒ Object



31
32
33
34
# File 'lib/otto/helpers/request.rb', line 31

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

#ajax?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/otto/helpers/request.rb', line 47

def ajax?
  env['HTTP_X_REQUESTED_WITH'].to_s.downcase == 'xmlhttprequest'
end

#client_ipaddressObject



6
7
8
9
# File 'lib/otto/helpers/request.rb', line 6

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


50
51
52
# File 'lib/otto/helpers/request.rb', line 50

def cookie name
  cookies[name.to_s]
end

#cookie?(name) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/otto/helpers/request.rb', line 53

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

#current_absolute_uriObject



56
57
58
59
# File 'lib/otto/helpers/request.rb', line 56

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

#current_serverObject



13
14
15
# File 'lib/otto/helpers/request.rb', line 13

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

#current_server_nameObject



16
17
18
# File 'lib/otto/helpers/request.rb', line 16

def current_server_name
  env['SERVER_NAME']
end

#http_hostObject



19
20
21
# File 'lib/otto/helpers/request.rb', line 19

def http_host
  env['HTTP_HOST']
end

#local?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/otto/helpers/request.rb', line 35

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



10
11
12
# File 'lib/otto/helpers/request.rb', line 10

def request_method
  env['REQUEST_METHOD']
end

#request_pathObject



22
23
24
# File 'lib/otto/helpers/request.rb', line 22

def request_path
  env['REQUEST_PATH']
end

#request_uriObject



25
26
27
# File 'lib/otto/helpers/request.rb', line 25

def request_uri
  env['REQUEST_URI']
end

#root_pathObject



28
29
30
# File 'lib/otto/helpers/request.rb', line 28

def root_path
  env['SCRIPT_NAME']
end

#secure?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/otto/helpers/request.rb', line 41

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



3
4
5
# File 'lib/otto/helpers/request.rb', line 3

def user_agent
  env['HTTP_USER_AGENT']
end