module RudderAnalyticsSync::Utils

Constants

UTC_OFFSET_WITHOUT_COLON
UTC_OFFSET_WITH_COLON

Public Class Methods

included(klass) click to toggle source
# File lib/rudder_analytics_sync/utils.rb, line 7
def self.included(klass)
  klass.extend(self)
end

Public Instance Methods

date_in_iso8601(date) click to toggle source
# File lib/rudder_analytics_sync/utils.rb, line 60
def date_in_iso8601(date)
  date.strftime('%F')
end
formatted_offset(time, colon = true, alternate_utc_string = nil) click to toggle source
# File lib/rudder_analytics_sync/utils.rb, line 64
def formatted_offset(time, colon = true, alternate_utc_string = nil)
  time.utc? && alternate_utc_string || seconds_to_utc_offset(time.utc_offset, colon)
end
isoify_dates(hash) click to toggle source

public: Returns a new hash with all the date values in the into iso8601

strings
# File lib/rudder_analytics_sync/utils.rb, line 26
def isoify_dates(hash)
  hash.each_with_object({}) do |(k, v), memo|
    memo[k] = maybe_datetime_in_iso8601(v)
  end
end
isoify_dates!(hash) click to toggle source

public: Converts all the date values in the into iso8601 strings in place

# File lib/rudder_analytics_sync/utils.rb, line 19
def isoify_dates!(hash)
  hash.replace isoify_dates hash
end
maybe_datetime_in_iso8601(datetime) click to toggle source
# File lib/rudder_analytics_sync/utils.rb, line 41
def maybe_datetime_in_iso8601(datetime)
  case datetime
    when Time
      time_in_iso8601 datetime
    when DateTime
      time_in_iso8601 datetime.to_time
    when Date
      date_in_iso8601 datetime
    else
      datetime
  end
end
seconds_to_utc_offset(seconds, colon = true) click to toggle source
# File lib/rudder_analytics_sync/utils.rb, line 68
def seconds_to_utc_offset(seconds, colon = true)
  (colon ? UTC_OFFSET_WITH_COLON : UTC_OFFSET_WITHOUT_COLON) % [(seconds < 0 ? '-' : '+'), (seconds.abs / 3600), ((seconds.abs % 3600) / 60)]
end
symbolize_keys(hash) click to toggle source
# File lib/rudder_analytics_sync/utils.rb, line 11
def symbolize_keys(hash)
  hash.each_with_object({}) do |(key, value), result|
    result[key.to_sym] = value
  end
end
time_in_iso8601(time, fraction_digits = 3) click to toggle source
# File lib/rudder_analytics_sync/utils.rb, line 54
def time_in_iso8601(time, fraction_digits = 3)
  fraction = (('.%06i' % time.usec)[0, fraction_digits + 1] if fraction_digits > 0)

  "#{time.strftime('%Y-%m-%dT%H:%M:%S')}#{fraction}#{formatted_offset(time, true, 'Z')}"
end
uid() click to toggle source

public: Returns a uid string

# File lib/rudder_analytics_sync/utils.rb, line 34
def uid
    arr = SecureRandom.random_bytes(16).unpack('NnnnnN')
    arr[2] = (arr[2] & 0x0fff) | 0x4000
    arr[3] = (arr[3] & 0x3fff) | 0x8000
    '%08x-%04x-%04x-%04x-%04x%08x' % arr
end