Module: OmniContacts::HTTPUtils

Included in:
Authorization::OAuth1, Authorization::OAuth2
Defined in:
lib/omnicontacts/http_utils.rb

Constant Summary collapse

SSL_PORT =
443

Class Method Summary collapse

Class Method Details

.encode(to_encode) ⇒ Object

Encodes the given input according to RFC 3986



29
30
31
# File 'lib/omnicontacts/http_utils.rb', line 29

def encode to_encode
  CGI.escape(to_encode)
end

.host_url_from_rack_env(env) ⇒ Object

Calculates the url of the host from a Rack environment. The result is in the form scheme://host:port If port is 80 the result is scheme://host According to Rack specification the HTTP_HOST variable is preferred over SERVER_NAME.



37
38
39
40
41
# File 'lib/omnicontacts/http_utils.rb', line 37

def host_url_from_rack_env env
  port = ((env["SERVER_PORT"] == 80) && "") || ":#{env['SERVER_PORT']}"
  host = (env["HTTP_HOST"]) || (env["SERVER_NAME"] + port)
  "#{scheme(env)}://#{host}"
end

.query_string_to_map(query_string) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/omnicontacts/http_utils.rb', line 14

def query_string_to_map query_string
  query_string.split('&').reduce({}) do |memo, key_value|
    (key, value) = key_value.split('=')
    memo[key]= value
    memo
  end
end

.scheme(env) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/omnicontacts/http_utils.rb', line 43

def scheme env
  if env['HTTPS'] == 'on'
    'https'
  elsif env['HTTP_X_FORWARDED_SSL'] == 'on'
    'https'
  elsif env['HTTP_X_FORWARDED_PROTO']
    env['HTTP_X_FORWARDED_PROTO'].split(',').first
  else
    env["rack.url_scheme"]
  end
end

.to_query_string(map) ⇒ Object



22
23
24
25
26
# File 'lib/omnicontacts/http_utils.rb', line 22

def to_query_string map
  map.collect do |key, value|
    key.to_s + "=" + value.to_s
  end.join("&")
end