class Etwings::API
Public Class Methods
new(opt = {})
click to toggle source
# File lib/etwings.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 @etwings_public_url = "https://exchange.etwings.com/api/1/" @etwings_trade_url = "https://exchange.etwings.com/tapi" end
Public Instance Methods
ask(currency_code, price, amount, counter_currency_code = "jpy")
click to toggle source
Issue ask order. Need api key.
# File lib/etwings.rb, line 109 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")
click to toggle source
Issue bid order. Need api key.
# File lib/etwings.rb, line 103 def bid(currency_code, price, amount, counter_currency_code = "jpy") return trade(currency_code, price, amount, "bid", counter_currency_code) end
cancel(order_id)
click to toggle source
Cancel order. Need api key.
# File lib/etwings.rb, line 115 def cancel(order_id) json = post_ssl(@etwings_trade_url, "cancel_order", {:order_id => order_id}) return json end
get_active_orders(option = {})
click to toggle source
Get your active orders. Avalible options: currency_pair Need api key.
# File lib/etwings.rb, line 84 def get_active_orders(option = {}) json = post_ssl(@etwings_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")
click to toggle source
Get depth of currency_code / counter_currency_code.
# File lib/etwings.rb, line 52 def get_depth(currency_code, counter_currency_code = "jpy") json = get_ssl(@etwings_public_url + "depth/" + currency_code + "_" + counter_currency_code) return json end
get_info()
click to toggle source
Get user infomation. Need api key.
# File lib/etwings.rb, line 63 def get_info json = post_ssl(@etwings_trade_url, "get_info", {}) return json end
get_last_price(currency_code, counter_currency_code = "jpy")
click to toggle source
Get last price of currency_code / counter_currency_code.
# File lib/etwings.rb, line 34 def get_last_price(currency_code, counter_currency_code = "jpy") json = get_ssl(@etwings_public_url + "last_price/" + currency_code + "_" + counter_currency_code) return json["last_price"] end
get_my_trades(option = {})
click to toggle source
Get your trade history. Avalible options: from. count, from_id, end_id, order, since, end, currency_pair Need api key.
# File lib/etwings.rb, line 71 def get_my_trades(option = {}) json = post_ssl(@etwings_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")
click to toggle source
Get ticker of currency_code / counter_currency_code.
# File lib/etwings.rb, line 40 def get_ticker(currency_code, counter_currency_code = "jpy") json = get_ssl(@etwings_public_url + "ticker/" + currency_code + "_" + counter_currency_code) return json end
get_trades(currency_code, counter_currency_code = "jpy")
click to toggle source
Get trades of currency_code / counter_currency_code.
# File lib/etwings.rb, line 46 def get_trades(currency_code, counter_currency_code = "jpy") json = get_ssl(@etwings_public_url + "trades/" + currency_code + "_" + counter_currency_code) return json end
set_api_key(api_key, api_secret)
click to toggle source
# File lib/etwings.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")
click to toggle source
Issue trade. Need api key.
# File lib/etwings.rb, line 95 def trade(currency_code, price, amount, action, counter_currency_code = "jpy") currency_pair = currency_code + "_" + counter_currency_code json = post_ssl(@etwings_trade_url, "trade", {:currency_pair => currency_pair, :action => action, :price => price, :amount => amount}) return json end
withdraw(currency_code, address, amount, option = {})
click to toggle source
Withdraw funds. Need api key.
# File lib/etwings.rb, line 122 def withdraw(currency_code, address, amount, option = {}) option["currency"] = currency_code option["address"] = address option["amount"] = amount json = post_ssl(@etwings_trade_url, "withdraw", option) return json end
Private Instance Methods
check_key()
click to toggle source
Class private method
# File lib/etwings.rb, line 136 def check_key if @api_key.nil? or @api_secret.nil? raise "You need to set a API key and secret" end end
get_cool_down()
click to toggle source
# File lib/etwings.rb, line 214 def get_cool_down if @cool_down sleep(@cool_down_time) end end
get_nonce()
click to toggle source
# File lib/etwings.rb, line 209 def get_nonce time = Time.now.to_f return time.to_i end
get_ssl(address)
click to toggle source
Connect to address via https, and return json reponse.
# File lib/etwings.rb, line 143 def get_ssl(address) uri = URI.parse(address) begin 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.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 raise APIErrorException, json["error"] if json.is_a?(Hash) && json.has_key?("error") get_cool_down return json else raise ConnectionFailedException, "Failed to connect to etwings." end } rescue raise end end
post_ssl(address, method, data)
click to toggle source
Connect to address via https, and return json reponse.
# File lib/etwings.rb, line 172 def post_ssl(address, method, data) check_key uri = URI.parse(address) data["method"] = method data["nonce"] = get_nonce begin req = Net::HTTP::Post.new(uri) req.set_form_data(data) req["Key"] = @api_key req["Sign"] = OpenSSL::HMAC::hexdigest(OpenSSL::Digest.new('sha512'), @api_secret, req.body) 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.start {|w| response = w.request(req) case response when Net::HTTPSuccess json = JSON.parse(response.body) raise JSONException, response.body if json == nil raise APIErrorException, json["error"] if json.is_a?(Hash) && json["success"] == 0 get_cool_down return json["return"] else raise ConnectionFailedException, "Failed to connect to etwings: " + response.value end } rescue raise end end