Class: Ytterb::StockSymbol::CacheBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ytterb/stock_symbol/cache_builder.rb

Instance Method Summary collapse

Constructor Details

#initializeCacheBuilder

Returns a new instance of CacheBuilder.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ytterb/stock_symbol/cache_builder.rb', line 12

def initialize
  @sml = MarketLoader.new      
  @local_settings = Util::Settings.new

  # build the queue used when syncing
  build_stock_sync_queue

  # run the processor that fetches and persists stock info
  stock_processor_run

  @local_settings.save
end

Instance Method Details

#build_stock_sync_queueObject



25
26
27
28
29
30
# File 'lib/ytterb/stock_symbol/cache_builder.rb', line 25

def build_stock_sync_queue
  @stock_sync_queue = Queue.new
  @sml.stock_symbols.shuffle.each do |stock|
    @stock_sync_queue << stock
  end
end

#stock_processor_runObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ytterb/stock_symbol/cache_builder.rb', line 32

def stock_processor_run
  initial_queue_size = @stock_sync_queue.length
  prev_done = 0
  puts "Initial queue size is #{initial_queue_size}"
  while true
    begin
      curr = @stock_sync_queue.pop(true) rescue nil
      break unless curr # queue is empty
      sleep(3600.0/@local_settings[:api_calls_per_hour])
      curr.fetch_history
      curr_done = (initial_queue_size - @stock_sync_queue.length) * 100 / initial_queue_size
      if curr_done > prev_done
        puts "#{curr_done} %"
        prev_done = curr_done
      else
        print "#{curr.symbol}."
      end
    rescue StandardError => e
      puts "#{e.message} : #{e.backtrace.join("|")}"
    end
  end
end