Class: CoinTools::Cryptowatch

Inherits:
Object
  • Object
show all
Defined in:
lib/cointools/cryptowatch.rb

Constant Summary collapse

BASE_URL =
"https://api.cryptowat.ch"
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
}
DataPoint =
BaseStruct.make(:price, :time, :api_time_spent, :api_time_remaining)

Instance Method Summary collapse

Instance Method Details

#exchangesObject



22
23
24
# File 'lib/cointools/cryptowatch.rb', line 22

def exchanges
  @exchanges ||= get_exchanges
end

#get_current_price(exchange, market) ⇒ Object



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
# File 'lib/cointools/cryptowatch.rb', line 96

def get_current_price(exchange, market)
  raise InvalidExchangeError if exchange.to_s.empty?
  raise InvalidSymbolError if market.to_s.empty?

  url = URI("#{BASE_URL}/markets/#{exchange}/#{market}/price")

  response = Request.get(url)

  case response
  when Net::HTTPSuccess
    json = Utils.parse_json(response.body)
    raise JSONError.new(response) unless json.is_a?(Hash)

    data = json['result']
    allowance = json['allowance']
    raise JSONError.new(response) unless data.is_a?(Hash) && allowance.is_a?(Hash)

    price = data['price']
    raise NoDataError.new(response) unless price

    return DataPoint.new(
      price: price,
      time: nil,
      api_time_spent: allowance['cost'],
      api_time_remaining: allowance['remaining']
    )
  when Net::HTTPNotFound
    raise UnknownCoinError.new(response)
  when Net::HTTPClientError
    raise BadRequestError.new(response)
  else
    raise ServiceUnavailableError.new(response)
  end
end

#get_markets(exchange) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cointools/cryptowatch.rb', line 26

def get_markets(exchange)
  raise InvalidExchangeError if exchange.to_s.empty?

  url = URI("#{BASE_URL}/markets/#{exchange}")

  response = Request.get(url)

  case response
  when Net::HTTPSuccess
    json = Utils.parse_json(response.body)
    raise JSONError.new(response) unless json.is_a?(Hash) && json['result'].is_a?(Array)

    return json['result'].select { |m| m['active'] == true }.map { |m| m['pair'] }.sort
  when Net::HTTPNotFound
    raise UnknownExchangeError.new(response)
  when Net::HTTPClientError
    raise BadRequestError.new(response)
  else
    raise ServiceUnavailableError.new(response)
  end
end

#get_price(exchange, market, time = nil) ⇒ Object



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
# File 'lib/cointools/cryptowatch.rb', line 48

def get_price(exchange, market, time = nil)
  raise InvalidExchangeError if exchange.to_s.empty?
  raise InvalidSymbolError if market.to_s.empty?

  if time.nil?
    return get_current_price(exchange, market)
  elsif time.is_a?(String)
    time = Utils.parse_time(time)
  end

  (time <= Time.now) or raise InvalidDateError.new('Future date was passed')
  (time.year >= 2009) or raise InvalidDateError.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 = Request.get(url)

  case response
  when Net::HTTPSuccess
    json = Utils.parse_json(response.body)
    raise JSONError.new(response) unless json.is_a?(Hash)

    data = json['result']
    allowance = json['allowance']
    raise JSONError.new(response) unless data.is_a?(Hash) && allowance.is_a?(Hash)

    timestamp, o, h, l, c, volume = best_matching_record(data, unixtime, current_time)
    raise NoDataError.new(response, 'No price data returned') unless timestamp && o

    actual_time = Time.at(timestamp)

    return DataPoint.new(
      price: o,
      time: actual_time,
      api_time_spent: allowance['cost'],
      api_time_remaining: allowance['remaining']
    )
  when Net::HTTPNotFound
    raise UnknownCoinError.new(response)
  when Net::HTTPClientError
    raise BadRequestError.new(response)
  else
    raise ServiceUnavailableError.new(response)
  end
end

#get_price_fast(exchange, market, time = nil) ⇒ Object



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
# File 'lib/cointools/cryptowatch.rb', line 131

def get_price_fast(exchange, market, time = nil)
  raise InvalidExchangeError if exchange.to_s.empty?
  raise InvalidSymbolError if market.to_s.empty?

  if time.nil?
    return get_current_price(exchange, market)
  elsif time.is_a?(String)
    time = Utils.parse_time(time)
  end

  (time <= Time.now) or raise InvalidDateError.new('Future date was passed')
  (time.year >= 2009) or raise InvalidDateError.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 = Request.get(url)

  case response
  when Net::HTTPSuccess
    json = Utils.parse_json(response.body)
    raise JSONError.new(response) unless json.is_a?(Hash)

    data = json['result']
    allowance = json['allowance']
    raise JSONError.new(response) unless data.is_a?(Hash) && allowance.is_a?(Hash)

    timestamp, o, h, l, c, volume = best_matching_record(data, unixtime, current_time)
    raise NoDataError.new(response, 'No price data returned') unless timestamp && o

    actual_time = Time.at(timestamp)

    return DataPoint.new(
      price: o,
      time: actual_time,
      api_time_spent: allowance['cost'],
      api_time_remaining: allowance['remaining']
    )
  when Net::HTTPNotFound
    raise UnknownCoinError.new(response)
  when Net::HTTPClientError
    raise BadRequestError.new(response)
  else
    raise ServiceUnavailableError.new(response)
  end
end