Module: RudderAnalyticsSync::Utils
- Included in:
- Batch, Client, Configuration, Operations::Operation
- Defined in:
- lib/rudder_analytics_sync/utils.rb
Constant Summary collapse
- UTC_OFFSET_WITH_COLON =
'%s%02d:%02d'- UTC_OFFSET_WITHOUT_COLON =
UTC_OFFSET_WITH_COLON.sub(':', '')
Class Method Summary collapse
Instance Method Summary collapse
- #date_in_iso8601(date) ⇒ Object
-
#formatted_offset(time, colon = true, alternate_utc_string = nil) ⇒ Object
rubocop:disable Style/OptionalBooleanParameter.
-
#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.
- #maybe_datetime_in_iso8601(datetime) ⇒ Object
-
#seconds_to_utc_offset(seconds, colon = true) ⇒ Object
rubocop:disable Style/OptionalBooleanParameter.
- #symbolize_keys(hash) ⇒ Object
- #time_in_iso8601(time, fraction_digits = 3) ⇒ Object
-
#uid ⇒ Object
public: Returns a uid string.
- #valid_date?(string) ⇒ Boolean
Class Method Details
.included(klass) ⇒ Object
7 8 9 |
# File 'lib/rudder_analytics_sync/utils.rb', line 7 def self.included(klass) klass.extend(self) end |
Instance Method Details
#date_in_iso8601(date) ⇒ Object
58 59 60 |
# File 'lib/rudder_analytics_sync/utils.rb', line 58 def date_in_iso8601(date) date.strftime('%F') end |
#formatted_offset(time, colon = true, alternate_utc_string = nil) ⇒ Object
rubocop:disable Style/OptionalBooleanParameter
62 63 64 |
# File 'lib/rudder_analytics_sync/utils.rb', line 62 def formatted_offset(time, colon = true, alternate_utc_string = nil) # rubocop:disable Style/OptionalBooleanParameter (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
24 25 26 27 28 |
# File 'lib/rudder_analytics_sync/utils.rb', line 24 def isoify_dates(hash) hash.transform_values do |v| maybe_datetime_in_iso8601(v) end end |
#isoify_dates!(hash) ⇒ Object
public: Converts all the date values in the into iso8601 strings in place
17 18 19 |
# File 'lib/rudder_analytics_sync/utils.rb', line 17 def isoify_dates!(hash) hash.replace isoify_dates hash end |
#maybe_datetime_in_iso8601(datetime) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rudder_analytics_sync/utils.rb', line 39 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) ⇒ Object
rubocop:disable Style/OptionalBooleanParameter
66 67 68 |
# File 'lib/rudder_analytics_sync/utils.rb', line 66 def seconds_to_utc_offset(seconds, colon = true) # rubocop:disable Style/OptionalBooleanParameter format((colon ? UTC_OFFSET_WITH_COLON : UTC_OFFSET_WITHOUT_COLON), (seconds.negative? ? '-' : '+'), (seconds.abs / 3600), ((seconds.abs % 3600) / 60)) end |
#symbolize_keys(hash) ⇒ Object
11 12 13 |
# File 'lib/rudder_analytics_sync/utils.rb', line 11 def symbolize_keys(hash) hash.transform_keys(&:to_sym) end |
#time_in_iso8601(time, fraction_digits = 3) ⇒ Object
52 53 54 55 56 |
# File 'lib/rudder_analytics_sync/utils.rb', line 52 def time_in_iso8601(time, fraction_digits = 3) fraction = (format('.%06i', time.usec)[0, fraction_digits + 1] if fraction_digits.positive?) "#{time.strftime('%Y-%m-%dT%H:%M:%S')}#{fraction}#{formatted_offset(time, true, 'Z')}" end |
#uid ⇒ Object
public: Returns a uid string
32 33 34 35 36 37 |
# File 'lib/rudder_analytics_sync/utils.rb', line 32 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 # rubocop:disable Style/FormatString end |
#valid_date?(string) ⇒ Boolean
70 71 72 73 74 |
# File 'lib/rudder_analytics_sync/utils.rb', line 70 def valid_date?(string) !!(string.match(/\d{4}-\d{2}-\d{2}/) && Date.strptime(string, '%Y-%m-%d')) rescue ArgumentError false end |