Class: MaiCoin::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/maicoin/client.rb

Direct Known Subclasses

OAuthClient

Defined Under Namespace

Classes: Error

Constant Summary collapse

BASE_URI =
'https://api.maicoin.com/v1'

Class Method Summary collapse

Instance Method Summary collapse

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='', options={})
  @api_key = api_key
  @api_secret = api_secret

  # defaults
  options[:base_uri] ||= BASE_URI
  @base_uri = options[:base_uri]
  options[:format]   ||= :json
  options.each do |k,v|
    self.class.send k, v
  end
end

Class Method Details

.build_whitelisted_cert_storeObject



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.expand_path(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_storeObject



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

#balanceObject

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')
  options = {amount: amount, type: 'buy', currency: currency}
  create_order(options)
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, options={})
  get '/checkouts', {page:page}.merge(options)
end

#create_account_pin(pin, options = {}) ⇒ Object



62
63
64
65
# File 'lib/maicoin/client.rb', line 62

def (pin, options={})
  options.merge!( {pin: pin})
  post '/user/account_pin', options
end

#create_checkout(options = {}) ⇒ Object

Checkouts



140
141
142
# File 'lib/maicoin/client.rb', line 140

def create_checkout(options = {})
  post '/checkouts', options
end

#create_order(options = {}) ⇒ Object



95
96
97
# File 'lib/maicoin/client.rb', line 95

def create_order (options={})
  post '/orders', options
end

#currenciesObject

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, options={})
  http_verb :delete, path, options
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', options={})
  options.merge!({currency: currency})
  post '/account/receive_address', options
end

#get(path, options = {}) ⇒ Object

Wrappers for the main HTTP verbs



156
157
158
# File 'lib/maicoin/client.rb', line 156

def get(path, options={})
  http_verb :get, path, options
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, options={})

  nonce = options[:nonce] || (Time.now.to_f * 1e6).to_i

  if [:get, :delete].include? verb
    request_options = {}
    path = "#{path}?#{URI.encode_www_form(options)}" if !options.empty?
    hmac_message = nonce.to_s + @base_uri + path
  else
    request_options = {body: options.to_json}
    hmac_message = nonce.to_s + @base_uri + path + options.to_json
  end

  signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @api_secret, hmac_message)

  headers = {
    'ACCESS_KEY' => @api_key,
    'ACCESS_SIGNATURE' => signature,
    'ACCESS_NONCE' => nonce.to_s,
    'Content-Type' => 'application/json',
  }
  request_options[:headers] = headers
  r = self.class.send(verb, path, request_options.merge(ssl_options))
  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, options ={})
  get '/orders',{page: page}.merge(options)
end

#post(path, options = {}) ⇒ Object



160
161
162
# File 'lib/maicoin/client.rb', line 160

def post(path, options={})
  http_verb :post, path, options
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, options={})
  http_verb :put, path, options
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, options = {})
  request_transaction(address, amount, currency, options)
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, options={})
  options.merge!( { type: 'request',
                    address: address,
                    amount: amount,
                    currency: currency})
  post '/transactions', options
end

#sell_btc(amount) ⇒ Object



90
91
92
93
# File 'lib/maicoin/client.rb', line 90

def sell_btc (amount)
  options = {amount: amount, type: 'sell'}
  create_order(options)
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, =nil, options={})
  options.merge!( { type: 'send',
                    account_pin: ,
                    currency: currency,
                    address: address,
                    amount: amount})
  post '/transactions', options
end

#ssl_optionsObject



197
198
199
# File 'lib/maicoin/client.rb', line 197

def ssl_options
  { 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, options={})
  get '/transactions', {page:page}.merge(options)
end

#update_account_pin(old_pin, new_pin, options = {}) ⇒ Object



67
68
69
70
# File 'lib/maicoin/client.rb', line 67

def (old_pin, new_pin, options={})
  options.merge!( {old_pin: old_pin, new_pin: new_pin})
  put '/user/account_pin', options
end

#userObject



58
59
60
# File 'lib/maicoin/client.rb', line 58

def user
  get '/user'
end