-
Notifications
You must be signed in to change notification settings - Fork 4
/
staffers.rb
executable file
·176 lines (137 loc) · 4.17 KB
/
staffers.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env ruby
require './config/environment'
require 'sinatra/content_for'
require './csv'
require './helpers'
set :public, 'public'
set :views, 'views'
# used by the search dropdown
before do
@committees = Office.committees.order_by([[:name, :asc]]).all
@offices = Office.others.order_by([[:name, :asc]]).all
@quarter_names = Quarter.all.distinct(:name).sort.reverse
end
get '/' do
# first 10 only
members = Office.members.house.order_by([["member.in_office", :desc], ["member.lastname", :asc], ["member.firstname", :asc]]).limit(10).all
erb :index, locals: {
committees: @committees,
offices: @offices,
members: members
}
end
get '/faq' do
erb :faq
end
get '/positions' do
search = {}
if params[:quarter].present?
search[:quarter] = params[:quarter]
end
if params[:first_name].present? and params[:first_name].size >= 2
search["staffer.first_name"] = regex_for params[:first_name]
end
if params[:last_name].present? and params[:last_name].size >= 2
search["staffer.last_name"] = regex_for params[:last_name]
end
if params[:title].present? and params[:title].size >= 2
search["title.name"] = regex_for params[:title]
end
if params[:state].present?
search["office.member.state"] = params[:state]
end
if params[:party].present?
search["office.member.party"] = params[:party]
end
if params[:office].present?
search["office.slug"] = params[:office]
end
if params[:committee].present?
search["office.slug"] = params[:committee]
end
if search.keys.empty?
positions = nil
else
positions = Position.where(search).desc(:quarter)
# positions = positions.limit(50)
end
if csv?
positions_to_csv positions
else
erb :positions, locals: {positions: positions}
end
end
get '/staffer/:slug' do
staffer = Staffer.where(:slug => params[:slug]).first
positions = Position.where("staffer.slug" => params[:slug]).all
if csv?
staffer_to_csv staffer, positions
else
erb :staffer, :locals => {:staffer => staffer, :positions => positions}
end
end
# office URLs
get '/office/:slug' do
office = Office.where(:slug => params[:slug]).first
positions = Position.where("office.slug" => params[:slug]).all
if csv?
office_to_csv office, positions
else
erb :office, :locals => {:office => office, :positions => positions}
end
end
# support legacy /legislator/ URL
get %r{/(?:member|legislator)/(\w\d+)} do
bioguide_id = params[:captures].first
office = Office.where("member.bioguide_id" => bioguide_id).first
positions = Position.where("office.member.bioguide_id" => bioguide_id).order_by([["staffer.last_name", :asc], ["staffer.first_name", :asc]]).all
if csv?
office_to_csv office, positions
else
erb :office, :locals => {:office => office, :positions => positions}
end
end
get '/committee/:committee_id' do
office = Office.where("committee.id" => params[:committee_id]).first
positions = Position.where("office.committee.id" => params[:committee_id]).order_by([["staffer.last_name", :asc], ["staffer.first_name", :asc]]).all
if csv?
office_to_csv office, positions
else
erb :office, :locals => {:office => office, :positions => positions}
end
end
get '/members' do
conditions = {}
[:state, :district, :title].each do |key|
if params[key]
conditions["member.#{key}"] = params[key]
end
end
offices = Office.members.house.where(conditions).order_by([["member.in_office", :desc], ["member.lastname", :asc], ["member.firstname", :asc]]).all
if csv?
members_to_csv offices
else
erb :offices, :locals => {:offices => offices, :type => 'members'}
end
end
get '/committees' do
offices = Office.committees.order_by([[:name, :asc]]).all
if csv?
committees_to_csv offices
else
erb :offices, :locals => {:offices => offices, :type => 'committees'}
end
end
get '/offices' do
offices = Office.others.order_by([[:name, :asc]]).all
if csv?
offices_to_csv offices
else
erb :offices, :locals => {:offices => offices, :type => 'offices'}
end
end
def regex_for(value)
regex_value = value.dup
%w{+ ? . * ^ $ ( ) [ ] { } | \ }.each {|char| regex_value.gsub! char, "\\#{char}"}
/^#{regex_value}$/i
end