-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_neta.rb
126 lines (113 loc) · 4.18 KB
/
my_neta.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
require 'roda'
require 'json'
require 'dotenv'
Dotenv.load
require_relative './models.rb'
require_relative 'lib/common/constants.rb'
require_relative 'lib/common/utils.rb'
# Main app class
class MyNeta < Roda
# All possible years for MPs
YEARS = %w(2004 2009 2014 2019)
plugin :json, serializer: proc { |o| JSON.pretty_generate(o) }
plugin :slash_path_empty
route do |r|
# Single initialization of the JSON object returned
ret = {}
# GET / request
r.root do
# Show endpoint examples
{
mps: {
endpoint: '/mps',
sample: '/mps[/year][/state or union_territory]',
examples: %W(/mps/#{YEARS.sample} /mps/2014/#{Constants::MP_STATES.sample})
},
mlas: {
endpoint: '/mlas',
sample: '/mlas[/state]',
examples: %W(/mlas /mlas/maharashtra /mlas/#{(Constants::MLA_STATES - %w(maharashtra)).sample})
},
states: Constants::MLA_STATES,
union_territories: Constants::MP_STATES - Constants::MLA_STATES
}
end
# # /scrape branch
# r.on 'scrape' do
# # Get all states
# r.is do
# NetaScraper.scrape_all_mlas
# end
# # Get one state only
# r.get ':state' do |state|
# NetaScraper.scrape_mlas(state)
# end
# end
# Route for getting MPs
r.on 'mps' do
r.is do
ret[:count] = MP.count
ret[:mps] = MP.order_by(:year, :state_or_ut).map(&:format)
ret
end
r.on :year do |year|
if YEARS.member?(year)
r.is do
ret[:count] = MP.filter(year: year).count
ret[:mps] = MP.filter(year: year).order_by(:state_or_ut, :constituency)
.map { |mp| mp.format(%i(mp_id year)) }
ret
end
r.get :state do |state|
if Constants::MP_STATES.member?(state)
state = Utils.format_state(state)
ret[:count] = MP.filter(year: year, state_or_ut: state).count
ret[:mps] = MP.filter(year: year, state_or_ut: state).order_by(:constituency)
.map { |mp| mp.format(%i(mp_id year state_or_ut)) }
else
response.status = 400
ret[:error] = 'That is not a valid state or UT'
ret[:valid_states] = Constants::MP_STATES
end
ret
end
else
response.status = 400
ret[:error] = 'That is not a valid year'
ret[:valid_years] = YEARS
ret
end
end
end
# Route for getting MLAs
r.on 'mlas' do
r.is do
ret[:count] = MLA.count
ret[:mlas] = MLA.map(&:format)
ret
end
r.get :state do |state|
if Constants::MLA_STATES.member?(state)
formatted_state = Utils.format_state(state)
{
state: formatted_state,
count: MLA.filter(state: formatted_state).count,
# Retrieve and format the required MLAs
mlas: MLA.filter(state: formatted_state)
.map { |mla| mla.format(%i(mla_id state)) }
}
else
response.status = 400
{
error: 'That is not a valid state',
valid_states: Constants::MLA_STATES
}
end
end
end
# Route for any other, redirects to root
r.on :other do |other|
r.redirect('/')
end
end
end