roda-rest_api 1.0.3 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/roda/plugins/rest_api.rb +20 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af55d203299440b1fdc7d6335ddeab64a6aeefd8
|
4
|
+
data.tar.gz: 8496c4b5081bced31212a7df915b2f63e0cb399a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 51b812a5f7c860fa7fcb49f4725d85114f43b6fdf42ef17b88a87a38e74859990467c1e02a498f78d2002c8cbb352dc32ee6af341eeb9cbe8b41b66d2f14e5d7
|
7
|
+
data.tar.gz: b5a781123357311c3e9060c67aa09bc84b01014fca83aa05f78a4469803091317f08162cc155e40e4eaaae861d8f2911c0a8f964ad341b21167046e28a678eb0
|
@@ -14,14 +14,22 @@ class Roda
|
|
14
14
|
|
15
15
|
class Resource
|
16
16
|
|
17
|
-
|
18
|
-
attr_reader :singleton, :content_type
|
17
|
+
attr_reader :request, :path, :singleton, :content_type, :parent, :captures
|
19
18
|
|
20
|
-
def initialize(request, options={})
|
19
|
+
def initialize(path, request, parent, options={})
|
21
20
|
@request = request
|
21
|
+
@path = path.to_s
|
22
|
+
bare = options.delete(:bare) || false
|
22
23
|
@singleton = options.delete(:singleton) || false
|
23
24
|
@primary_key = options.delete(:primary_key) || "id"
|
25
|
+
@parent_key = options.delete(:parent_key) || "parent_id"
|
24
26
|
@content_type = options.delete(:content_type) || APPLICATION_JSON
|
27
|
+
if parent && !@request.script_name.empty?
|
28
|
+
@parent = parent
|
29
|
+
@parent.routes!
|
30
|
+
@captures = @request.captures.dup
|
31
|
+
@path = [':d', @path].join('/') unless bare
|
32
|
+
end
|
25
33
|
end
|
26
34
|
|
27
35
|
def list(&block)
|
@@ -52,7 +60,7 @@ class Roda
|
|
52
60
|
def routes(*routes)
|
53
61
|
@routes = routes
|
54
62
|
end
|
55
|
-
|
63
|
+
|
56
64
|
def routes!
|
57
65
|
unless @routes
|
58
66
|
@routes = SINGLETON_ROUTES.dup
|
@@ -65,17 +73,17 @@ class Roda
|
|
65
73
|
begin
|
66
74
|
args = method === :save ? JSON.parse(@request.body) : @request.GET
|
67
75
|
args.merge!(@primary_key => id) if id
|
76
|
+
args.merge!(@parent_key => @captures[0]) if @parent && !@captures.empty?
|
68
77
|
self.send(method).call(args)
|
69
78
|
rescue StandardError => e
|
70
79
|
@request.response.status = method === :save ? 422 : 404
|
71
80
|
end
|
72
81
|
end
|
73
82
|
|
74
|
-
|
75
83
|
end
|
76
84
|
|
77
85
|
module RequestMethods
|
78
|
-
|
86
|
+
|
79
87
|
def api(options={}, &block)
|
80
88
|
path = options.delete(:path) || 'api'
|
81
89
|
subdomain = options.delete(:subdomain)
|
@@ -87,13 +95,14 @@ class Roda
|
|
87
95
|
on("v#{version}", &block)
|
88
96
|
end
|
89
97
|
|
90
|
-
def resource(
|
91
|
-
@resource = Resource.new(self, options)
|
92
|
-
on(
|
98
|
+
def resource(path, options={})
|
99
|
+
@resource = Resource.new(path, self, @resource, options)
|
100
|
+
on(@resource.path, options) do
|
93
101
|
yield @resource
|
94
|
-
|
95
|
-
|
102
|
+
@resource.routes!
|
103
|
+
response.status = 404
|
96
104
|
end
|
105
|
+
@resource = @resource.parent
|
97
106
|
end
|
98
107
|
|
99
108
|
def index(options={}, &block)
|