Module: PostHog::Utils Private

Included in:
Client, FeatureFlagsPoller, FieldParser, SendWorker, Transport
Defined in:
lib/posthog/utils.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Utility helpers used internally by the SDK.

Defined Under Namespace

Classes: SizeLimitedHash

Constant Summary collapse

UTC_OFFSET_WITH_COLON =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

'%s%02d:%02d'
UTC_OFFSET_WITHOUT_COLON =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

UTC_OFFSET_WITH_COLON.sub(':', '')

Class Method Summary collapse

Class Method Details

.convert_to_datetime(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/posthog/utils.rb', line 107

def convert_to_datetime(value)
  if value.respond_to?(:strftime)
    value

  elsif value.respond_to?(:to_str)
    begin
      DateTime.parse(value)
    rescue ArgumentError
      raise InconclusiveMatchError, "#{value} is not in a valid date format"
    end
  else
    raise InconclusiveMatchError, 'The date provided must be a string or date object'
  end
end

.date_in_iso8601(date) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



93
94
95
# File 'lib/posthog/utils.rb', line 93

def date_in_iso8601(date)
  date.strftime('%F')
end

.datetime_in_iso8601(datetime) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/posthog/utils.rb', line 73

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

.deep_symbolize_keys(obj) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

public: Recursively convert all keys to symbols in a Hash/Array tree



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/posthog/utils.rb', line 35

def deep_symbolize_keys(obj)
  case obj
  when Hash
    obj.each_with_object({}) do |(key, value), result|
      result[key.to_sym] = deep_symbolize_keys(value)
    end
  when Array
    obj.map { |item| deep_symbolize_keys(item) }
  else
    obj
  end
end

.formatted_offset(time, colon = true, alternate_utc_string = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



97
98
99
100
# File 'lib/posthog/utils.rb', line 97

def formatted_offset(time, colon = true, alternate_utc_string = nil)
  (time.utc? && alternate_utc_string) ||
    seconds_to_utc_offset(time.utc_offset, colon)
end

.get_by_symbol_or_string_key(hash, key) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

public: Get value from hash by symbol key first, then string key

Handles falsy values correctly (unlike ||)

hash - Hash to lookup value in key - Symbol or String key to lookup

Returns the value if found, nil otherwise



140
141
142
143
144
145
146
147
148
149
# File 'lib/posthog/utils.rb', line 140

def get_by_symbol_or_string_key(hash, key)
  symbol_key = key.to_sym
  string_key = key.to_s

  if hash.key?(symbol_key)
    hash[symbol_key]
  else
    hash[string_key]
  end
end

.is_valid_regex(regex) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO: Rename to ‘valid_regex?` in future version



126
127
128
129
130
131
# File 'lib/posthog/utils.rb', line 126

def is_valid_regex(regex) # rubocop:disable Naming/PredicateName
  Regexp.new(regex)
  true
rescue RegexpError
  false
end

.isoify_dates(hash) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

strings


51
52
53
54
55
# File 'lib/posthog/utils.rb', line 51

def isoify_dates(hash)
  hash.transform_values do |v|
    datetime_in_iso8601(v)
  end
end

.isoify_dates!(hash) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



59
60
61
# File 'lib/posthog/utils.rb', line 59

def isoify_dates!(hash)
  hash.replace isoify_dates hash
end

.seconds_to_utc_offset(seconds, colon = true) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



102
103
104
105
# File 'lib/posthog/utils.rb', line 102

def seconds_to_utc_offset(seconds, colon = true)
  format((colon ? UTC_OFFSET_WITH_COLON : UTC_OFFSET_WITHOUT_COLON), (seconds.negative? ? '-' : '+'),
         seconds.abs / 3600, (seconds.abs % 3600) / 60)
end

.stringify_keys(hash) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

public: Return a new hash with keys as strings



29
30
31
# File 'lib/posthog/utils.rb', line 29

def stringify_keys(hash)
  hash.transform_keys(&:to_s)
end

.symbolize_keys(hash) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

public: Return a new hash with keys converted from strings to symbols



17
18
19
# File 'lib/posthog/utils.rb', line 17

def symbolize_keys(hash)
  hash.transform_keys(&:to_sym)
end

.symbolize_keys!(hash) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

public: Convert hash keys from strings to symbols in place



23
24
25
# File 'lib/posthog/utils.rb', line 23

def symbolize_keys!(hash)
  hash.replace symbolize_keys hash
end

.time_in_iso8601(time, fraction_digits = 3) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



86
87
88
89
90
91
# File 'lib/posthog/utils.rb', line 86

def time_in_iso8601(time, fraction_digits = 3)
  fraction =
    (('.%06i' % time.usec)[0, fraction_digits + 1] if fraction_digits.positive?) # rubocop:disable Style/FormatString

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

.uidObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

public: Returns a uid string



65
66
67
68
69
70
71
# File 'lib/posthog/utils.rb', line 65

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/FormatStringToken, Style/FormatString
end