Sha256: f4144c0d6cd93c537e1a0fe811bc719adb586ca1d914e39e59c495fc7fc4ff5c

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 KB

Contents

require 'net/https'
require 'active_support'
require 'active_support/core_ext/hash'

module Pulsedive
  class Client

    attr_reader :api_key

    HOST = "pulsedive.com".freeze
    URL = "https://#{HOST}".freeze

    def initialize(api_key)
      @api_key = api_key
    end

    private

    def url_for(path)
      URI(URL + path)
    end

    def https_options
      if proxy = ENV["HTTPS_PROXY"] || ENV["https_proxy"]
        uri = URI(proxy)
        {
          proxy_address:  uri.hostname,
          proxy_port:     uri.port,
          proxy_from_env: false,
          use_ssl: true
        }
      else
        { use_ssl: true }
      end
    end

    def request(req)
      Net::HTTP.start(HOST, 443, https_options) do |http|
        response = http.request(req)
        if response.code == '200'
          json = JSON.parse(response.body)
          if json["error"]
            raise(ResponseError, json["error"])
          else
            yield json
          end
        else
          raise(ResponseError, "unsupported response code returned: #{response.code}")
        end
      end
    end

    def get(path, params, &block)
      params["key"] = api_key

      url = url_for(path)
      url.query = params.to_query
      get = Net::HTTP::Get.new(url)
      request(get, &block)
    end

    def post(path, params , &block)
      params["key"] = api_key

      post = Net::HTTP::Post.new(url_for(path))
      post.body = URI.encode_www_form(params)
      request(post, &block)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pulsedive-0.1.3 lib/pulsedive/client.rb
pulsedive-0.1.2 lib/pulsedive/client.rb
pulsedive-0.1.1 lib/pulsedive/client.rb
pulsedive-0.1.0 lib/pulsedive/client.rb