aggregator/stock.rb
# Acts similar to quote, but for specific symbols
# TODO: DRY this up
Acts similar to quote, but for specific symbols
TODO: DRY this up
Acts similar to quote but for specific symbols TODO : DRY this up
def quote_missing_symbol(missing_symbol)
    endpoint = 'https://www.alphavantage.co/'
    query = "query?function=GLOBAL_QUOTE&symbol=#{missing_symbol}&apikey=#{@api}"
    resp = JSON.parse(URI.open("#{endpoint}#{query}").read)['Global Quote']
    if resp.nil?
      @value = Stock::NO_VALUE
      @movement = Stock::NO_CHANGE
    else
      @value = resp['05. price'].to_i
      change = resp['09. change'].to_i
      @movement = if change.positive?
                    Stock::POS_CHANGE
                  elsif change.negative?
                    Stock::NEG_CHANGE
                  elsif change.zero?
                    Stock::NO_CHANGE
                  else
                    Stock::NO_CHANGE
                  end
    end
    old_value = @cache[missing_symbol]
    @cache[missing_symbol] = "#{missing_symbol} $#{@value} #{@movement}"
    return unless @cache[missing_symbol] != old_value
    File.open(Stock::CACHE_FILENAME, 'w') do |file|
      file.write(@cache.to_json)
    end
    @cache[missing_symbol]
  end
def quote_missing_symbol ( missing_symbol ) endpoint = 'https://www.alphavantage.co/' query = "query?function=GLOBAL_QUOTE&symbol=#{missing_symbol}&apikey=#{@api}" resp = JSON . parse ( URI . open ( "#{endpoint}#{query}" ) . read ) [ 'Global Quote' ] if resp . nil? @value = Stock :: NO_VALUE @movement = Stock :: NO_CHANGE else @value = resp [ '05. price' ] . to_i change = resp [ '09. change' ] . to_i @movement = if change . positive? Stock :: POS_CHANGE elsif change . negative? Stock :: NEG_CHANGE elsif change . zero? Stock :: NO_CHANGE else Stock :: NO_CHANGE end end old_value = @cache [ missing_symbol ] @cache [ missing_symbol ] = "#{missing_symbol} $#{@value} #{@movement}" return unless @cache [ missing_symbol ] != old_value File . open ( Stock :: CACHE_FILENAME , 'w' ) do | file | file . write ( @cache . to_json ) end @cache [ missing_symbol ] end
Acts similar to quote, but for specific symbols
TODO: DRY this up
Acts similar to quote but for specific symbols TODO : DRY this up
#### TODO: Break this out into a new function, since #quote uses it, too. Not doing it now for the sake of time. # TODO: DRY this up, make sure it isn't being called too much (see init_cache) # TODO: simple file operations like this are a good candidate for optimization/moving
missing_symbol
<not_specific>