class Pulsedive::Client

Constants

HOST
URL

Attributes

api_key[R]

Public Class Methods

new(api_key) click to toggle source
# File lib/pulsedive/client.rb, line 13
def initialize(api_key)
  @api_key = api_key
end

Private Instance Methods

get(path, params, &block) click to toggle source
# File lib/pulsedive/client.rb, line 51
def get(path, params, &block)
  params["key"] = api_key

  url = url_for(path)
  url.query = URI.encode_www_form(params)
  get = Net::HTTP::Get.new(url)
  request(get, &block)
end
https_options() click to toggle source
# File lib/pulsedive/client.rb, line 23
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
post(path, params, &block) click to toggle source
# File lib/pulsedive/client.rb, line 60
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
request(req) { |json| ... } click to toggle source
# File lib/pulsedive/client.rb, line 37
def request(req)
  Net::HTTP.start(HOST, 443, https_options) do |http|
    response = http.request(req)
    if response.code != "200"
      raise(ResponseError, "Unsupported response code returned: #{response.code}")
    end

    json = JSON.parse(response.body)
    raise(ResponseError, json["error"]) if json["error"]

    yield json
  end
end
url_for(path) click to toggle source
# File lib/pulsedive/client.rb, line 19
def url_for(path)
  URI(URL + path)
end