Class: MaiCoin::Client
- Inherits:
-
Object
- Object
- MaiCoin::Client
- Includes:
- HTTParty
- Defined in:
- lib/maicoin/client.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Error
Constant Summary collapse
- BASE_URI =
'https://api.maicoin.com/v1'
Class Method Summary collapse
Instance Method Summary collapse
- #addresses(currency = 'btc') ⇒ Object
-
#balance ⇒ Object
Account.
- #buy_btc(amount) ⇒ Object
- #buy_order(amount, currency = 'btc') ⇒ Object
- #cancel_request_btc(txid) ⇒ Object
- #cancel_request_transaction(txid) ⇒ Object
- #checkout(uid) ⇒ Object
- #checkouts(page = 1, options = {}) ⇒ Object
- #create_account_pin(pin, options = {}) ⇒ Object
-
#create_checkout(options = {}) ⇒ Object
Checkouts.
- #create_order(options = {}) ⇒ Object
-
#currencies ⇒ Object
Currencies.
- #delete(path, options = {}) ⇒ Object
- #generate_receive_address(currency = 'btc', options = {}) ⇒ Object
-
#get(path, options = {}) ⇒ Object
Wrappers for the main HTTP verbs.
- #http_verb(verb, path, options = {}) ⇒ Object
-
#initialize(api_key = '', api_secret = '', options = {}) ⇒ Client
constructor
A new instance of Client.
- #order(txid) ⇒ Object
-
#orders(page = 1, options = {}) ⇒ Object
Orders, options:limit:25.
- #post(path, options = {}) ⇒ Object
-
#prices(currency = 'TWD') ⇒ Object
Prices.
- #put(path, options = {}) ⇒ Object
- #receive_address(currency = 'btc') ⇒ Object
-
#request_btc(address, amount, currency, options = {}) ⇒ Object
options:“”.
- #request_transaction(address, amount, currency, options = {}) ⇒ Object
- #sell_btc(amount) ⇒ Object
-
#send_transaction(address, amount, currency, account_pin = nil, options = {}) ⇒ Object
btc transactions.
- #ssl_options ⇒ Object
- #transaction(txid) ⇒ Object
-
#transactions(page = 1, options = {}) ⇒ Object
Transactions, option:limit:25.
- #update_account_pin(old_pin, new_pin, options = {}) ⇒ Object
- #user ⇒ Object
Constructor Details
#initialize(api_key = '', api_secret = '', options = {}) ⇒ Client
Returns a new instance of Client.
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/maicoin/client.rb', line 15 def initialize(api_key='', api_secret='', ={}) @api_key = api_key @api_secret = api_secret # defaults [:base_uri] ||= BASE_URI @base_uri = [:base_uri] [:format] ||= :json .each do |k,v| self.class.send k, v end end |
Class Method Details
.build_whitelisted_cert_store ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/maicoin/client.rb', line 176 def self.build_whitelisted_cert_store path = File.(File.join(File.dirname(__FILE__), 'ca-maicoin.crt')) certs = [ [] ] File.readlines(path).each{|line| next if ["\n","#"].include?(line[0]) certs.last << line certs << [] if line == "-----END CERTIFICATE-----\n" } result = OpenSSL::X509::Store.new certs.each{|lines| next if lines.empty? cert = OpenSSL::X509::Certificate.new(lines.join) result.add_cert(cert) } result end |
.whitelisted_cert_store ⇒ Object
172 173 174 |
# File 'lib/maicoin/client.rb', line 172 def self.whitelisted_cert_store @@cert_store ||= build_whitelisted_cert_store end |
Instance Method Details
#addresses(currency = 'btc') ⇒ Object
49 50 51 |
# File 'lib/maicoin/client.rb', line 49 def addresses(currency='btc') get "/account/addresses/#{currency}" end |
#balance ⇒ Object
Account
41 42 43 |
# File 'lib/maicoin/client.rb', line 41 def balance get '/account/balance' end |
#buy_btc(amount) ⇒ Object
81 82 83 |
# File 'lib/maicoin/client.rb', line 81 def buy_btc (amount) buy_order(amount, 'btc') end |
#buy_order(amount, currency = 'btc') ⇒ Object
85 86 87 88 |
# File 'lib/maicoin/client.rb', line 85 def buy_order(amount, currency='btc') = {amount: amount, type: 'buy', currency: currency} create_order() end |
#cancel_request_btc(txid) ⇒ Object
113 114 115 |
# File 'lib/maicoin/client.rb', line 113 def cancel_request_btc (txid) cancel_request_transaction(txid) end |
#cancel_request_transaction(txid) ⇒ Object
135 136 137 |
# File 'lib/maicoin/client.rb', line 135 def cancel_request_transaction(txid) delete "/transactions/#{txid}" end |
#checkout(uid) ⇒ Object
144 145 146 |
# File 'lib/maicoin/client.rb', line 144 def checkout(uid) get "/checkouts/#{uid}" end |
#checkouts(page = 1, options = {}) ⇒ Object
148 149 150 |
# File 'lib/maicoin/client.rb', line 148 def checkouts (page=1, ={}) get '/checkouts', {page:page}.merge() end |
#create_account_pin(pin, options = {}) ⇒ Object
62 63 64 65 |
# File 'lib/maicoin/client.rb', line 62 def create_account_pin(pin, ={}) .merge!( {pin: pin}) post '/user/account_pin', end |
#create_checkout(options = {}) ⇒ Object
Checkouts
140 141 142 |
# File 'lib/maicoin/client.rb', line 140 def create_checkout( = {}) post '/checkouts', end |
#create_order(options = {}) ⇒ Object
95 96 97 |
# File 'lib/maicoin/client.rb', line 95 def create_order (={}) post '/orders', end |
#currencies ⇒ Object
Currencies
36 37 38 |
# File 'lib/maicoin/client.rb', line 36 def currencies get '/currencies' end |
#delete(path, options = {}) ⇒ Object
168 169 170 |
# File 'lib/maicoin/client.rb', line 168 def delete(path, ={}) http_verb :delete, path, end |
#generate_receive_address(currency = 'btc', options = {}) ⇒ Object
53 54 55 56 |
# File 'lib/maicoin/client.rb', line 53 def generate_receive_address(currency='btc', ={}) .merge!({currency: currency}) post '/account/receive_address', end |
#get(path, options = {}) ⇒ Object
Wrappers for the main HTTP verbs
156 157 158 |
# File 'lib/maicoin/client.rb', line 156 def get(path, ={}) http_verb :get, path, end |
#http_verb(verb, path, options = {}) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/maicoin/client.rb', line 201 def http_verb(verb, path, ={}) nonce = [:nonce] || (Time.now.to_f * 1e6).to_i if [:get, :delete].include? verb = {} path = "#{path}?#{URI.encode_www_form()}" if !.empty? = nonce.to_s + @base_uri + path else = {body: .to_json} = nonce.to_s + @base_uri + path + .to_json end signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @api_secret, ) headers = { 'ACCESS_KEY' => @api_key, 'ACCESS_SIGNATURE' => signature, 'ACCESS_NONCE' => nonce.to_s, 'Content-Type' => 'application/json', } [:headers] = headers r = self.class.send(verb, path, .merge()) Hashie::Mash.new(JSON.parse(r.body)) end |
#order(txid) ⇒ Object
77 78 79 |
# File 'lib/maicoin/client.rb', line 77 def order (txid) get "/orders/#{txid}" end |
#orders(page = 1, options = {}) ⇒ Object
Orders, options:limit:25
73 74 75 |
# File 'lib/maicoin/client.rb', line 73 def orders (page=1, ={}) get '/orders',{page: page}.merge() end |
#post(path, options = {}) ⇒ Object
160 161 162 |
# File 'lib/maicoin/client.rb', line 160 def post(path, ={}) http_verb :post, path, end |
#prices(currency = 'TWD') ⇒ Object
Prices
31 32 33 |
# File 'lib/maicoin/client.rb', line 31 def prices (currency='TWD') get '/prices/' + currency end |
#put(path, options = {}) ⇒ Object
164 165 166 |
# File 'lib/maicoin/client.rb', line 164 def put(path, ={}) http_verb :put, path, end |
#receive_address(currency = 'btc') ⇒ Object
45 46 47 |
# File 'lib/maicoin/client.rb', line 45 def receive_address(currency='btc') get "/account/receive_address/#{currency}" end |
#request_btc(address, amount, currency, options = {}) ⇒ Object
options:“”
109 110 111 |
# File 'lib/maicoin/client.rb', line 109 def request_btc (address, amount, currency, = {}) request_transaction(address, amount, currency, ) end |
#request_transaction(address, amount, currency, options = {}) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/maicoin/client.rb', line 127 def request_transaction(address, amount, currency, ={}) .merge!( { type: 'request', address: address, amount: amount, currency: currency}) post '/transactions', end |
#sell_btc(amount) ⇒ Object
90 91 92 93 |
# File 'lib/maicoin/client.rb', line 90 def sell_btc (amount) = {amount: amount, type: 'sell'} create_order() end |
#send_transaction(address, amount, currency, account_pin = nil, options = {}) ⇒ Object
btc transactions
118 119 120 121 122 123 124 125 |
# File 'lib/maicoin/client.rb', line 118 def send_transaction(address, amount, currency, account_pin=nil, ={}) .merge!( { type: 'send', account_pin: account_pin, currency: currency, address: address, amount: amount}) post '/transactions', end |
#ssl_options ⇒ Object
197 198 199 |
# File 'lib/maicoin/client.rb', line 197 def { verify: true, cert_store: self.class.whitelisted_cert_store } end |
#transaction(txid) ⇒ Object
104 105 106 |
# File 'lib/maicoin/client.rb', line 104 def transaction (txid) get "/transactions/#{txid}" end |
#transactions(page = 1, options = {}) ⇒ Object
Transactions, option:limit:25
100 101 102 |
# File 'lib/maicoin/client.rb', line 100 def transactions (page=1, ={}) get '/transactions', {page:page}.merge() end |
#update_account_pin(old_pin, new_pin, options = {}) ⇒ Object
67 68 69 70 |
# File 'lib/maicoin/client.rb', line 67 def update_account_pin(old_pin, new_pin, ={}) .merge!( {old_pin: old_pin, new_pin: new_pin}) put '/user/account_pin', end |
#user ⇒ Object
58 59 60 |
# File 'lib/maicoin/client.rb', line 58 def user get '/user' end |