-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
70 lines (56 loc) · 1.35 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 'rubygems'
require 'bundler/setup'
require 'sinatra'
require 'sinatra/reloader' if development?
require 'rdiscount'
require 'json'
require 'faker'
set :show_exceptions, :after_handler
set :views, File.dirname(__FILE__)
get '/' do
markdown :README
end
get '/methods' do
erb :methods, locals: {
modules: Faker.constants.sort.map do |const|
modularize(const)
end
}
end
get '/:mod' do |mod|
erb :methods, locals: { modules: [modularize(mod.capitalize)] }
end
get '/:mod/:method/?*?' do |mod, method, extra_params|
method = methodize(mod, method)
method.call(*to_params(extra_params)).to_json
end
error NameError do
[404, "I don't know nothin bout no #{env['sinatra.error'].message}"]
end
def to_params params_from_user
params_from_user.split('/').map do |param|
if param =~ /\A\d+\Z/
param.to_i
elsif %w{true false}.include?(param)
param == "true" # heh
else
param
end
end
end
def modularize mod
Kernel.const_get("Faker::#{mod}")
end
def methodize mod, method
klass = modularize(mod.capitalize)
check_publicity(klass, method)
klass.method(method.to_sym)
end
def check_publicity klass, method
unless klass.methods(false).include?(method.to_sym)
raise NameError.new(
"#{klass}.#{method} is not allowed. Must not be an inherited method.",
"#{klass}.#{method}"
)
end
end