Sha256: 359dd1411f503f73201d080502ea4a9ebdcef415876d66e029048bdf12edadac

Contents?: true

Size: 791 Bytes

Versions: 2

Compression:

Stored size: 791 Bytes

Contents

# frozen_string_literal: true

require 'pp'
require 'bigdecimal'
require 'net/http'

module Mcoin
  # :nodoc:
  class InfluxDB
    def initialize(endpoint, db, user = nil, pass = '')
      @endpoint = endpoint
      @db = db
      @user = user
      @pass = pass
    end

    def save(data)
      req = request(data)
      res = Net::HTTP.start(uri.hostname, uri.port) do |http|
        http.request(req)
      end

      pp JSON.parse(res.body, decimal_class: BigDecimal) unless res.body.nil?
    end

    protected

    def request(data)
      req = Net::HTTP::Post.new(uri)
      req.basic_auth @user, @pass if @user
      req.content_type = 'multipart/form-data'
      req.body = data.join("\n")
      req
    end

    def uri
      URI("#{@endpoint}/write?db=#{@db}")
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mcoin-0.6.1 lib/mcoin/influx_db.rb
mcoin-0.6.0 lib/mcoin/influx_db.rb