-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.rb
158 lines (133 loc) · 4.43 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
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
# frozen_string_literal: true
require 'cgi'
require 'dotenv'
require 'octokit'
require 'open-uri'
require 'require_all'
require 'sass'
require 'set'
require 'sinatra'
require 'yajl/json_gem'
require 'everypolitician'
require 'everypolitician/popolo'
require 'sinatra/reloader' if development?
require_relative './lib/popolo_helper'
require_relative './lib/html_helper'
require_rel './lib/page'
Dotenv.load
helpers Popolo::Helper
helpers HTMLHelper
set :erb, trim: '-'
set :main_url, 'http://everypolitician.org'
set :docs_url, 'http://docs.everypolitician.org'
set :datasource, ENV.fetch('DATASOURCE', 'https://github.com/everypolitician/everypolitician-data/raw/master/countries.json')
set :index, EveryPolitician::Index.new(index_url: settings.datasource)
configure :development do
register Sinatra::Reloader
also_reload 'lib/**/*.rb'
end
get '/' do
@page = Page::Home.new(index: settings.index)
erb :homepage
end
get '/countries.html' do
@page = Page::Countries.new(index: settings.index)
erb :countries
end
get '/:country/' do |country_slug|
pass unless country = settings.index.country(country_slug)
@page = Page::Country.new(country: country)
erb :country
end
get '/:country/' do |country_slug|
@page = Page::MissingCountry.new(country: country_slug)
pass unless @page.country
erb :country_missing
end
get '/:country/:house/' do |country_slug, house_slug|
pass unless country = settings.index.country(country_slug)
pass unless house = country.legislature(house_slug)
@page = Page::House.new(house: house)
erb :house
end
get '/:country/:house/wikidata' do |country_slug, house_slug|
pass unless country = settings.index.country(country_slug)
pass unless house = country.legislature(house_slug)
@page = Page::HouseWikidata.new(house: house)
erb :house_wikidata
end
get '/:country/:house/term-table/:id.html' do |country_slug, house_slug, termid|
pass unless country = settings.index.country(country_slug)
pass unless house = country.legislature(house_slug)
pass unless term = house.term(termid)
@page = Page::TermTable.new(term: term)
erb :term_table
end
get '/:country/download.html' do |country_slug|
pass unless country = settings.index.country(country_slug)
@page = Page::Country.new(country: country)
meta_redirect(URI.join(settings.main_url, "/#{country_slug}/"), @page.title)
end
get '/:country/:house/download.html' do |country_slug, house_slug|
pass unless country = settings.index.country(country_slug)
pass unless house = country.legislature(house_slug)
@page = Page::HouseDownload.new(house: house, index: settings.index)
erb :house_download
end
get '/status/all_countries.html' do
@page = Page::SpiderBase.new
erb :spider_base
end
get '/needed.html' do
@page = Page::Needed.new(access_token: ENV['GITHUB_ACCESS_TOKEN'])
erb :needed
end
get '/*.css' do |filename|
scss :"sass/#{filename}"
end
get '/styling' do
erb :styling
end
get '/404.html' do
erb :fourohfour
end
# Old doc pages are now at docs.everypolitician.org: redirect to them
get '/about.html' do
docs_redirect('/', 'About')
end
set :docs_map, contribute: 'How to contribute',
data_structure: 'About EveryPolitician’s data',
data_summary: 'What’s in EveryPolitician’s data?',
repo_structure: 'Getting the most recent data',
scrapers: 'About writing scrapers',
submitting: 'How we import data',
technical: 'Technical overview',
use_the_data: 'Use EveryPolitician data'
settings.docs_map.each do |page, text|
path = '/%s.html' % page
get path do
docs_redirect(path, text)
end
end
not_found do
status 404
erb :fourohfour
end
# Can't do server-side redirection on a GitHub Pages-hosted static site, so the
# kindest next-best-thing is to have a placeholder with meta HTTP-refresh.
# This works for humans (i.e., browsers parse and follow the redirect) but,
# because wget simply fetches the HTML document, this lets us continue to
# spider the site to generate the contents of everypolitician/viewer-static.
# See scripts/release.sh (build_viewer_static).
def docs_redirect(path, page_title)
meta_redirect(URI.join(settings.docs_url, path), page_title)
end
def meta_redirect(uri, page_title)
@url = uri
@head_tags = [
%(<meta http-equiv="refresh" content="0; url=#{@url}">),
%(<link rel="canonical" href="#{@url}"/>),
].join("\n\t")
@page_title = page_title
erb :redirect
end