Last active
August 29, 2015 14:02
-
-
Save elikem/7344fe6147a3183f20aa to your computer and use it in GitHub Desktop.
Sinatra Demo App -- Bitcoin Prices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "sinatra" | |
| require 'mechanize' | |
| require 'json' | |
| require 'hashie' | |
| require 'bigdecimal' | |
| require "sqlite3" | |
| require "active_record" | |
| ActiveRecord::Base.establish_connection( | |
| adapter: 'sqlite3', | |
| database: 'database.db') | |
| API_URL = 'http://www.cryptocoincharts.info/v2/api' | |
| ActiveRecord::Schema.define do | |
| unless ActiveRecord::Base.connection.tables.include? 'pricings' | |
| create_table :pricings do |t| | |
| t.column :coin, :string | |
| t.column :price, :decimal | |
| t.column :price_before_24h, :decimal | |
| t.column :volume_first, :decimal | |
| t.column :volume_second, :decimal | |
| t.column :volume_btc, :decimal | |
| t.column :best_market, :string | |
| end | |
| end | |
| end | |
| class Pricings < ActiveRecord::Base | |
| end | |
| get "/" do | |
| response = JSON.parse(Mechanize.new.post( | |
| "#{API_URL}/tradingPairs", | |
| {:pairs => "btc_usd,doge_usd,drk_usd" }).body | |
| ) | |
| last_responses = [] | |
| response.each {|pricing| last_responses << Pricings.create({coin: pricing["id"], | |
| price: pricing["price"], | |
| price_before_24h: pricing["price_before_24h"], | |
| volume_first: pricing["volume_first"], | |
| volume_second: pricing["volume_second"], | |
| volume_btc: pricing["volume_btc"], | |
| best_market: pricing["best_market"]} | |
| )} | |
| html = '<!doctype html> | |
| <head> | |
| <meta http-equiv="refresh" content="60"> | |
| </head> | |
| <body> | |
| ' | |
| last_responses | |
| .each do |pricing| | |
| html += "<div id=#{pricing.id}>#{pricing.id}. #{pricing.coin} -- #{pricing.price}</div>" | |
| end | |
| html += "</body>" | |
| html | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment