-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
168 lines (113 loc) · 3.87 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
159
160
161
162
163
164
165
166
167
# encoding: utf-8
def codes_group_by_name_and_country( codes )
### fix-fix-fix: move to country code model!!!!!
## note: make code.name case insensitive (use) upcase
# in your order clause use => CountryCode.order( 'UPPER(name),country_id')
## pass 1: group codes by code.name n country.name
groups1 = []
group1 = nil
last_code_name = nil
last_country_name = nil
codes.each do |code|
new_code = last_code_name != code.name.upcase
new_country = last_country_name != code.country.name
if new_code || new_country
groups1 << group1 if group1
group1 = []
end
group1 << code
last_code_name = code.name.upcase
last_country_name = code.country.name
end
groups1 << group1 if group1
### pass 2: group by code (more than once country possible per code!)
groups2 = []
group2 = nil
last_code_name = nil
groups1.each do |group|
new_code = last_code_name != group[0].name.upcase
if new_code
groups2 << group2 if group2
group2 = []
end
group2 << group
last_code_name = group[0].name.upcase
end
groups2 << group2 if group2
## sort groups by size (list country w/ more codes first)
groups2.each do |group|
group.sort! { |l,r| r.size <=> l.size }
end
groups2
end
class CountryCodesApp < Sinatra::Base
include WorldDb::Models ## (re)use world.db models
##############
# Helpers
def link_to( title, href )
"<a href='#{href}'>#{title}</a>"
end
def link_to_country( country )
link_to( country.name, "/c/#{country.key}" )
end
def link_to_continent( continent )
link_to( continent.name, "/r/#{continent.key}" )
end
def link_to_codepage( page ) # e.g. A2, NET, FIPS etc. same as code.kind
link_to( page, "/p/#{page}" )
end
def link_to_code( code )
## remove dots e.g. .at => at etc.
link_to( code, "/#{code.gsub('.','').downcase}")
end
##############################################
# Controllers / Routing / Request Handlers
get '/' do
where_clause = build_where_clause_from_params( params ) ## filter countries by kind/type (e.g. supra/country/dependency)
countries = Country.where( where_clause )
erb :index, locals: { title: 'World',
countries: countries.by_name,
countries_count: countries.count,
where_clause: where_clause
}
end
get '/codes' do
erb :codes
end
get '/stats' do
erb :stats
end
get '/:name' do |name|
codes = CountryCode.where( "UPPER(name)=?", name.upcase ).order( 'country_id' )
erb :code, locals: { name: name, codes: codes, codes_count: codes.count() }
end
get '/p/:name' do |name|
codes = CountryCode.where( kind: name ).order( 'name' )
erb :page, locals: { name: name, codes: codes, codes_count: codes.count() }
end
get '/c/:key' do |key|
country = Country.find_by_key!( key )
erb :country, locals: { country: country }
end
get '/r/:key' do |key|
continent = Continent.find_by_key!( key )
where_clause = build_where_clause_from_params( params ) ## filter countries by kind/type (e.g. supra/country/dependency)
countries = continent.countries.where( where_clause )
erb :index, locals: { title: "#{continent.name}",
countries: countries.by_name,
countries_count: countries.count,
where_clause: where_clause
}
end
private
def build_where_clause_from_params( params )
c = params[:c].nil? ? true : (['f','off','no','n'].include?(params[:c]) ? false : true)
d = params[:d].nil? ? true : (['f','off','no','n'].include?(params[:d]) ? false : true)
s = params[:s].nil? ? true : (['f','off','no','n'].include?(params[:s]) ? false : true)
conds = []
conds << "c = 't'" if c
conds << "d = 't'" if d
conds << "s = 't'" if s
conds.join(' OR ')
end
end # class CountryCodesApp