15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/telesink.rb', line 15
def track(
event:, text:, emoji: nil, properties: {}, occurred_at: nil,
idempotency_key: nil, endpoint: nil
)
return false unless enabled? && (endpoint || self.endpoint)
payload = {
event: event,
text: text,
emoji: emoji,
properties: properties,
occurred_at: (occurred_at || Time.now).utc.iso8601,
idempotency_key: idempotency_key || SecureRandom.uuid,
sdk: { name: "telesink.ruby", version: VERSION }
}.compact
uri = URI(endpoint || self.endpoint)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme == "https"
http.open_timeout = http.read_timeout = 3
http.write_timeout = 3 if http.respond_to?(:write_timeout)
req = Net::HTTP::Post.new(uri)
req["Content-Type"] = "application/json"
req["User-Agent"] = "telesink.ruby/#{VERSION}"
req["Idempotency-Key"] = payload[:idempotency_key]
req.body = payload.to_json
http.request(req).is_a?(Net::HTTPSuccess)
rescue => e
logger.error("[Telesink] #{e.class}: #{e.message}")
false
end
|