Class: CoinTools::Cryptowatch
- Inherits:
-
Object
- Object
- CoinTools::Cryptowatch
show all
- Defined in:
- lib/cointools/cryptowatch.rb
Defined Under Namespace
Classes: BadRequestException, DataPoint, InvalidDateException, InvalidResponseException, NoDataException
Constant Summary
collapse
- BASE_URL =
"https://api.cryptowat.ch"
- USER_AGENT =
"cointools/#{CoinTools::VERSION}"
- DAYS_FOR_PERIODS =
we expect this many days worth of data for a given period precision (in seconds); NOT guaranteed by the API
{
60 => 3, 180 => 10, 300 => 15, 900 => 2 * 30, 1800 => 4 * 30, 3600 => 8 * 30,
7200 => 365, 14400 => 1.5 * 365, 21600 => 2 * 365, 43200 => 3 * 365, 86400 => 4 * 365
}
Instance Method Summary
collapse
Instance Method Details
#exchanges ⇒ Object
38
39
40
|
# File 'lib/cointools/cryptowatch.rb', line 38
def exchanges
@exchanges ||= get_exchanges
end
|
#get_current_price(exchange, market) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/cointools/cryptowatch.rb', line 88
def get_current_price(exchange, market)
url = URI("#{BASE_URL}/markets/#{exchange}/#{market}/price")
response = make_request(url)
case response
when Net::HTTPSuccess
json = JSON.load(response.body)
price = json['result']['price']
allowance = json['allowance']
return DataPoint.new(price, nil, allowance['cost'], allowance['remaining'])
when Net::HTTPBadRequest
raise BadRequestException.new(response)
else
raise InvalidResponseException.new(response)
end
end
|
#get_markets(exchange) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/cointools/cryptowatch.rb', line 42
def get_markets(exchange)
url = URI("#{BASE_URL}/markets/#{exchange}")
response = make_request(url)
case response
when Net::HTTPSuccess
json = JSON.load(response.body)
return json['result'].select { |m| m['active'] == true }.map { |m| m['pair'] }.sort
when Net::HTTPBadRequest
raise BadRequestException.new(response)
else
raise InvalidResponseException.new(response)
end
end
|
#get_price(exchange, market, time = nil) ⇒ Object
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
|
# File 'lib/cointools/cryptowatch.rb', line 58
def get_price(exchange, market, time = nil)
return get_current_price(exchange, market) if time.nil?
(time <= Time.now) or raise InvalidDateException.new('Future date was passed')
(time.year >= 2009) or raise InvalidDateException.new('Too early date was passed')
unixtime = time.to_i
current_time = Time.now.to_i
url = URI("#{BASE_URL}/markets/#{exchange}/#{market}/ohlc?after=#{unixtime}")
response = make_request(url)
case response
when Net::HTTPSuccess
json = JSON.load(response.body)
data = json['result']
allowance = json['allowance']
timestamp, o, h, l, c, volume = best_matching_record(data, unixtime, current_time)
raise NoDataException.new('No data found for a given time') if timestamp.nil?
actual_time = Time.at(timestamp)
return DataPoint.new(o, actual_time, allowance['cost'], allowance['remaining'])
when Net::HTTPBadRequest
raise BadRequestException.new(response)
else
raise InvalidResponseException.new(response)
end
end
|
#get_price_fast(exchange, market, time) ⇒ Object
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
|
# File 'lib/cointools/cryptowatch.rb', line 107
def get_price_fast(exchange, market, time)
(time <= Time.now) or raise InvalidDateException.new('Future date was passed')
(time.year >= 2009) or raise InvalidDateException.new('Too early date was passed')
period = precision_for_time(time)
if period.nil?
return get_price(exchange, market, time)
end
unixtime = time.to_i
current_time = Time.now.to_i
url = URI("#{BASE_URL}/markets/#{exchange}/#{market}/ohlc?after=#{unixtime}&periods=#{period}")
response = make_request(url)
case response
when Net::HTTPSuccess
json = JSON.load(response.body)
data = json['result']
allowance = json['allowance']
timestamp, o, h, l, c, volume = best_matching_record(data, unixtime, current_time)
raise NoDataException.new('No data found for a given time') if timestamp.nil?
actual_time = Time.at(timestamp)
return DataPoint.new(o, actual_time, allowance['cost'], allowance['remaining'])
when Net::HTTPBadRequest
raise BadRequestException.new(response)
else
raise InvalidResponseException.new(response)
end
end
|