Sha256: 7dc1347ddccf16e92ccbab5d416c31701675655fb9b84a07202aa0b7d34c0f51

Contents?: true

Size: 1.46 KB

Versions: 4

Compression:

Stored size: 1.46 KB

Contents

require 'console_table'

require 'json'
require 'net/http'
require 'open-uri'
require 'colorize'

symbols = ["YHOO", "AAPL", "GOOG", "MSFT", "C", "MMM", "KO", "WMT", "GM", "IBM", "MCD", "VZ", "HD", "DIS", "INTC"]

params = symbols.collect{|s| "\"#{s}\"" }.join(",")
url = "http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quotes where symbol in (#{params})&env=http://datatables.org/alltables.env&format=json"
uri = URI.parse(URI::encode(url))
response = Net::HTTP.get_response(uri)
json = JSON.parse(response.body)

table_config = [
    {:key=>:symbol, :title=>"Symbol", :size=>6},
    {:key=>:name, :title=>"Name", :size=>17},
    {:key=>:price, :title=>"Price", :size=>5, :justify=>:right},
    {:key=>:change, :title=>"Change", :size=>7, :justify=>:right},
    {:key=>:recommendation, :title=>"Recommendation", :size=>15, :justify=>:right}
]

ConsoleTable.define(table_config, :title=>"Stock Prices") do |table|

    json["query"]["results"]["quote"].each do |j|
        change = j["ChangeRealtime"]
        if change.start_with?("+")
            change = change.green
        else
            change = change.red
        end

        recommendation = (rand() <= 0.5) ? "BUY!".white.on_green.bold.underline : "Sell".yellow

        table << [
            j["Symbol"].magenta,
            j["Name"],
            j["LastTradePriceOnly"],
            change,
            recommendation
        ]

    end

    table.footer << "Recommendations randomly generated"

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
console_table-0.3.1 examples/stocks.rb
console_table-0.3.0 examples/stocks.rb
console_table-0.2.4 examples/stocks.rb
console_table-0.2.3 examples/stocks.rb