My First Sinatra App - Tracking US Debt and Bitcoin Price

in OCD4 years ago

image.png

So, I was playing around with Sinatra and came up with this little guy.

A Trillion Reasons

This little project just calls the CoinGecko API for the BTC data and a Treasury API for the U.S. debt.

I used Bootstrap for the front-end and I wrote the HTML in Slim-Lang which I am actually loving quite a bit. I tried to use UIkit but I ran into some issues with Slim. I'm new to Slim, so I could have just been doing something wrong. But, it's worked beautifully with Bootstrap.

I plan to turn this into a little bit of a resource for people new to Bitcoin and crypto, but this is what I have done so far.

def self.check
    assets = self.all

    assets.collect do |a|
      minute_check = Time.now.min - a.updated_at.min
      if minute_check.abs >= 10
        if a.name == "Bitcoin"
          url = a.api_url
          response = RestClient.get(url)
          api = JSON.parse(response)

          a.price = api["market_data"]["current_price"]["usd"].to_s
          a.circulating_supply = api["market_data"]["circulating_supply"]
          a.total_supply = api["market_data"]["total_supply"]
          a.updated_at = Time.now.utc
          a.save
        elsif a.name == "United States Dollar"
          url = 'https://www.treasurydirect.gov/NP_WS/debt/current.json'
          response = RestClient.get(url)
          api = JSON.parse(response)
          
          a.total_debt = api["totalDebt"].to_i
          a.updated_at = Time.now.utc
          a.save
        end
      end
    end
  end

I'm also running a minute check so this only updates every 10 minutes. I don't need to burn through a ton of API calls, so I figured this was a smart idea.

I'll be making a video about it and the code will be on GitHub once I'm finished. But, here's a little preview.