Class: Zaif::API

Inherits:
Object
  • Object
show all
Defined in:
lib/zaif.rb

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ API

Returns a new instance of API.



14
15
16
17
18
19
20
21
22
# File 'lib/zaif.rb', line 14

def initialize(opt = {})
    @cool_down = opt[:cool_down] || true
    @cool_down_time = opt[:cool_down_time] || 2
    @cert_path = opt[:cert_path] || nil
    @api_key = opt[:api_key] || nil
    @api_secret = opt[:api_secret] || nil
    @zaif_public_url = "https://api.zaif.jp/api/1/"
    @zaif_trade_url = "https://api.zaif.jp/tapi"
end

Instance Method Details

#ask(currency_code, price, amount, counter_currency_code = "jpy") ⇒ Object

Issue ask order. Need api key.



119
120
121
# File 'lib/zaif.rb', line 119

def ask(currency_code, price, amount, counter_currency_code = "jpy")
    return trade(currency_code, price, amount, "ask", counter_currency_code)
end

#bid(currency_code, price, amount, counter_currency_code = "jpy") ⇒ Object

Issue bid order. Need api key.



113
114
115
# File 'lib/zaif.rb', line 113

def bid(currency_code, price, amount, counter_currency_code = "jpy")
    return trade(currency_code, price, amount, "bid", counter_currency_code)
end

#cancel(order_id) ⇒ Object

Cancel order. Need api key.



125
126
127
128
# File 'lib/zaif.rb', line 125

def cancel(order_id)
    json = post_ssl(@zaif_trade_url, "cancel_order", {:order_id => order_id})
    return json
end

#get_active_orders(option = {}) ⇒ Object

Get your active orders. Avalible options: currency_pair Need api key.



94
95
96
97
98
99
100
101
102
# File 'lib/zaif.rb', line 94

def get_active_orders(option = {})
    json = post_ssl(@zaif_trade_url, "active_orders", option)
    # Convert to datetime
    json.each do|k, v|
        v["datetime"] = Time.at(v["timestamp"].to_i)
    end

    return json
end

#get_depth(currency_code, counter_currency_code = "jpy") ⇒ Object

Get depth of currency_code / counter_currency_code.

Parameters:

  • currency_code (String)

    Base currency code

  • counter_currency_code (String) (defaults to: "jpy")

    Counter currency code



60
61
62
63
# File 'lib/zaif.rb', line 60

def get_depth(currency_code, counter_currency_code = "jpy")
    json = get_ssl(@zaif_public_url + "depth/" + currency_code + "_" + counter_currency_code)
    return json
end

#get_infoHash

Get user infomation. Need api key.

Returns:

  • (Hash)

    Infomation of user.



72
73
74
75
# File 'lib/zaif.rb', line 72

def get_info
    json = post_ssl(@zaif_trade_url, "get_info", {})
    return json
end

#get_last_price(currency_code, counter_currency_code = "jpy") ⇒ Object

Get last price of currency_code / counter_currency_code.

Parameters:

  • currency_code (String)

    Base currency code

  • counter_currency_code (String) (defaults to: "jpy")

    Counter currency code



36
37
38
39
# File 'lib/zaif.rb', line 36

def get_last_price(currency_code, counter_currency_code = "jpy")
    json = get_ssl(@zaif_public_url + "last_price/" + currency_code + "_" + counter_currency_code)
    return json["last_price"]
end

#get_my_trades(option = {}) ⇒ Object

Get your trade history. Avalible options: from. count, from_id, end_id, order, since, end, currency_pair Need api key.

Parameters:

  • (Hash)


81
82
83
84
85
86
87
88
89
# File 'lib/zaif.rb', line 81

def get_my_trades(option = {})
    json = post_ssl(@zaif_trade_url, "trade_history", option)
    # Convert to datetime
    json.each do|k, v|
        v["datetime"] = Time.at(v["timestamp"].to_i)
    end

    return json
end

#get_ticker(currency_code, counter_currency_code = "jpy") ⇒ Object

Get ticker of currency_code / counter_currency_code.

Parameters:

  • currency_code (String)

    Base currency code

  • counter_currency_code (String) (defaults to: "jpy")

    Counter currency code



44
45
46
47
# File 'lib/zaif.rb', line 44

def get_ticker(currency_code, counter_currency_code = "jpy")
    json = get_ssl(@zaif_public_url + "ticker/" + currency_code + "_" + counter_currency_code)
    return json
end

#get_trades(currency_code, counter_currency_code = "jpy") ⇒ Object

Get trades of currency_code / counter_currency_code.

Parameters:

  • currency_code (String)

    Base currency code

  • counter_currency_code (String) (defaults to: "jpy")

    Counter currency code



52
53
54
55
# File 'lib/zaif.rb', line 52

def get_trades(currency_code, counter_currency_code = "jpy")
    json = get_ssl(@zaif_public_url + "trades/" + currency_code + "_" + counter_currency_code)
    return json
end

#set_api_key(api_key, api_secret) ⇒ Object



24
25
26
27
# File 'lib/zaif.rb', line 24

def set_api_key(api_key, api_secret)
    @api_key = api_key
    @api_secret = api_secret
end

#trade(currency_code, price, amount, action, counter_currency_code = "jpy") ⇒ Object

Issue trade. Need api key.



105
106
107
108
109
# File 'lib/zaif.rb', line 105

def trade(currency_code, price, amount, action, counter_currency_code = "jpy")
    currency_pair = currency_code + "_" + counter_currency_code
    json = post_ssl(@zaif_trade_url, "trade", {:currency_pair => currency_pair, :action => action, :price => price, :amount => amount})
    return json
end

#withdraw(currency_code, address, amount, option = {}) ⇒ Object

Withdraw funds. Need api key.



132
133
134
135
136
137
138
# File 'lib/zaif.rb', line 132

def withdraw(currency_code, address, amount, option = {})
    option["currency"] = currency_code
    option["address"] = address
    option["amount"] = amount
    json = post_ssl(@zaif_trade_url, "withdraw", option)
    return json
end