diff --git a/app/controllers/moneytags_controller.rb b/app/controllers/moneytags_controller.rb index 3e673e4..20674b1 100644 --- a/app/controllers/moneytags_controller.rb +++ b/app/controllers/moneytags_controller.rb @@ -15,7 +15,15 @@ def show @username = "@" + @user.username @posting = Posting.new @allpostings = Posting.all - @stock ||= @moneytag ? Stock.create(symbol: @moneytag.name.upcase) : Stock.find_by_symbol(params[:moneytag]) + + data = YahooFinance.quotes(["#{@moneytag}"], [:ask, :bid, :low_52_weeks, :high_52_weeks, :name]) + + @stock ||= @moneytag ? Stock.create(symbol: @moneytag.name.upcase, + ask: data[0].ask, + bid: data[0].bid, + year_low: data[0].low_52_weeks, + year_high: data[0].high_52_weeks, + name: data[0].name) : Stock.find_by_symbol(params[:moneytag]) @stocksymbol = "$" + @stock.symbol end @@ -35,39 +43,6 @@ def followers render 'show_follow' end - def create - respond_with Stock.create(stock_params) - end - - def update - respond_with Stock.find(params[:id]).update_attributes(stock_params) - end - - def destroy - respond_with Stock.destroy(params[:id]) - end - - def ohlc - stock = Stock.find(params[:stock_id]) - url = URI::parse "http://ichart.finance.yahoo.com/table.csv?s=" + stock.symbol + "&c=1962" - req = Net::HTTP::get(url).gsub /"/, '' - - csv_format = CSV.parse(req, {converters: :numeric}) - csv_format = csv_format.drop(1) - data = [] - csv_format.reverse.each do |entry| - adjusted_ohlc_factor = entry[6].to_f / entry[4] - data.push([entry[0], - adjusted_ohlc_factor * entry[1], - adjusted_ohlc_factor * entry[2], - adjusted_ohlc_factor * entry[3], - entry[6] - ]) - end - result = { "ohlc" => data } - respond_with result - end - private def stock_params params.require(:stock).permit(:id, :symbol, :name, :bid, :ask, :year_low, :year_high) diff --git a/app/models/stock.rb b/app/models/stock.rb index e635534..79cd065 100644 --- a/app/models/stock.rb +++ b/app/models/stock.rb @@ -26,4 +26,5 @@ def self.find_by_symbol(symbol) Stock.where("symbol =?", symbol).first end + end diff --git a/db/migrate/20140320162543_add_stock_info_to_stocks.rb b/db/migrate/20140320162543_add_stock_info_to_stocks.rb index c196a9d..9700d97 100644 --- a/db/migrate/20140320162543_add_stock_info_to_stocks.rb +++ b/db/migrate/20140320162543_add_stock_info_to_stocks.rb @@ -1,8 +1,8 @@ class AddStockInfoToStocks < ActiveRecord::Migration def change - add_column :stocks, :bid, :decimal - add_column :stocks, :ask, :decimal - add_column :stocks, :year_high, :decimal - add_column :stocks, :year_low, :decimal + add_column :stocks, :bid, :decimal, precision: 4 + add_column :stocks, :ask, :decimal, precision: 4 + add_column :stocks, :year_high, :decimal, precision: 4 + add_column :stocks, :year_low, :decimal, precision: 4 end end diff --git a/db/seeds.rb b/db/seeds.rb index 5f50896..523e6ec 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -12,4 +12,4 @@ Derivative.create!(symbol: "BARC.L", name: 'Barclays') Derivative.create!(symbol: 'TWTR', name: 'Twitter') Derivative.create!(symbol: 'FB', name: 'Facebook') -Derivative.create!(symbol: "SIE.DE", name: "Siemens") \ No newline at end of file +Derivative.create!(symbol: "SIE.DE", name: "Siemens")