Class: SnowAgent::Service
- Inherits:
-
Object
- Object
- SnowAgent::Service
- Defined in:
- lib/snowagent/service.rb
Overview
Encapsulates HTTP related logic for sending data to SnowmanIO instance TODO: use faraday instead of raw net/http?
Instance Method Summary collapse
- #build_connection(conf) ⇒ Object
- #headers ⇒ Object
-
#initialize(conf) ⇒ Service
constructor
A new instance of Service.
- #post_data(payload) ⇒ Object
Constructor Details
#initialize(conf) ⇒ Service
Returns a new instance of Service.
6 7 8 9 10 |
# 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 |
Instance Method Details
#build_connection(conf) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# 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 ⇒ Object
33 34 35 |
# File 'lib/snowagent/service.rb', line 33 def headers {'Content-Type' =>'application/json'} end |
#post_data(payload) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# 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 |