forked from rockonla23/ar-sunlight-legislators
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
79 lines (61 loc) · 2.54 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
48
49
50
51
52
53
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
require 'pathname'
require 'sqlite3'
APP_ROOT = Pathname.new(File.dirname(File.expand_path(__FILE__)))
require APP_ROOT.join('app', 'models', 'politician')
require APP_ROOT.join('lib', 'sunlight_legislators_importer')
require APP_ROOT.join('app', 'models', 'senator')
require APP_ROOT.join('app', 'models', 'tweet')
require APP_ROOT.join('app', 'models', 'representative')
require APP_ROOT.join('lib', 'twitter_importer')
class FindInfo
def self.politician_in_state(state)
puts "Senators:"
Politician.where(state: state, title: "Sen" ).each do |senators|
puts " #{senators.fullname} (#{senators.party})"
end
puts "Representatives:"
Politician.where(state: state, title: "Rep" ).each do |representative|
puts " #{representative.fullname} (#{representative.party})"
end
end
def self.gender_percentage(gender)
percentage, gender_num, gender_name = calculate_gender_percentage(gender, "Sen")
puts "#{gender_name} Senators: #{gender_num} (#{percentage}%)"
percentage, gender_num, gender_name = calculate_gender_percentage(gender, "Rep")
puts "#{gender_name} Representatives: #{gender_num} (#{percentage}%)"
end
def self.states_and_representatives
states_array = Politician.select(:state).map(&:state).uniq
states_and_count = states_array.map do |state|
[state, count_congresspeople(state)]
end
sorted_values = states_and_count.sort{ |a,b| b[1]<=>a[1] }
sorted_values.each do |pairs|
puts "#{pairs[0]}: #{count_senators(pairs[0])} Senators, #{count_representatives(pairs[0])} Representative(s)"
end
end
def self.total_congresspeople
puts "Senators: #{Politician.where(title: "Sen").count}"
puts "Representatives: #{Politician.where(title: "Rep").count}"
end
def self.delete_inactive_member
Politician.where(:in_office => false).destroy_all
end
private
def self.calculate_gender_percentage(gender, title)
total = Politician.where(title: "Sen", in_office: true).length
gender_num = Politician.where(gender: gender, title: "Sen", in_office: true).length
gender == "M"? gender_name = "Male": gender_name = "Female"
percentage = ((gender_num.to_f/total)*100).round
return percentage, gender_num, gender_name
end
def self.count_congresspeople(state)
Politician.where(state: state, in_office: true).count
end
def self.count_senators(state)
Politician.where(state: state, title: "Sen", in_office: true).count
end
def self.count_representatives(state)
Politician.where(state: state, title: "Rep", in_office: true).count
end
end