-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
executable file
·70 lines (52 loc) · 1.29 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
require 'open-uri'
require 'i18n'
require 'sinatra'
class HelloLamppostWebsite < Sinatra::Base
configure do
I18n.load_path = Dir[File.join(settings.root, 'config', 'locales', '*.yml')]
I18n.default_locale = :en
I18n.backend.load_translations
end
helpers do
def link_html_class(path)
request.path_info.start_with?(path) ? 'current' : ''
end
def locale_path(path)
return "/#{I18n.locale}#{path}"
end
def code_parser(code)
code = code.to_s
words = code.split(/\s+/)
if words.length >= 2
{
object_type: (words[0..-2].join('_').downcase),
object_code: words[-1].gsub("#", '')
}
else
nil
end
end
end
before /.*/ do
matches = request.path_info.match(/\A\/(kz|ru|en)(.*)/)
if matches
I18n.locale = matches[1]
request.path_info = matches[2]
else
redirect to('/en' + request.path_info)
end
end
get '/' do
@page_title = I18n.t(:home_title)
erb :index
end
get '/questions/random' do
cache_control :public, max_age: 0
url = "http://hello-lamp-post-api.herokuapp.com/questions/random?location_id=13&except=292&locale=en"
open(url).read
end
get '/' do
@page_title = "Hello Lamp Post London"
erb :index
end
end