Sha256: 68167869940e151d3e9eb9fe1a492f834e7f1d204c39d1854d4ecbd735c42eb2

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

require 'http'

module Yodeler::Adapters
  class HttpAdapter
    attr_accessor :host
    attr_accessor :port
    attr_accessor :path
    attr_accessor :use_ssl
    attr_accessor :default_params

    def initialize(host=nil, port:nil, path:nil, use_ssl:false, params:{})
      @host = host
      @port = port
      @path = path
      @use_ssl = use_ssl
      @default_params = params
      @handlers = {}

      handle(:default) do |url, metric, default_params|
        HTTP.post(url, json: default_params.merge(metric.to_hash))
      end
    end

    def handle(type, &block)
      @handlers[type] = block
    end

    def dispatch(metric)
      (@handlers[metric.type] || @handlers[:default]).call(url, metric, default_params)
    end

    def url
      "#{protocol}://#{host_with_port}#{path}"
    end

    private
    def host_with_port
      if port
        "#{host}:#{port}"
      else
        host
      end
    end

    def protocol
      use_ssl ? :https : :http
    end

    Yodeler.register_adapter(:http, self)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yodeler-0.1.1 lib/yodeler/adapters/http_adapter.rb