class Bitcoin::OTC::Client

HTTP client for fetching ‘#bitcoin-otc` data.

Constants

BASE_URL

Public Class Methods

base_url() click to toggle source

Returns the ‘#bitcoin-otc` base URL.

@return [URI]

# File lib/bitcoin/otc/client.rb, line 16
def self.base_url
  @base_url ||= URI.parse(BASE_URL)
end
load(*args) click to toggle source
# File lib/bitcoin/otc/client.rb, line 20
def self.load(*args)
  self.new.load(*args)
end
new() click to toggle source
# File lib/bitcoin/otc/client.rb, line 24
def initialize
  @headers = {}
end

Public Instance Methods

get(path, headers = {}, &block) click to toggle source

Performs an HTTP GET request for the given path.

@param [String, to_s] path @param [Hash{String => String}] headers @yield [response] @yieldparam [Net::HTTPResponse] response @return [Net::HTTPResponse]

# File lib/bitcoin/otc/client.rb, line 76
def get(path, headers = {}, &block)
  Net::HTTP.start(self.host, self.port) do |http|
    response = http.get(path.to_s, @headers.merge(headers))
    if block_given?
      block.call(response)
    else
      response
    end
  end
end
host() click to toggle source

Returns the server’s host name.

@return [String]

# File lib/bitcoin/otc/client.rb, line 32
def host
  self.class.base_url.host
end
load(op, options = {}) click to toggle source

@param [String] op @param [Hash] options @return [Hash]

# File lib/bitcoin/otc/client.rb, line 57
def load(op, options = {})
  self.get(self.path_for(op, options)) do |response|
    case response
      when Net::HTTPSuccess
        JSON.parse(response.body)
      else
        raise Error.new("failed to retrive #bitcoin-otc data", response)
    end
  end
end
path_for(op, options = {}) click to toggle source

@param [String] op @param [Hash] options @return [Hash]

# File lib/bitcoin/otc/client.rb, line 48
def path_for(op, options = {})
  options[:outformat] ||= :json
  '/' + op + '?' + options.map { |k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join('&')
end
port() click to toggle source

Returns the server’s port number.

@return [Integer]

# File lib/bitcoin/otc/client.rb, line 40
def port
  self.class.base_url.port
end