class YahooFinance::BaseQuote

Public Class Methods

new( hash, valarray=nil ) click to toggle source
# File lib/assets/yfinadaptor/yfinadaptor.rb, line 250
def initialize( hash, valarray=nil )
  @formathash = hash
  @formathash.each_key { |elem|
    # Create a getter method for each format element.
    instance_eval( "def #{@formathash[elem][0]}() " +
                     "@#{@formathash[elem][0]} " +
                     "end" )
    # Create a setter method for each format element.
    instance_eval( "def #{@formathash[elem][0]}=(val) " +
                     "@#{@formathash[elem][0]}=#{@formathash[elem][1]} " +
                     "end" )
  }

  parse( valarray ) if valarray

end

Public Instance Methods

get_info() click to toggle source
# File lib/assets/yfinadaptor/yfinadaptor.rb, line 278
def get_info()
  "#{symbol} : #{name}"
end
load_quote( symbol ) click to toggle source
# File lib/assets/yfinadaptor/yfinadaptor.rb, line 267
def load_quote( symbol )
  csv = YahooFinance.get( symbol, @formathash.keys.join )
  parse( CSV.parse_line( csv ) )
end
to_s() click to toggle source

Val A. Red: wrote a new #to_s function that replaces the string writing with a hash containing all the fields necessary for

API use within the website.
# File lib/assets/yfinadaptor/yfinadaptor.rb, line 283
def to_s()
  ret = Hash.new("rethash")
  #ret << self.class.name << "\n"
  @formathash.each_value { |val|
    #Val A. Red: The below IF statement ORs all monetary values and converts them to
    #           floats, multiplying them by 100 and then converting
    #           them into integers (i.e. cents) to be compatible with the Money module
    if (val[0]=="lastTrade" || val[0]=="changePoints" || val[0]=="previousClose" || val[0]=="open" || val[0]=="dayHigh" || val[0]=="dayLow" || val[0]=="bid" || val[0]=="ask" || val[0]=="weeks52ChangeFromLow" || val[0]=="weeks52ChangeFromHigh" || val[0]=="earningsPerShare" || val[0]=="bookValue" || val[0]=="pricePerBook" || val[0]=="pricePerSales" )
      ret.store(val[0], (send( val[0] ).to_f*100).to_int)
    elsif (val[0]=="changePercent" || val[0]=="shortRatio" || val[0]=="pegRatio" || val[0]=="peRatio")
      ret.store(val[0], (send( val[0] ).to_f))
    else
      ret.store(val[0], send( val[0] ).to_s)
    end
    #ret << "\n"
  }
  puts ret["symbol"]
  puts ret["name"]
  puts ret["lastTrade"]
  return ret
end
to_s_old() click to toggle source
# File lib/assets/yfinadaptor/yfinadaptor.rb, line 305
def to_s_old()
  ret = String.new
  ret << self.class.name << "\n"
  @formathash.each_value { |val|
    ret << "#{val[0]} = "
    ret << send( val[0] ).to_s unless send( val[0] ) == nil
    ret << "\n"
  }
  return ret
end
valid?() click to toggle source
# File lib/assets/yfinadaptor/yfinadaptor.rb, line 272
def valid?()
  # Not sure this is the best way to do this, but OK for now.
  return self.name != self.symbol if self.name
  false
end

Protected Instance Methods

convert( value ) click to toggle source
# File lib/assets/yfinadaptor/yfinadaptor.rb, line 331
def convert( value )
  if ( value == "N/A" )
    return value
  elsif ( value =~ /.*\..*B/ )
    return value
  else
    return value
  end
end
parse( results ) click to toggle source
# File lib/assets/yfinadaptor/yfinadaptor.rb, line 318
def parse( results )
  begin
    ctr = 0
    results.each { |elem|
      # Call the setter method for this element.
      send "#{@formathash[@formathash.keys[ctr]][0]}=", elem
      ctr += 1
    }
  rescue 
    puts "yfparse:#{$!}"
  end
end