Sha256: 28ecb7dbb434b43dd0582fa2acc24d0b9deccc279ca15be0d780d23e3d83a722

Contents?: true

Size: 1.1 KB

Versions: 5

Compression:

Stored size: 1.1 KB

Contents

class StockGains::Stock
  attr_accessor :name, :cur_price, :prev_close, :open, :year_trgt
  attr_accessor :d_range, :y_range,:days_value, :pe, :eps, :shares

  def initialize(name, cur_price, prev_close, open, year_trgt, d_range, y_range, pe, eps, shares)
    @name = name
    @cur_price = cur_price
    @prev_close = prev_close
    @open = open
    @year_trgt = year_trgt
    @d_range = d_range
    @y_range = y_range
    @pe = pe
    @eps = eps
    @shares = shares
    calculate_days_value
  end

  def self.all 
    @@all ||= create_stock_from_portfolio
  end

  def calculate_days_value
    @days_value = ((cur_price.to_f * shares.to_f) - (prev_close.to_f * shares.to_f)).round(2).to_f 
  end

  def self.create_stock_from_portfolio
    CSV.foreach("portfolio.csv").collect do |stock|
      s = (retrieve_stock(stock) << stock[1]).flatten
      new(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[8], s[9])
    end
  end

  def self.retrieve_stock(stock)
    url = "http://finance.yahoo.com/d/quotes.csv?s=#{stock.first}&f=napot8mwre"
    open(url) do |csv|
      CSV.parse(csv).collect{ |row| row }
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
stock-gains-0.1.8 lib/stock-gains/stock.rb
stock-gains-0.1.7 lib/stock-gains/stock.rb
stock-gains-0.1.6 lib/stock-gains/stock.rb
stock-gains-0.1.5 lib/stock-gains/stock.rb
stock-gains-0.1.3 lib/stock-gains/stock.rb