Class: Orange::Middleware::RestfulRouter
- Inherits:
-
Base
show all
- Defined in:
- lib/orange-core/middleware/restful_router.rb
Instance Method Summary
collapse
Methods inherited from Base
#call, #initialize, #inspect, #orange, #pass, #recapture
Instance Method Details
#action_exposed?(list, route_parts) ⇒ Boolean
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/orange-core/middleware/restful_router.rb', line 79
def action_exposed?(list, route_parts)
return true if list == :all
return true if list == route_parts[:resource_action]
return true if list.is_a?(Array) && list.include?(route_parts[:resource_action])
if list.is_a?(Hash)
all = list.has_key?(:all) ? action_exposed?(list[:all], route_parts) : false
one = list.has_key?(route_parts[:resource]) ? action_exposed?(list[route_parts[:resource]], route_parts) : false
return all || one
end
false
end
|
#init(*args) ⇒ Object
5
6
7
8
9
10
|
# File 'lib/orange-core/middleware/restful_router.rb', line 5
def init(*args)
opts = args..with_defaults(:restful_contexts => [:admin, :orange], :not_found => false, :exposed_actions => {:admin => :all, :orange => :all})
@exposed = opts[:exposed_actions]
@contexts = opts[:restful_contexts]
@not_found = opts[:not_found]
end
|
#packet_call(packet) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/orange-core/middleware/restful_router.rb', line 17
def packet_call(packet)
return (pass packet) if packet['route.router'] parts = route_parts(packet)
if(should_route?(packet, parts))
packet['route.resource'] = parts[:resource] if parts[:resource]
packet['route.resource_id'] = parts[:resource_id] if parts[:resource_id]
packet['route.resource_action'] = parts[:resource_action] if parts[:resource_action]
packet['route.resource_path'] = parts[:remainder]
if(packet['route.resource', false])
packet['route.router'] = self
elsif(@not_found)
packet['route.resource'] = @not_found
packet['route.router'] = self
end
end
pass packet
end
|
#route(packet) ⇒ Object
91
92
93
94
95
96
97
|
# File 'lib/orange-core/middleware/restful_router.rb', line 91
def route(packet)
resource = packet['route.resource']
raise 'resource not found' unless orange.loaded? resource
mode = packet['route.resource_action'] ||
(packet['route.resource_id'] ? :show : :list)
packet[:content] = orange[resource].view packet
end
|
#route_parts(packet) ⇒ Object
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
|
# File 'lib/orange-core/middleware/restful_router.rb', line 42
def route_parts(packet)
return_parts = {}
path = packet['route.path'] || packet.request.path_info
parts = path.split('/')
pad = parts.shift
if !parts.empty?
resource = parts.shift
if orange.loaded?(resource.to_sym)
return_parts[:resource] = resource.to_sym
if !parts.empty?
second = parts.shift
if second =~ /^\d+$/
return_parts[:resource_id] = second
if !parts.empty?
return_parts[:resource_action] = parts.shift.to_sym
else
return_parts[:resource_action] = :show
end
else
return_parts[:resource_action] = second.to_sym
end
else
return_parts[:resource_action] = :list
end else
parts.unshift(resource)
end end return_parts[:remainder] = parts.unshift(pad).join('/')
return_parts
end
|
#should_route?(packet, parts) ⇒ Boolean
74
75
76
77
|
# File 'lib/orange-core/middleware/restful_router.rb', line 74
def should_route?(packet, parts)
return false unless @exposed.has_key?(packet['route.context'])
action_exposed?(@exposed[packet['route.context']], parts)
end
|