aggregator/stock.rb
## Hit the alphavantage API for the given symbol
#   to get the current value and movement.
# Global stocks work, but the format is... different.
Hit the alphavantage API for the given symbol
to get the current value and movement.
Global stocks work, but the format is... different.
Hit the alphavantage API for the given symbol to get the current value and movement . Global stocks work but the format is ... different .
def quote
    endpoint = 'https://www.alphavantage.co/'
    query = "query?function=GLOBAL_QUOTE&symbol=#{@current_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[@current_symbol]
    @cache[@current_symbol] = "#{@current_symbol} $#{@value} #{@movement}"
    return unless @cache[@current_symbol] != old_value
    File.open(Stock::CACHE_FILENAME, 'w') do |file|
      file.write(@cache.to_json)
    end
    @cache[@current_symbol]
  end
def quote endpoint = 'https://www.alphavantage.co/' query = "query?function=GLOBAL_QUOTE&symbol=#{@current_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 [ @current_symbol ] @cache [ @current_symbol ] = "#{@current_symbol} $#{@value} #{@movement}" return unless @cache [ @current_symbol ] != old_value File . open ( Stock :: CACHE_FILENAME , 'w' ) do | file | file . write ( @cache . to_json ) end @cache [ @current_symbol ] end
Hit the alphavantage API for the given symbol
to get the current value and movement.
Hit the alphavantage API for the given symbol to get the current value and movement .
# 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