ruby Sinatra演示应用程序 - 比特币价格

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Sinatra演示应用程序 - 比特币价格相关的知识,希望对你有一定的参考价值。

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

以上是关于ruby Sinatra演示应用程序 - 比特币价格的主要内容,如果未能解决你的问题,请参考以下文章

比特币币价大跌还有必要继续挖矿吗?

ruby 一个示例sinatra omniauth客户端应用程序

如何在 Heroku 上托管的 Ruby/Sinatra 应用程序中设置 HTTP 标头?

ruby 最小的sinatra应用程序与美洲狮的配置。

不再是泡沫:本次币价上涨比2013年更加稳定,入手比特币是明智的投资选择

将 Ruby Sinatra 的 ADAL 示例映射到 Active Directory