Class: GoogleMaps::Services::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/googlemaps/services/util.rb

Overview

Set of utility methods.

Class Method Summary collapse

Class Method Details

.current_timeTime

Returns the current time

Rails extends the Time and DateTime objects, and includes the “current” property for retrieving the time the Rails environment is set to (default = UTC), as opposed to the server time (Could be anything).

Returns:

  • (Time)

    a new Time object for the current time.



71
72
73
# File 'lib/googlemaps/services/util.rb', line 71

def self.current_time
  (Time.respond_to? :current) ? Time.current : Time.now
end

.current_unix_timeInteger

Returns the current time in unix format (seconds since unix epoch).

Returns:

  • (Integer)

    number of seconds since unix epoch.



85
86
87
# File 'lib/googlemaps/services/util.rb', line 85

def self.current_unix_time
  current_time.to_i
end

.current_utctimeTime

Returns the current UTC time.

Returns:

  • (Time)

    a new Time object for the current UTC (GMT) time.



78
79
80
# File 'lib/googlemaps/services/util.rb', line 78

def self.current_utctime
  (Time.respond_to? :current) ? Time.current.utc : Time.now.utc
end

.sign_hmac(secret, payload) ⇒ String

Returns a base64-encoded HMAC-SHA1 signature of a given string.

Parameters:

  • secret (String)

    The key used for the signature, base64 encoded.

  • payload (String)

    The payload to sign.

Returns:

  • (String)

    a base64-encoded signature string.



95
96
97
98
99
100
101
# File 'lib/googlemaps/services/util.rb', line 95

def self.sign_hmac(secret, payload)
  payload = payload.encode('ascii')
  secret = secret.encode('ascii')
  digest = OpenSSL::Digest.new('sha1')
  sig = OpenSSL::HMAC.digest(digest, Base64.urlsafe_decode64(secret), payload)
  return Base64.urlsafe_encode64(sig).encode('utf-8')
end

.urlencode_params(params) ⇒ String

URL encodes the parameters.

Parameters:

  • params (Hash)

    The parameters.

Returns:

  • (String)

    URL-encoded string.



108
109
110
# File 'lib/googlemaps/services/util.rb', line 108

def self.urlencode_params(params)
  URI.encode_www_form(params)
end