grape-resources 0.0.1.alpha → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +49 -0
- data/lib/grape/resources.rb +25 -16
- data/lib/grape/resources/version.rb +1 -1
- data/spec/lib/grape/resources_spec.rb +50 -0
- data/spec/support/examples/api_example.rb +0 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 96c8f832053917cc1f99c0e0562f270a8455a721
|
4
|
+
data.tar.gz: 05614c9a6bd1f46715cde8a546186fdb73c4678f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d395e028761dd5010248b5d2868f8d3b095cbb4df260d6b5573a12ae2ceb8eb2ad8e8e8c741b23c2fd792af468ce926454486a3715fc1c9f85a5d11093caa263
|
7
|
+
data.tar.gz: 8ee61be4d6e088254bdeb906eeb311a201cf99d12f95b74d1473c216eef5cb5c0ef712d1f2b8f171bf83a176f9f4b05d3b03f2bf49d87455b8eca5aab0799d72
|
data/README.md
CHANGED
@@ -64,6 +64,55 @@ Available options for routes are:
|
|
64
64
|
:put -> [PUT] /player/:id
|
65
65
|
:delete -> [DELETE /player/:id
|
66
66
|
|
67
|
+
resources_for method can receive a block to allow nested resources, and custom endpoints for a particular class
|
68
|
+
|
69
|
+
for example:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
|
73
|
+
class MyApi::API < Grape::API
|
74
|
+
resources_for Player, [:list, :get] do
|
75
|
+
get :tshirt_size do
|
76
|
+
...
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
And it will generate:
|
83
|
+
|
84
|
+
GET /players
|
85
|
+
GET /player/:id
|
86
|
+
GET /players/tshirt_size
|
87
|
+
|
88
|
+
|
89
|
+
TODO: v0.0.3
|
90
|
+
|
91
|
+
- Detect when resources_for is being called inside another resource, in that case, generated routes should
|
92
|
+
consider the parent resource id.
|
93
|
+
|
94
|
+
for example:
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
|
98
|
+
class MyApi::API < Grape::API
|
99
|
+
resources :teams do
|
100
|
+
resources_for(Player, [:list, :get])
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
```
|
105
|
+
|
106
|
+
And it should generate:
|
107
|
+
|
108
|
+
GET /team/:id/players
|
109
|
+
GET /team/:id/player/:id
|
110
|
+
POST /team/:id/player
|
111
|
+
PUT /team/:id/player/:id
|
112
|
+
DELETE /team/:id/player/:id
|
113
|
+
|
114
|
+
|
115
|
+
|
67
116
|
## Contributing
|
68
117
|
|
69
118
|
1. Fork it ( https://github.com/[my-github-username]/grape-resources/fork )
|
data/lib/grape/resources.rb
CHANGED
@@ -6,35 +6,43 @@ module Grape
|
|
6
6
|
include Grape::Resources
|
7
7
|
class << self
|
8
8
|
def resources_for( clazz, methods=[:list, :get, :post, :put, :delete])
|
9
|
-
singular_name = clazz.name.underscore
|
10
|
-
plural_name = clazz.name.pluralize.underscore
|
11
|
-
|
12
9
|
raise Error("To use grape_resources on a given class it should inherit from ActiveRecord::Base.( at least for now buddy ;) )") unless clazz < ActiveRecord::Base
|
10
|
+
|
11
|
+
plural_name = clazz.name.pluralize.underscore
|
12
|
+
resources plural_name.to_sym do
|
13
|
+
Grape::Resources.list_endpoint_for( clazz, self ) if methods.include?(:list)
|
14
|
+
yield if block_given?
|
15
|
+
end
|
13
16
|
|
14
|
-
Grape::Resources.
|
15
|
-
Grape::Resources.get_endpoint_for( clazz, self ) if methods.include?(:get)
|
16
|
-
Grape::Resources.create_endpoint_for( clazz, self ) if methods.include?(:post)
|
17
|
-
Grape::Resources.update_endpoint_for( clazz, self ) if methods.include?(:put)
|
18
|
-
Grape::Resources.delete_endpoint_for(clazz, self) if methods.include?(:delete)
|
17
|
+
Grape::Resources.load_singular_endpoints(clazz, self, methods)
|
19
18
|
end
|
20
19
|
end
|
21
20
|
end
|
22
21
|
|
23
22
|
module Resources
|
24
23
|
class << self
|
24
|
+
|
25
|
+
def load_singular_endpoints( clazz, api_instance, methods)
|
26
|
+
singular_name = singular_name_for clazz
|
27
|
+
|
28
|
+
api_instance.resource singular_name.to_sym do
|
29
|
+
Grape::Resources.get_endpoint_for( clazz, api_instance ) if methods.include?(:get)
|
30
|
+
Grape::Resources.create_endpoint_for( clazz, api_instance ) if methods.include?(:post)
|
31
|
+
Grape::Resources.update_endpoint_for( clazz, api_instance ) if methods.include?(:put)
|
32
|
+
Grape::Resources.delete_endpoint_for( clazz, api_instance) if methods.include?(:delete)
|
33
|
+
end
|
34
|
+
end
|
25
35
|
|
26
36
|
def list_endpoint_for(clazz, api_instance)
|
27
|
-
|
28
|
-
|
29
|
-
api_instance.route('GET', ["/#{plural_name}"], {} ) do
|
37
|
+
api_instance.get do
|
30
38
|
result = Grape::Resources.list(clazz, params)
|
31
39
|
result
|
32
40
|
end
|
33
41
|
end
|
34
42
|
|
35
|
-
def get_endpoint_for(clazz, api_instance)
|
43
|
+
def get_endpoint_for(clazz, api_instance)
|
36
44
|
singular_name = singular_name_for clazz
|
37
|
-
api_instance.
|
45
|
+
api_instance.get ":id" do
|
38
46
|
result = Grape::Resources.find(clazz, params)
|
39
47
|
error!( "#{singular_name} not found", 404) if result.nil?
|
40
48
|
result
|
@@ -44,7 +52,7 @@ module Grape
|
|
44
52
|
def delete_endpoint_for(clazz, api_instance)
|
45
53
|
singular_name = singular_name_for clazz
|
46
54
|
|
47
|
-
api_instance.
|
55
|
+
api_instance.delete ":id" do
|
48
56
|
result = Grape::Resources.find(clazz, params)
|
49
57
|
error!( "#{singular_name} not found", 404) if result.nil?
|
50
58
|
result.destroy
|
@@ -53,7 +61,7 @@ module Grape
|
|
53
61
|
|
54
62
|
def create_endpoint_for(clazz, api_instance)
|
55
63
|
singular_name = singular_name_for clazz
|
56
|
-
api_instance.
|
64
|
+
api_instance.post do
|
57
65
|
result = clazz.new
|
58
66
|
|
59
67
|
Grape::Resources.apply_attributes(result, params)
|
@@ -66,7 +74,7 @@ module Grape
|
|
66
74
|
def update_endpoint_for(clazz, api_instance)
|
67
75
|
singular_name = singular_name_for clazz
|
68
76
|
|
69
|
-
api_instance.
|
77
|
+
api_instance.put ":id" do
|
70
78
|
result = clazz.find_by_id(params[:id])
|
71
79
|
error!( {error: "#{singular_name} with id '#{params[:id]}' was not found"}, 404) unless result.present?
|
72
80
|
|
@@ -96,6 +104,7 @@ module Grape
|
|
96
104
|
def singular_name_for( clazz )
|
97
105
|
clazz.name.underscore
|
98
106
|
end
|
107
|
+
|
99
108
|
end
|
100
109
|
end
|
101
110
|
end
|
@@ -215,4 +215,54 @@ describe Grape::Resources do
|
|
215
215
|
end
|
216
216
|
|
217
217
|
end
|
218
|
+
|
219
|
+
describe "block processing" do
|
220
|
+
it "should accept a block and process it after the resources part" do
|
221
|
+
|
222
|
+
subject.class_eval do
|
223
|
+
resources_for(Car, [:list] ) do
|
224
|
+
get :engine do
|
225
|
+
["Cars usually have one engine"]
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
get "/cars/engine"
|
231
|
+
expect(last_response.status).to eq 200
|
232
|
+
end
|
233
|
+
|
234
|
+
it "blocks inside can be nested as Grape provides" do
|
235
|
+
|
236
|
+
subject.class_eval do
|
237
|
+
resources_for(Car, [:list] ) do
|
238
|
+
namespace :engine do
|
239
|
+
before do
|
240
|
+
@something = "cool"
|
241
|
+
end
|
242
|
+
|
243
|
+
get do
|
244
|
+
end
|
245
|
+
|
246
|
+
post do
|
247
|
+
{ something: @something }.to_json
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
post "/cars/engine"
|
254
|
+
expect(JSON.parse(last_response.body)["something"]).to eq "cool"
|
255
|
+
end
|
256
|
+
|
257
|
+
it "should allow nested resources_for" do
|
258
|
+
subject.class_eval do
|
259
|
+
resources_for(Car, [:list] ) do
|
260
|
+
resources_for(User)
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
get "/cars/users"
|
265
|
+
expect(last_response.status).to eq 200
|
266
|
+
end
|
267
|
+
end
|
218
268
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-resources
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Antonio Pagano, Israel De La Hoz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -234,9 +234,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
234
234
|
version: '0'
|
235
235
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
236
236
|
requirements:
|
237
|
-
- - "
|
237
|
+
- - ">="
|
238
238
|
- !ruby/object:Gem::Version
|
239
|
-
version:
|
239
|
+
version: '0'
|
240
240
|
requirements: []
|
241
241
|
rubyforge_project:
|
242
242
|
rubygems_version: 2.4.1
|
@@ -253,3 +253,4 @@ test_files:
|
|
253
253
|
- spec/support/models/car.rb
|
254
254
|
- spec/support/models/user.rb
|
255
255
|
- spec/support/schema.rb
|
256
|
+
has_rdoc:
|