class Kaesen::Bitbank

Bitbank Wrapper Class docs.bitbank.cc/

Public Class Methods

new(options = {}) { |self| ... } click to toggle source
Calls superclass method
# File lib/kaesen/bitbank.rb, line 14
def initialize(options = {})
  super()
  @name        = "Bitbank"
  @api_key     = ENV["BITBANK_KEY"]
  @api_secret  = ENV["BITBANK_SECRET"]
  @url_public  = "https://public.bitbank.cc"
  @url_private = "https://api.bitbank.cc/v1"

  options.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
  yield(self) if block_given?
end

Public Instance Methods

depth() click to toggle source

Get order book. @abstract @return [hash] array of market depth

asks: [Array] 売りオーダー
   price : [BigDecimal]
   size : [BigDecimal]
bids: [Array] 買いオーダー
   price : [BigDecimal]
   size : [BigDecimal]
ltimestamp: [int] ローカルタイムスタンプ
# File lib/kaesen/bitbank.rb, line 67
def depth
  h = get_ssl(@url_public + "/btc_jpy/depth")
  h = h["data"]
  {
    "asks"       => h["asks"].map{|a,b| [BigDecimal.new(a.to_s), BigDecimal.new(b.to_s)]}, # to_s でないと誤差が生じる
    "bids"       => h["bids"].map{|a,b| [BigDecimal.new(a.to_s), BigDecimal.new(b.to_s)]}, # to_s でないと誤差が生じる
    "ltimestamp" => Time.now.to_i,
  }
end
ticker() click to toggle source

Get ticker information. @return [hash] ticker

ask: [BigDecimal] 最良売気配値
bid: [BigDecimal] 最良買気配値
last: [BigDecimal] 最近値(?用語要チェック), last price
high: [BigDecimal] 高値
low: [BigDecimal] 安値
volume: [BigDecimal] 取引量
timestamp: [int] タイムスタンプ
ltimestamp: [int] ローカルタイムスタンプ
# File lib/kaesen/bitbank.rb, line 42
def ticker
  h = get_ssl(@url_public + "/btc_jpy/ticker")
  h = h["data"]
  {
    "ask"        => BigDecimal.new(h["sell"][0]),
    "bid"        => BigDecimal.new(h["buy"][0]),
    "last"       => BigDecimal.new(h["last"][0]),
    "high"       => BigDecimal.new(h["high"][1]), # of the previous 24 hours
    "low"        => BigDecimal.new(h["low"][1]), # of the previous 24 hours
    "volume"     => BigDecimal.new(h["vol"][1]), # of the previous 24 hours
    "ltimestamp" => Time.now.to_i,
    "timestamp"  => h["timestamp"],
  }
end

Private Instance Methods

get_nonce() click to toggle source
# File lib/kaesen/bitbank.rb, line 111
def get_nonce
  pre_nonce = @@nonce
  next_nonce = (Time.now.to_i) * 100

  if next_nonce <= pre_nonce
    @@nonce = pre_nonce + 1
  else
    @@nonce = next_nonce
  end

  return @@nonce
end
get_sign(req) click to toggle source
# File lib/kaesen/bitbank.rb, line 124
def get_sign(req)
  secret = @api_secret
  text = req.body

  OpenSSL::HMAC::hexdigest(OpenSSL::Digest.new('sha512'), secret, text)
end
get_ssl(address) click to toggle source

Connect to address via https, and return json response.

# File lib/kaesen/bitbank.rb, line 90
def get_ssl(address)
  uri = URI.parse(address)

  begin
    https = initialize_https(uri)
    https.start {|w|
      response = w.get(uri.request_uri)
      case response
        when Net::HTTPSuccess
          json = JSON.parse(response.body)
          raise JSONException, response.body if json == nil
          return json
        else
          raise ConnectionFailedException, "Failed to connect to #{@name}."
      end
    }
  rescue
    raise
  end
end
initialize_https(uri) click to toggle source
# File lib/kaesen/bitbank.rb, line 79
def initialize_https(uri)
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true
  https.open_timeout = 5
  https.read_timeout = 15
  https.verify_mode = OpenSSL::SSL::VERIFY_PEER
  https.verify_depth = 5
  https
end
post_ssl(address, data={}) click to toggle source

Connect to address via https, and return json response.

# File lib/kaesen/bitbank.rb, line 132
def post_ssl(address, data={})
  uri = URI.parse(address)
  data["nonce"] = get_nonce

  begin
    req = Net::HTTP::Post.new(uri)
    req.set_form_data(data)
    req["Key"] = @api_key
    req["Sign"] = get_sign(req)

    https = initialize_https(uri)
    https.start {|w|
      response = w.request(req)
      case response
        when Net::HTTPSuccess
          json = JSON.parse(response.body)
          raise JSONException, response.body if json == nil
          return json
        else
          raise ConnectionFailedException, "Failed to connect to #{@name}: " + response.value
      end
    }
  rescue
    raise
  end
end