Class: QuickTicker::Cli

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

Direct Known Subclasses

DevCli, MainCli

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli = nil) ⇒ Cli

Returns a new instance of Cli.



7
8
9
# File 'lib/cli.rb', line 7

def initialize(cli = nil)
	self.scraper = QuickTicker::Scraper.new(self)
end

Instance Attribute Details

#exit_messageObject

Returns the value of attribute exit_message.



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

def exit_message
  @exit_message
end

#last_option_lambdaObject

Returns the value of attribute last_option_lambda.



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

def last_option_lambda
  @last_option_lambda
end

#scraperObject

Returns the value of attribute scraper.



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

def scraper
  @scraper
end

#stockObject

Returns the value of attribute stock.



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

def stock
  @stock
end

Instance Method Details

#call_stock_option_menu(option_hash) ⇒ Object



64
65
66
# File 'lib/cli.rb', line 64

def call_stock_option_menu(option_hash)
	process_stock_option_menu_input(display_stock_option_menu(option_hash[:option_1_string], option_hash[:option_2_string]), option_hash[:option_1_lambda], option_hash[:option_2_lambda], option_hash[:last_option_lambda])
end

#display_stock_descriptionObject



30
31
32
33
# File 'lib/cli.rb', line 30

def display_stock_description
	print "#{self.stock.description.sector} : #{self.stock.description.industry}\n\n"
	puts "#{self.stock.description.summary}".fit
end

#display_stock_headerObject



16
17
18
# File 'lib/cli.rb', line 16

def display_stock_header
	print "\n#{self.stock.name} (#{self.stock.exchange}:#{self.stock.symbol})\n\n"
end

#display_stock_option_menu(option_1_string, option_2_string) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/cli.rb', line 68

def display_stock_option_menu(option_1_string, option_2_string)
	gets
	puts "1. #{option_1_string} for #{self.stock.symbol}."
	puts "2. #{option_2_string} for #{self.stock.symbol}."
	puts "3. Enter another ticker symbol."
	puts "Enter any other key to exit."
	gets.strip.gsub('.', '')
end

#display_stock_quoteObject



20
21
22
23
24
25
26
27
28
# File 'lib/cli.rb', line 20

def display_stock_quote
	puts "Current:  #{self.stock.quote.price} #{stock.quote.change}(#{stock.quote.change_pct}%)"
	puts "Open:     #{self.stock.quote.open}"
	puts "Volume:   #{self.stock.quote.volume}"
	puts "Avg Vol:  #{self.stock.quote.volume_avg}"
	puts "Mkt Cap:  #{self.stock.quote.mkt_cap}"
	puts "P/E(ttm): #{self.stock.quote.pe_ttm}"
	puts "Yield:    #{self.stock.quote.div_yld}%"
end


35
36
37
38
39
40
41
42
43
44
# File 'lib/cli.rb', line 35

def display_stock_related_companies
	output = "Symbol  Price                   Mkt Cap\n"
	self.stock.related_companies.each do |stock_related_company|
		output += (stock_related_company.symbol + ' ' * 8)[0,8]
		output += (stock_related_company.price + ' ' * 8)[0,8]
		output += ("#{stock_related_company.change}(#{stock_related_company.change_pct}%)" + ' ' * 16)[0,16]
		output += stock_related_company.mkt_cap + "\n"
	end
	puts output
end

#fetch_stock_descriptionObject



52
53
54
55
56
# File 'lib/cli.rb', line 52

def fetch_stock_description
	self.display_stock_header
	self.display_stock_description
	self.call_stock_option_menu({ option_1_string: "Display a quote", option_2_string: "Display related companies", option_1_lambda: -> { self.fetch_stock_quote }, option_2_lambda: -> { self.fetch_stock_related_companies }, last_option_lambda: -> {self.last_option_lambda.()} })
end

#fetch_stock_quoteObject



46
47
48
49
50
# File 'lib/cli.rb', line 46

def fetch_stock_quote
	self.display_stock_header
	self.display_stock_quote
	self.call_stock_option_menu({ option_1_string: "Display a company description", option_2_string: "Display related companies", option_1_lambda: -> { self.fetch_stock_description }, option_2_lambda: -> { self.fetch_stock_related_companies }, last_option_lambda: -> {self.last_option_lambda.()} })
end


58
59
60
61
62
# File 'lib/cli.rb', line 58

def fetch_stock_related_companies
	self.display_stock_header
	self.display_stock_related_companies
	self.call_stock_option_menu({ option_1_string: "Display a quote", option_2_string: "Display a company description", option_1_lambda: -> { self.fetch_stock_quote }, option_2_lambda: -> { self.fetch_stock_description }, last_option_lambda: -> {self.last_option_lambda.()} })
end

#process_stock_option_menu_input(input, option_1_lambda, option_2_lambda, option_3_lambda) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cli.rb', line 77

def process_stock_option_menu_input(input, option_1_lambda, option_2_lambda, option_3_lambda)
	if input == "1"
		option_1_lambda.()
	elsif input == "2"
		option_2_lambda.()
	elsif input == "3"
		option_3_lambda.()
	else
		puts self.exit_message
		return nil
	end
end

#symbol_validation(symbol, fixture_url = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cli.rb', line 90

def symbol_validation(symbol, fixture_url = nil)
	# stock_array[0] is a stock if one was succesfully created and nil otherwise.
	# stock_array[1] indicates whether the symbol cooresponds to a mutual fund.
	stock_array = self.scraper.load_gfs(symbol, fixture_url)
	self.stock = stock_array[0]
	valid = self.stock.nil? ? false : true
	puts "Invalid ticker symbol." if !valid
	puts "Mutual funds are not currently not supported." if stock_array[1]
	self.fetch_stock_quote if valid
	# returns an array.
	# array[0] - whether the entered symbol was valid
	# array[1] = stock_array[1] - whether the entered
	# symbol was a mutal fund.
	[valid, stock_array[1]]
end

#welcome(mode_name, mode_lambda) ⇒ Object



11
12
13
14
# File 'lib/cli.rb', line 11

def welcome(mode_name, mode_lambda)
	print "\nWelcome to " + mode_name + "!\n"
	mode_lambda.()
end