Class: PaBillreader::CLI

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

Constant Summary collapse

@@viewed_bills =
[]

Instance Method Summary collapse

Instance Method Details

#callObject



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/pa_billreader/cli.rb', line 5

def call
	puts "-------------------------------"
	puts "Welcome to the PA bill reader!"
	puts "-------------------------------"
	puts "Loading bills now..."
	puts "-------------------------------"
	create_bills
	# binding.pry
	menu
	goodbye
end

#create_bill_details_allObject



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

def create_bill_details_all
	PaBillreader::Bill.all.each {|bill|
		attributes = PaBillreader::Scraper.scrape_bill_detail(bill.branch, bill.number)
		bill.add_bill_attributes(attributes)
	}
end

#create_bill_details_single(bill) ⇒ Object



35
36
37
38
# File 'lib/pa_billreader/cli.rb', line 35

def create_bill_details_single(bill)
	attributes = PaBillreader::Scraper.scrape_bill_detail(bill.branch, bill.number)
	bill.add_bill_attributes(attributes)
end

#create_billsObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/pa_billreader/cli.rb', line 17

def create_bills
	PaBillreader::Scraper.scrape_bill_nums("H") #house bills
	
	puts "There are currently #{PaBillreader::Bill.house_bills.size} bills in the House"

	PaBillreader::Scraper.scrape_bill_nums("S") #senate bills
	
	puts "There are currently #{PaBillreader::Bill.senate_bills.size} bills in the Senate"

end

#display_bill_info(bill) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/pa_billreader/cli.rb', line 103

def display_bill_info(bill)
	puts "#{display_branch(bill.branch)} Bill Number #{bill.number}: #{bill.short_title}"
	puts "Primary Sponsor: #{bill.prime_sponsor}"
	puts "Last Action: #{bill.last_action}"
	puts "Type 'Open Memo' to open memo in browser." if bill.memo_url
	puts "Type 'Open Full' to open full bill text in browser." if bill.full_text_url
end

#display_branch(abbrev) ⇒ Object



123
124
125
126
# File 'lib/pa_billreader/cli.rb', line 123

def display_branch(abbrev)
	return "Senate" if abbrev == "S"
	return "House" if abbrev == "H"
end

#get_valid_bill_numObject



83
84
85
86
87
88
89
90
91
# File 'lib/pa_billreader/cli.rb', line 83

def get_valid_bill_num
	input_num = "0"
	while !input_num.to_i.between?(1, PaBillreader::Bill.size)
		puts "Enter the number of the bill you'd like to view"
		puts "-------------------------------"
		input_num = gets.strip
	end
	input_num
end

#get_valid_branchObject



93
94
95
96
97
98
99
100
101
# File 'lib/pa_billreader/cli.rb', line 93

def get_valid_branch
	input_branch = ""
	while input_branch != "S" && input_branch != "H"
		puts "Would you like to see the senate or house bill?"
		puts "-------------------------------"
		input_branch = gets.strip.upcase[0,1]
	end
	input_branch
end

#goodbyeObject



132
133
134
# File 'lib/pa_billreader/cli.rb', line 132

def goodbye
	puts "See you later for more bills bills bills!"
end

#list_billsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pa_billreader/cli.rb', line 40

def list_bills
	puts "Here are all the current bills for Regular Session 2017-2018"
	puts "-------------------------------"
	puts "This may take a while..."
	puts "-------------------------------"
	create_bill_details_all
	PaBillreader::Bill.sorted!
	PaBillreader::Bill.all.each {|bill|
		puts bill.number
		puts bill.short_title
	}
	puts "-------------------------------"
end


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pa_billreader/cli.rb', line 54

def menu
	input = nil
	while input != "exit"
		puts "would you like to VIEW a bill, LIST all bills, or EXIT?"
		puts "-------------------------------"
		input = gets.downcase.strip
		if input == "view"
			input_num = get_valid_bill_num
			input_branch = get_valid_branch
			bill_to_find = PaBillreader::Bill.find_by_number(input_num, input_branch)
			@@viewed_bills << bill_to_find
			create_bill_details_single(bill_to_find) unless bill_to_find == nil
			display_bill_info(bill_to_find) unless bill_to_find == nil
			puts "That is not a valid bill" if bill_to_find == nil
		elsif input == "list"
			list_bills
		elsif input == "open memo"
			open_memo(bill_to_find)
		elsif input == "open full"
			open_full(bill_to_find)
		elsif input == "history"
			# list bills
			print_history
		else
			puts "Not sure what you want, type view, list or exit"
		end
	end
end

#open_full(bill) ⇒ Object



115
116
117
# File 'lib/pa_billreader/cli.rb', line 115

def open_full(bill)
	open_in_browser(bill.full_text_url)
end

#open_in_browser(url) ⇒ Object



119
120
121
# File 'lib/pa_billreader/cli.rb', line 119

def open_in_browser(url)
	system("open '#{url}'")
end

#open_memo(bill) ⇒ Object



111
112
113
# File 'lib/pa_billreader/cli.rb', line 111

def open_memo(bill)
	open_in_browser(bill.memo_url)
end


128
129
130
# File 'lib/pa_billreader/cli.rb', line 128

def print_history
	@@viewed_bills.each {|bill| display_bill_info(bill)}
end