Module: Segment::Analytics::Utils
- Extended by:
- Utils
- Included in:
- Client, FieldParser, Transport, Utils, Worker
- Defined in:
- lib/segment/analytics/utils.rb
Constant Summary collapse
- UTC_OFFSET_WITH_COLON =
'%s%02d:%02d'
- UTC_OFFSET_WITHOUT_COLON =
UTC_OFFSET_WITH_COLON.sub(':', '')
Instance Method Summary collapse
- #date_in_iso8601(date) ⇒ Object
- #datetime_in_iso8601(datetime) ⇒ Object
- #formatted_offset(time, colon = true, alternate_utc_string = nil) ⇒ Object
-
#isoify_dates(hash) ⇒ Object
public: Returns a new hash with all the date values in the into iso8601 strings.
-
#isoify_dates!(hash) ⇒ Object
public: Converts all the date values in the into iso8601 strings in place.
- #seconds_to_utc_offset(seconds, colon = true) ⇒ Object
-
#stringify_keys(hash) ⇒ Object
public: Return a new hash with keys as strings.
-
#symbolize_keys(hash) ⇒ Object
public: Return a new hash with keys converted from strings to symbols.
-
#symbolize_keys!(hash) ⇒ Object
public: Convert hash keys from strings to symbols in place.
- #time_in_iso8601(time) ⇒ Object
-
#uid ⇒ Object
public: Returns a uid string.
Instance Method Details
#date_in_iso8601(date) ⇒ Object
73 74 75 |
# File 'lib/segment/analytics/utils.rb', line 73 def date_in_iso8601(date) date.strftime('%F') end |
#datetime_in_iso8601(datetime) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/segment/analytics/utils.rb', line 56 def 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 |
#formatted_offset(time, colon = true, alternate_utc_string = nil) ⇒ Object
77 78 79 |
# File 'lib/segment/analytics/utils.rb', line 77 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) ⇒ Object
public: Returns a new hash with all the date values in the into iso8601
strings
35 36 37 38 39 |
# File 'lib/segment/analytics/utils.rb', line 35 def isoify_dates(hash) hash.each_with_object({}) do |(k, v), memo| memo[k] = datetime_in_iso8601(v) end end |
#isoify_dates!(hash) ⇒ Object
public: Converts all the date values in the into iso8601 strings in place
43 44 45 |
# File 'lib/segment/analytics/utils.rb', line 43 def isoify_dates!(hash) hash.replace isoify_dates hash end |
#seconds_to_utc_offset(seconds, colon = true) ⇒ Object
81 82 83 |
# File 'lib/segment/analytics/utils.rb', line 81 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 |
#stringify_keys(hash) ⇒ Object
public: Return a new hash with keys as strings
26 27 28 29 30 |
# File 'lib/segment/analytics/utils.rb', line 26 def stringify_keys(hash) hash.each_with_object({}) do |(k, v), memo| memo[k.to_s] = v end end |
#symbolize_keys(hash) ⇒ Object
public: Return a new hash with keys converted from strings to symbols
12 13 14 15 16 |
# File 'lib/segment/analytics/utils.rb', line 12 def symbolize_keys(hash) hash.each_with_object({}) do |(k, v), memo| memo[k.to_sym] = v end end |
#symbolize_keys!(hash) ⇒ Object
public: Convert hash keys from strings to symbols in place
20 21 22 |
# File 'lib/segment/analytics/utils.rb', line 20 def symbolize_keys!(hash) hash.replace symbolize_keys hash end |
#time_in_iso8601(time) ⇒ Object
69 70 71 |
# File 'lib/segment/analytics/utils.rb', line 69 def time_in_iso8601(time) "#{time.strftime('%Y-%m-%dT%H:%M:%S.%3N')}#{formatted_offset(time, true, 'Z')}" end |
#uid ⇒ Object
public: Returns a uid string
49 50 51 52 53 54 |
# File 'lib/segment/analytics/utils.rb', line 49 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 |