class SnowAgent::Service

Encapsulates HTTP related logic for sending data to SnowmanIO instance TODO: use faraday instead of raw net/http?

Public Class Methods

new(conf) click to toggle source
# File lib/snowagent/service.rb, line 6
def initialize(conf)
  @uri        =  URI.join(conf.server, "agent/metrics")
  @connection = build_connection(conf)
  @token      = conf.secret_token
end

Public Instance Methods

build_connection(conf) click to toggle source
# File lib/snowagent/service.rb, line 23
def build_connection(conf)
  http = Net::HTTP.new(@uri.host, @uri.port)

  http.use_ssl      = (@uri.scheme == "https")
  http.read_timeout = conf.read_timeout if conf.read_timeout
  http.open_timeout = conf.open_timeout if conf.open_timeout

  http
end
headers() click to toggle source
# File lib/snowagent/service.rb, line 33
def headers
  {'Content-Type' =>'application/json'}
end
post_data(payload) click to toggle source
# File lib/snowagent/service.rb, line 12
def post_data(payload)
  req = Net::HTTP::Post.new("#{@uri.path}?#{@uri.query}", headers)
  req.body = JSON.dump(payload.merge(token: @token))
  size = req.body.length

  response = @connection.request(req)
  SnowAgent.logger.info "[SnowAgent] POST #{size} bytes to #{@uri}"

  response
end