Class: BoringMetrics::Transport

Inherits:
Object
  • Object
show all
Defined in:
lib/boringmetrics/transport.rb

Overview

Transport layer for the BoringMetrics SDK

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Transport

Initialize a new transport

Parameters:



12
13
14
# File 'lib/boringmetrics/transport.rb', line 12

def initialize(config)
  @config = config
end

Instance Method Details

#send_logs(logs) ⇒ void

This method returns an undefined value.

Send logs to the BoringMetrics API

Parameters:

  • logs (Array<Hash>)

    The logs to send



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
# File 'lib/boringmetrics/transport.rb', line 20

def send_logs(logs)
  # Convert from Ruby style to camelCase for API
  api_logs = logs.map do |log|
    api_log = {}
    log.each do |key, value|
      case key.to_s
      when "sent_at"
        api_log[:sentAt] = value
      when "session_id"
        api_log[:sessionId] = value
      else
        api_log[key] = value
      end
    end
    api_log
  end

  with_retry do
    response = connection.post("/api/v1/logs") do |req|
      req.headers["Content-Type"] = "application/json"
      req.headers["Authorization"] = "Bearer #{@config.token}"
      req.body = { logs: api_logs }.to_json
    end

    raise "Failed to send logs: #{response.status}" unless response.success?
  end
end

#update_live(update) ⇒ void

This method returns an undefined value.

Update a live metric

Parameters:

  • update (Hash)

    The live update to send



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/boringmetrics/transport.rb', line 52

def update_live(update)
  # Convert from Ruby style to camelCase for API
  api_update = {}
  update.each do |key, value|
    case key.to_s
    when "live_id"
      api_update[:liveId] = value
    when "sent_at"
      api_update[:sentAt] = value
    else
      api_update[key] = value
    end
  end

  with_retry do
    live_id = update[:live_id] || update[:liveId]
    response = connection.put("/api/v1/lives/#{live_id}") do |req|
      req.headers["Content-Type"] = "application/json"
      req.headers["Authorization"] = "Bearer #{@config.token}"
      req.body = { live: api_update }.to_json
    end

    raise "Failed to send live update: #{response.status}" unless response.success?
  end
end