-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.rb
168 lines (156 loc) · 5.61 KB
/
functions.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
$error = Logger.new('errors.log', 'monthly') # log de erros
def title(s) # IMPRIMINDO TÍTULO
print "\n"
x = 80 - s.length
(x / 2).times do
print "_"
end
print s.upcase
(x / 2.0).round.times do
print "_"
end
print "\n"
end
def internet? # CHECANDO CONEXÃO
if open('https://google.com/') || open('https://www.coingecko.com/')
return true
else
return false
end
end
def beep # SOM DE BIPE
if RUBY_PLATFORM.downcase.include?("linux")
print 7.chr
elsif RUBY_PLATFORM.downcase.include?("mac")
system('say "beep"')
else
print "\a"
end
end
def listar # LISTA DE TODOS OS ATIVOS
print "\n [Importing asset list... "
response = apidata("https://coingecko.p.rapidapi.com/coins/list")
if eval(response.read_body) == nil || eval(response.read_body) == "" || response.read_body.include?("invalid")
print "error]\n" # erro em caso de retorno vazio
$error.debug(response.read_body) # escrevendo a resposta da API no log de erros
gets
exit
end
print "#{eval(response.read_body).length} inputs]\n"
return eval(response.read_body)
end
def data(a, p) # DADOS DO CRIPTOATIVO
print " [Importing asset data... " if p == true
response = apidata("https://coingecko.p.rapidapi.com/coins/" + a)
if JSON.parse(response.read_body) == nil || response.read_body == "" || response.read_body.downcase.include?("invalid") || response.read_body.downcase.include?("could not find coin")
print "\n\nERROR: ASSET NOT FOUND!\n" # erro em caso de retorno vazio
$error.debug(response.read_body) # escrevendo a resposta da API no log de erros
gets
exit
end
# convertendo em array e substituindo caracteres
data = JSON.parse(response.read_body.gsub(":", ",").gsub("{", "[").gsub("}", "]"))[JSON.parse(response.read_body.gsub(":", ",").gsub("{", "[").gsub("}", "]")).index("market_data") + 1]
# removendo outras moedas além do USD
arr = ["current_price", "ath", "ath_change_percentage", "ath_date", "atl", "atl_change_percentage", "atl_date", "market_cap", "total_volume", "high_24h", "low_24h", "price_change_24h_in_currency", "price_change_percentage_1h_in_currency", "price_change_percentage_24h_in_currency", "price_change_percentage_7d_in_currency", "price_change_percentage_14d_in_currency", "price_change_percentage_30d_in_currency", "price_change_percentage_60d_in_currency", "price_change_percentage_200d_in_currency", "price_change_percentage_1y_in_currency", "market_cap_change_24h_in_currency", "market_cap_change_percentage_24h_in_currency"]
arr.each do |n|
next if !data.include?(n) # pulando caso o elemento não esteja incluso
next if data[data.index(n) + 1].class != Array # pulando se não for array
temp = data[data.index(n) + 1]
if temp.include?("usd") # verificando se tem o valor em dólar
data[data.index(n) + 1] = temp[temp.index("usd") + 1].to_f
else
data[data.index(n) + 1] = 0.0
end
end
out = Hash.new # criando hash
for x in (0..data.length - 2)
next if x % 2 != 0 # pulando posições ímpares
out[data[x]] = data[x + 1] # adicionando os elementos da hash
end
if out.length > 0
print "#{out.length} inputs]\n" if p == true
else
print "error]\n" if p == true
$error.debug(out.to_s) # escrevendo a resposta da API no log de erros
gets
exit
end
return out
end
def fnum(n, f) # FORMATANDO OS NÚMEROS
n = n.to_f
if f == 1 # formato para moeda
if n >= 1
return "$#{"%.2f" % n.round(2)}"
else
return "$#{"%.8f" % n.round(8)}"
end
elsif f == 2 # formato para porcentagem
if n > 0
return "+#{n.round(2)}%"
else
return "#{n.round(2)}%"
end
elsif f == 3 # formato geral
if n.abs >= 1
return "#{n.round(2)}"
else
return "#{n.round(8)}"
end
elsif f == 4 # valores altos
return "#{n.round.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse}"
else
return "[invalid option]"
end
end
def media(a) # MÉDIA E MEDIANA
mid = a.length / 2
sorted = a.sort
return [a.inject{ |sum, el| sum + el }.to_f / a.size, a.length.odd? ? sorted[mid] : 0.5 * (sorted[mid] + sorted[mid - 1]), (a.max + a.min) / 2.0]
end
def chart(a, d, p) # HISTÓRICO DO ATIVO
print " [Importing chart of ", d, " days... " if p == true
response = apidata("https://coingecko.p.rapidapi.com/coins/" + a + "/market_chart?vs_currency=usd&days=" + d.to_s)
if eval(response.read_body)[:prices] == nil || eval(response.read_body)[:prices] == "" || response.read_body.downcase.include?("invalid") || response.read_body.downcase.include?("could not find coin")
print "\n\nERROR: ASSET NOT FOUND!\n" # erro em caso de retorno vazio
$error.debug(response.read_body) # escrevendo a resposta da API no log de erros
gets
exit
end
hist = {:prices => [], :caps => [], :vols => []}
for n in 0..eval(response.read_body)[:prices].length-1 do
hist[:prices][n] = eval(response.read_body)[:prices][n][1]
hist[:caps][n] = eval(response.read_body)[:market_caps][n][1]
hist[:vols][n] = eval(response.read_body)[:total_volumes][n][1]
end
print hist[:prices].length, " inputs]\n" if p == true
return hist
end
def ava(s, b, o) # AVALIAÇÃO DE SCORE
if o == 1 # ordem crescente
if s < b * (1/5.0)
x = "terrible!"
elsif s < b * (2/5.0)
x = "bad "
elsif s < b * (3/5.0)
x = "reasonable"
elsif s < b * (4/5.0)
x = "good"
else
x = "excellent!"
end
else # ordem decrescente
if s < b * (1/5.0)
x = "excellent!"
elsif s < b * (2/5.0)
x = "good"
elsif s < b * (3/5.0)
x = "reasonable"
elsif s < b * (4/5.0)
x = "bad"
else
x = "terrible!"
end
end
return x
end