-
Notifications
You must be signed in to change notification settings - Fork 0
/
petrovich-app.rb
86 lines (72 loc) · 2.12 KB
/
petrovich-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
# encoding: utf-8
require 'bundler/setup'
Bundler.require(:default)
require 'sinatra/base'
require 'sinatra/json'
require 'sinatra/asset_pipeline'
require 'sprockets'
require 'sprockets-helpers'
require 'petrovich'
class PetrovichApp < Sinatra::Base
register Sinatra::AssetPipeline
set :assets_precompile, %w(application.css application.js)
set :assets_prefix, 'assets'
set :assets_css_compressor, :sass
set :assets_js_compressor, :uglifier
set :assets_digest, false
set :environment, :production
helpers do
include Sprockets::Helpers
end
configure do
EvilFront.install_all(sprockets)
EvilBlocks.install_to_slim!
end
helpers Sinatra::JSON
get '/' do
@input = { gender: :male,
lastname: 'Иванов',
firstname: 'Иван',
middlename: 'Иванович' }
@example = example(@input)
@cases = {
genitive: ['род.', 'Родительный падеж'],
dative: ['дат.', 'Дательный падеж'],
accusative: ['вин.', 'Винительный падеж'],
instrumental: ['тво.', 'Творительный падеж'],
prepositional: ['пре.', 'Предложный падеж']
}
slim :index
end
# Склонять по всем падежам
#
# Параметры:
#
# * lastname - Фамилия
# * firstname - Имя
# * middlename - Отчество
# * gender - Пол
#
# ФИО должны указываться в именительном падаже
post '/decline.json' do
json example(params)
end
def example(options)
petrovich = Petrovich(
lastname: options[:lastname],
firstname: options[:firstname],
middlename: options[:middlename],
gender: options[:gender]
)
create_hash(Petrovich::CASES) do |rcase|
create_hash([:lastname, :firstname, :middlename]) do |part|
petrovich.to(rcase).public_send(part) if options[part]
end
end
end
def create_hash(keys, &block)
hash = { }
keys.each { |i| hash[i] = yield(i) }
hash
end
end