Class: CoinSync::Importers::BitBayAPI
- Inherits:
-
Base
- Object
- Base
- CoinSync::Importers::BitBayAPI
show all
- Defined in:
- lib/coinsync/importers/bitbay_api.rb
Defined Under Namespace
Classes: HistoryEntry
Constant Summary
collapse
- BASE_URL =
"https://bitbay.net/API/Trading/tradingApi.php"
- OP_PURCHASE =
'+currency_transaction'
- OP_SALE =
'-pay_for_currency'
- OP_FEE =
'-fee'
- MAX_TIME_DIFFERENCE =
5.0
- TRANSACTION_TYPES =
[OP_PURCHASE, OP_SALE, OP_FEE]
- POLISH_TIMEZONE =
TZInfo::Timezone.get('Europe/Warsaw')
Instance Method Summary
collapse
Methods inherited from Base
#can_build?, register_commands, register_importer, registered_commands
Constructor Details
#initialize(config, params = {}) ⇒ BitBayAPI
Returns a new instance of BitBayAPI.
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/coinsync/importers/bitbay_api.rb', line 73
def initialize(config, params = {})
super
@public_key = params['api_public_key']
@secret_key = params['api_private_key']
end
|
Instance Method Details
#can_import?(type) ⇒ Boolean
87
88
89
|
# File 'lib/coinsync/importers/bitbay_api.rb', line 87
def can_import?(type)
@public_key && @secret_key && [:balances, :transactions].include?(type)
end
|
#import_balances ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/coinsync/importers/bitbay_api.rb', line 126
def import_balances
info = fetch_info
info['balances'].select { |k, v|
v['available'].to_f > 0 || v['locked'].to_f > 0
}.map { |k, v|
Balance.new(
CryptoCurrency.new(k),
available: BigDecimal.new(v['available']),
locked: BigDecimal.new(v['locked'])
)
}
end
|
#import_transactions(filename) ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/coinsync/importers/bitbay_api.rb', line 91
def import_transactions(filename)
info = fetch_info
currencies = info['balances'].keys
transactions = []
currencies.each do |currency|
sleep 1
response = make_request('history', currency: currency, limit: 10000)
case response
when Net::HTTPSuccess
json = JSON.parse(response.body)
if !json.is_a?(Array)
raise "BitBay API importer: Invalid response: #{response.body}"
end
transactions.concat(json)
when Net::HTTPBadRequest
raise "BitBay API importer: Bad request: #{response}"
else
raise "BitBay API importer: Bad response: #{response}"
end
end
transactions.each_with_index { |tx, i| tx['i'] = i }
transactions.sort_by! { |tx| [tx['time'], -tx['i']] }
transactions.each_with_index { |tx, i| tx.delete('i') }
File.write(filename, JSON.pretty_generate(transactions))
end
|
#read_transaction_list(source) ⇒ Object
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/coinsync/importers/bitbay_api.rb', line 140
def read_transaction_list(source)
json = JSON.parse(source.read)
matching = []
transactions = []
json.each do |hash|
entry = HistoryEntry.new(hash)
next unless TRANSACTION_TYPES.include?(entry.type)
if !matching.empty?
must_match = matching.any? { |e| (e.date - entry.date).abs > MAX_TIME_DIFFERENCE }
transaction = process_matched(matching, must_match)
transactions << transaction if transaction
end
matching << entry
end
if !matching.empty?
transactions << process_matched(matching, true)
end
transactions
end
|