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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/roda/plugins/rest_api.rb +20 -11
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c191d44a767a70a6ae9cb0c39e9efd7fc44cdec7
4
- data.tar.gz: da767f27f394f69be08b126d85b2a4fda606fd76
3
+ metadata.gz: af55d203299440b1fdc7d6335ddeab64a6aeefd8
4
+ data.tar.gz: 8496c4b5081bced31212a7df915b2f63e0cb399a
5
5
  SHA512:
6
- metadata.gz: baa4cd7ce2ef637e0593e8b150df1704838acfdf6db9a794f9259c4ce6a159706a4c87ec62d33588e5cf419a12e51298762692bd4b0b880f573a74653ce46b90
7
- data.tar.gz: e31fe6ce5d13813f43d7d62327b86bdec434e83336f1711869fe3c84f373d51e44ca8b8e8ce2b37201bb4d271c067304b1ab1b0968b8b84ca56b4daf5f3f11ad
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(name, options={})
91
- @resource = Resource.new(self, options)
92
- on(name.to_s, options) do
98
+ def resource(path, options={})
99
+ @resource = Resource.new(path, self, @resource, options)
100
+ on(@resource.path, options) do
93
101
  yield @resource
94
- @resource.routes!
95
- response.status = 404
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)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roda-rest_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Benevento