Class: Stockery::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/stockery/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Runner

Returns a new instance of Runner.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/stockery/runner.rb', line 7

def initialize(argv)
  @argv = argv
  
  # Default options values
  @options = {
    :source          => Stockery::GOOGLE,
    :quotes          => '',
    :output          => 'json',
  }

  parse!
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/stockery/runner.rb', line 5

def options
  @options
end

Instance Method Details

#run!Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/stockery/runner.rb', line 20

def run!
  if @options[:quotes].length == 0
    abort "Stock symbols missing. Usage `stockery -q \"GOOG, MSFT\""
  end

  s_q = Stockery::Quote.new
  s_q.source = @options[:source]

  quotes = @options[:quotes].split(',').collect { |q| q.to_s.strip }
  quotes_res = []
  
  quotes.each do |quote|
    quotes_res << s_q.get_status(quote)
  end

  case @options[:output]
  when 'print'
    puts "Quotes at #{Time.now}\n"
    puts ""

    quotes_res.each do |quote|
      puts s_q.print(quote)
      puts "\n"
    end
  when 'json'
    puts JSON.generate(quotes_res)
  end
end