forked from austvik/wadlgen
/
route_parser.rb
89 lines (71 loc) · 2.07 KB
/
route_parser.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
module Wadlgen
class RouteParser
attr_accessor :application, :base
def initialize(application, base)
self.application = application
self.base = base
end
def parse
get_route_structure base
end
private
def get_route_structure(base)
app = Wadlgen::Application.new
ress = app.add_resources(base)
application.routes.routes.each do |route|
defaults = route.defaults
controller = defaults[:controller]
action = defaults[:action]
next if action == 'edit'
resource = ress.get_resource(nil, route.path)
method = resource.get_method(route.verb, defaults[:action])
req = method.add_request
query = req.add_param('format', 'query')
success = method.add_response(200)
get_representations(controller, action).each_pair do |format, element|
success.add_representation "application/#{format}", element
query.add_option(format, "application/#{format}")
end
get_params(action).each_pair do |param, type|
req.add_param(param, type)
end
end
app
end
def get_representations(controller, action)
res = {}
controller_name = "#{controller}_controller".camelcase
if controller_name.match /::/
module_name = controller_name.split('::').first
controller_name = controller_name.split('::').last
obj = Object::const_get(module_name)
cont_obj = obj.const_get(controller_name).new()
else
cont_obj = Object::const_get(controller_name).new()
end
cont_obj.mimes_for_respond_to.each_pair do |format, type|
res[format] = format
# TODO: Only and Except
end
res
end
def get_params(action)
case action
when 'show'
{:id => 'query'}
when 'new'
{:id => 'query'}
when 'create'
{:id => 'query'}
when 'update'
{:id => 'query'}
when 'destroy'
{:id => 'query'}
when 'index'
{}
else
{}
end
end
end
end