Class: Synchrolog::Logger::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/synchrolog/logger/http.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key, **args) ⇒ HTTP

Returns a new instance of HTTP.



7
8
9
10
# File 'lib/synchrolog/logger/http.rb', line 7

def initialize(api_key, **args)
  @api_key = api_key
  @host = args[:host]
end

Instance Method Details

#body(message) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/synchrolog/logger/http.rb', line 20

def body(message)
  {
    event_type: 'log', 
    timestamp: message[:timestamp],
    anonymous_id: message[:anonymous_id],
    user_id: message[:user_id],
    source: 'backend',
    api_key: @api_key,
    log: message
  }
end

#closeObject



32
33
34
# File 'lib/synchrolog/logger/http.rb', line 32

def close
  nil
end

#write(message) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/synchrolog/logger/http.rb', line 12

def write(message)
  return unless message[:anonymous_id]
  json_headers = {'Authorization' => "Basic #{@api_key}", 'Content-Type' =>'application/json'}
  uri = URI.parse("#{@host}/v1/track-backend")
  http = Net::HTTP.new(uri.host, uri.port)
  http.post(uri.path, body(message).to_json, json_headers)
end