-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
39 lines (32 loc) · 1.02 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
require 'sinatra'
require 'erb'
require 'shellwords'
require 'i18n'
require 'sinatra/i18n'
require_relative 'models/cowsay_validator'
require_relative 'lib/cowsay_characters'
I18n.load_path = Dir[File.join(settings.root, 'locales', '*.yml')]
I18n.config.available_locales = [:en, :pt]
I18n.default_locale = :en
before do
I18n.locale = request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first if request.env['HTTP_ACCEPT_LANGUAGE']
end
get '/' do
erb :index
end
get '/cowsay_output' do
cowsay_validator = CowsayValidator.new(
message: params['message'],
character: params['character'],
available_characters: CowsayCharacters.available_characters
)
if cowsay_validator.valid?
escaped_message = Shellwords.escape(cowsay_validator.message)
escaped_character = Shellwords.escape(cowsay_validator.character)
@output = `/usr/games/cowsay -f #{escaped_character} #{escaped_message}`
else
@output = I18n.t('unavailable_character', character: escaped_character)
end
erb :cowsay_output
end
__END__