1# frozen_string_literal: true
2
3require "em-http"
4require "em_promise"
5require "em-synchrony/em-http" # For aget vs get
6require "money/bank/open_exchange_rates_bank"
7require "nokogiri"
8
9require_relative "em"
10
11class CryptoSellPrices
12 def initialize(redis, oxr_app_id)
13 @redis = redis
14 @oxr = Money::Bank::OpenExchangeRatesBank.new(
15 Money::RatesStore::Memory.new
16 )
17 @oxr.app_id = oxr_app_id
18 end
19
20 def usd
21 EMPromise.all([cad, cad_to_usd]).then { |(a, b)| a * b }
22 end
23
24 def ticker_row_selector
25 raise NotImplementedError, "Subclass must implement"
26 end
27
28 def crypto_name
29 raise NotImplementedError, "Subclass must implement"
30 end
31
32 def cad
33 fetch_canadianbitcoins.then do |http|
34 cb = Nokogiri::HTML.parse(http.response)
35
36 row = cb.at(self.ticker_row_selector)
37 unless row.at("td").text == self.crypto_name
38 raise "#{crypto_name} row has moved"
39 end
40
41 BigDecimal(
42 row.at("td:nth-of-type(4)").text.match(/^\$(\d+\.\d+)/)[1]
43 )
44 end
45 end
46
47protected
48
49 def fetch_canadianbitcoins
50 EM::HttpRequest.new(
51 "https://www.canadianbitcoins.com",
52 tls: { verify_peer: true }
53 ).aget
54 end
55
56 def cad_to_usd
57 @redis.get("cad_to_usd").then do |rate|
58 next rate.to_f if rate
59
60 EM.promise_defer {
61 # OXR gem is not async, so defer to threadpool
62 @oxr.update_rates
63 @oxr.get_rate("CAD", "USD")
64 }.then do |orate|
65 @redis.setex("cad_to_usd", 60 * 60, orate).then { orate }
66 end
67 end
68 end
69end
70
71class BCHSellPrices < CryptoSellPrices
72 def crypto_name
73 "Bitcoin Cash"
74 end
75
76 def ticker_row_selector
77 "#ticker > table > tbody > tr:nth-of-type(2)"
78 end
79end
80
81class BTCSellPrices < CryptoSellPrices
82 def crypto_name
83 "Bitcoin"
84 end
85
86 def ticker_row_selector
87 "#ticker > table > tbody > tr"
88 end
89end