-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_current.rb
191 lines (189 loc) · 4.07 KB
/
get_current.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
require 'json'
require 'open-uri'
class String
def is_i?
/\A[-+]?\d+\z/ === self
end
end
class GetData
def getResponse(api)
# Buy, Sell and Volume are at same endpoint
currentEx = Hash['success'=> false, 'buy'=> -1, 'sell'=> -1, 'volume'=> -1]
if api.length == 1
url = api['all']['endpoint']
buy = api['all']['buyKey'].split('.')
sell = api['all']['sellKey'].split('.')
volume = api['all']['volumeKey']
# Check if Volume is provided
if volume
volume = volume.split('.')
else
volume = -1
end
# Get Buy, Sell Price and Volume
begin
response = JSON.parse(open(url).read)
rescue Exception => e
currentEx["success"] = false
currentEx["buy"] = -1
currentEx["sell"] = -1
currentEx["volume"] = -1
else
currentEx["success"] = true
tmp = response
for key in buy
key = key.is_i? ? key.to_i : key
if !tmp.nil?
tmp = tmp[key]
else
currentEx["success"] = false
tmp = -1
break
end
end
currentEx["buy"] = tmp
tmp = response
for key in sell
key = key.is_i? ? key.to_i : key
if !tmp.nil?
tmp = tmp[key]
else
currentEx["success"] = false
tmp = -1
break
end
end
currentEx["sell"] = tmp
if volume != -1
tmp = response
for key in volume
key = key.is_i? ? key.to_i : key
if !tmp.nil?
tmp = tmp[key]
else
tmp = -1
break
end
end
else
tmp = -1
end
currentEx["volume"] = tmp
end
# Buy, Sell and Volume are at different endpoint
else
buyEndpoint = api['buy']['endpoint']
buyKey = api['buy']['buyKey'].split('.')
sellEndpoint = api['sell']['endpoint']
sellKey = api['sell']['sellKey'].split('.')
if api['volume']
volumeEndpoint = api['volume']['endpoint']
volumeKey = api['volume']['volumeKey'].split('.')
else
volumeKey = -1
end
# Get Buy Price
begin
response = JSON.parse(open(buyEndpoint).read)
rescue Exception => e
currentEx["success"] = false
currentEx["buy"] = -1
currentEx["sell"] = -1
currentEx["volume"] = -1
return
else
tmp = response
for key in buyKey
key = key.is_i? ? key.to_i : key
if !tmp.nil?
tmp = tmp[key]
else
currentEx["success"] = false
tmp = -1
break
end
end
currentEx["buy"] = tmp
end
# Get Sell Price
begin
response = JSON.parse(open(sellEndpoint).read)
rescue Exception => e
currentEx["success"] = false
currentEx["buy"] = -1
currentEx["sell"] = -1
currentEx["volume"] = -1
return
else
currentEx["success"] = true
tmp = response
for key in sellKey
key = key.is_i? ? key.to_i : key
if !tmp.nil?
tmp = tmp[key]
else
currentEx["success"] = false
tmp = -1
break
end
end
currentEx["sell"] = tmp
end
# Get Volume if provided
if volumeKey != -1
begin
response = JSON.parse(open(volumeEndpoint).read)
rescue Exception => e
currentEx["volume"] = -1
else
tmp = response
for key in volumeKey
key = key.is_i? ? key.to_i : key
if !tmp.nil?
tmp = tmp[key]
else
tmp = -1
break
end
end
currentEx["volume"] = tmp
end
end
end
return currentEx
end
def getFileContents(file)
text = File.read(file)
return text
rescue Exception => e
puts e.message
return -1
end
def getCurrent
for exchange in @jsonData
result = getResponse(exchange['api'])
currentData = {}
currentData["crypto_curr"] = exchange['crypto_currency']
currentData["curr"] = exchange['currency']
currentData["exchange_id"] = exchange['id']
currentData["success"] = result["success"]
currentData["buy"] = result["buy"]
currentData["sell"] = result["sell"]
currentData["volume"] = result["volume"]
@result.push(currentData)
end
end
def getResult
return @result
end
def initialize
@result = []
path = './exchanges.json'
@jsonData = JSON.parse(getFileContents(path))
if @jsonData != -1
getCurrent
else
@result = []
end
end
end