Module: PostHog::Utils

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

Defined Under Namespace

Classes: SizeLimitedHash

Constant Summary collapse

UTC_OFFSET_WITH_COLON =
'%s%02d:%02d'
UTC_OFFSET_WITHOUT_COLON =
UTC_OFFSET_WITH_COLON.sub(':', '')

Instance Method Summary collapse

Instance Method Details

#convert_to_datetime(value) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/posthog/utils.rb', line 90

def convert_to_datetime(value)
  if value.respond_to?(:strftime)
    parsed_date = value
    return parsed_date
  elsif value.respond_to?(:to_str)
    begin
      parsed_date = DateTime.parse(value)
      return parsed_date
    rescue ArgumentError => e
      raise InconclusiveMatchError.new("#{value} is not in a valid date format")
    end
  else
    raise InconclusiveMatchError.new("The date provided must be a string or date object")
  end
end

#date_in_iso8601(date) ⇒ Object



73
74
75
# File 'lib/posthog/utils.rb', line 73

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

#datetime_in_iso8601(datetime) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/posthog/utils.rb', line 53

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
80
# File 'lib/posthog/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

#is_valid_regex(regex) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/posthog/utils.rb', line 109

def is_valid_regex(regex)
  begin
    Regexp.new(regex)
    return true
  rescue RegexpError
    return false
  end
end

#isoify_dates(hash) ⇒ Object

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

strings


32
33
34
35
36
# File 'lib/posthog/utils.rb', line 32

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



40
41
42
# File 'lib/posthog/utils.rb', line 40

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

#seconds_to_utc_offset(seconds, colon = true) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/posthog/utils.rb', line 82

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



25
26
27
# File 'lib/posthog/utils.rb', line 25

def stringify_keys(hash)
  hash.each_with_object({}) { |(k, v), memo| memo[k.to_s] = v }
end

#symbolize_keys(hash) ⇒ Object

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



13
14
15
# File 'lib/posthog/utils.rb', line 13

def symbolize_keys(hash)
  hash.each_with_object({}) { |(k, v), memo| memo[k.to_sym] = v }
end

#symbolize_keys!(hash) ⇒ Object

public: Convert hash keys from strings to symbols in place



19
20
21
# File 'lib/posthog/utils.rb', line 19

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

#time_in_iso8601(time, fraction_digits = 3) ⇒ Object



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

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

#uidObject

public: Returns a uid string



46
47
48
49
50
51
# File 'lib/posthog/utils.rb', line 46

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