class BitcoinTicker::Client

Public Class Methods

new(endpoint) click to toggle source
# File lib/bitcoin_ticker/client.rb, line 9
def initialize(endpoint)
  @uri = URI.parse(endpoint)

  http
end

Public Instance Methods

get(path) click to toggle source
# File lib/bitcoin_ticker/client.rb, line 15
def get(path)
  response = request(path)

  case response.code.to_i
  when 200, 201
    body = response.body ? JSON.parse(response.body, symbolize_names: true) : ""
    OpenStruct.new(code: response.code, body: body)
  when (400..499)
    fail 'client error'
  when (500..599)
    fail 'server error'
  end
rescue JSON::ParserError
  response
end

Private Instance Methods

http() click to toggle source
# File lib/bitcoin_ticker/client.rb, line 32
def http
  @http ||= begin
    http = Net::HTTP.new(@uri.host, @uri.port)
    http.use_ssl = @uri.scheme == 'https'
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    http
  end
end
request(path) click to toggle source
# File lib/bitcoin_ticker/client.rb, line 41
def request(path)
  request = Net::HTTP::Get.new(path)

  @http.request(request)
end