Browse Source

Made crypto much better

master
Stephen 2 months ago
parent
commit
8eb3ff6a65
5 changed files with 42 additions and 9 deletions
  1. +1
    -0
      Gemfile
  2. +5
    -0
      Gemfile.lock
  3. +22
    -4
      app/controllers/root_controller.rb
  4. +12
    -2
      app/helpers/market.rb
  5. +2
    -3
      public/index.html

+ 1
- 0
Gemfile View File

@@ -54,5 +54,6 @@ end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem 'iex-ruby-client'
gem 'cryptocompare'

gem 'statsd-instrument'

+ 5
- 0
Gemfile.lock View File

@@ -74,6 +74,9 @@ GEM
childprocess (3.0.0)
concurrent-ruby (1.1.6)
crass (1.0.6)
cryptocompare (0.14.0)
faraday
json
erubi (1.9.0)
faraday (0.17.3)
multipart-post (>= 1.2, < 3)
@@ -92,6 +95,7 @@ GEM
money_helper
jbuilder (2.10.0)
activesupport (>= 5.0.0)
json (2.3.0)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
@@ -220,6 +224,7 @@ DEPENDENCIES
bootsnap (>= 1.4.2)
byebug
capybara (>= 2.15)
cryptocompare
iex-ruby-client
jbuilder (~> 2.7)
listen (>= 3.0.5, < 3.2)


+ 22
- 4
app/controllers/root_controller.rb View File

@@ -30,10 +30,28 @@ class RootController < ApplicationController
end

def crypto
fiat = ERB::Util.url_encode params[:fiat]
crypt = ERB::Util.url_encode params[:crypto]
info = Market.get "#{crypt}#{fiat}T"
render body: "#{crypt}: #{number_to_currency(info.latest_price)} #{fiat}\n"
StatsD.increment("crypto.hit")
crypt = params[:crypto].upcase
fiat = params[:fiat].upcase
info = Market.crypto(fiat, crypt)

render body: "Unknown query" and return if info.nil?
delta_s = info["CHANGEPCT24HOUR"]
delta = delta_s.to_d
delta_s = "+" + delta_s if delta >= 0
s = "#{crypt}: #{info["PRICE"]} #{delta_s}%\n"
if params[:f] == "i3"
ticker_i3(s, delta)
return
end

if delta > 0
s = green s
elsif delta < 0
s = red s
end
render body: s
end

private


+ 12
- 2
app/helpers/market.rb View File

@@ -1,5 +1,5 @@
class Market
@client = IEX::Api::Client.new
@@client = IEX::Api::Client.new
def self.get(ticker)
ticker.upcase!
a = Rails.cache.fetch("get_ticker_#{ticker}",
@@ -7,11 +7,21 @@ class Market
puts "Cache miss"
StatsD.measure("iex.lookup_time") do
begin
@client.quote(ticker)
@@client.quote(ticker)
rescue IEX::Errors::SymbolNotFoundError => e
nil
end
end
end
end

def self.crypto(fiat, crypt)
return nil unless fiat =~ /\A\p{Alnum}+\z/
return nil unless crypt =~ /\A\p{Alnum}+\z/
Rails.cache.fetch("get_crypto_#{fiat}#{crypt}",
expires_in: 5.minutes) do
puts "Crypto cache miss"
Cryptocompare::Price.full(crypt, fiat).dig("DISPLAY", crypt, fiat)
end
end
end

+ 2
- 3
public/index.html View File

@@ -8,9 +8,8 @@
$ curl stonks.scd31.com/TSLA<br>
<span style="color: #FF0000">TSLA: $546.62 -2.48%</span><br>
<br>
$ curl stonks.scd31.com/crypto/USD/BTC<br>
<span>BTC: $5,345.21 USD</span><br>
<small>*Note: Only supports USD at the moment</small><br>
$ curl stonks.scd31.com/crypto/CAD/BCH<br>
<span style="color: #00FF00">BCH: CAD 261.76 +0.26%</span><br>
<br>
i3blocks compatibility<br>
<code>


Loading…
Cancel
Save