grape-resources 0.0.1.alpha → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 837961eaaf5f621932eec50334d3cf76b3036b2b
4
- data.tar.gz: 618ca3bb4d8ef74cf314018e0a4de72a3c476426
3
+ metadata.gz: 96c8f832053917cc1f99c0e0562f270a8455a721
4
+ data.tar.gz: 05614c9a6bd1f46715cde8a546186fdb73c4678f
5
5
  SHA512:
6
- metadata.gz: f14e1a80d8a32848d668b6010a192a8acb103c9812c09d02361ff6cc603f761578a5a8a1b67bc8f935df2739ba8213e6cf6aaa19bbe83c8c8d77864d1b6a556c
7
- data.tar.gz: 2ca3c1960f2d0e6b72260c41c46f3b583d410b07806c519cbcc0bc2832ce5651be670ed3c2cb41b4a5b53a32556aab2cacc0e9bbf06ff79493b321bab98cae8d
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 )
@@ -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.list_endpoint_for( clazz, self ) if methods.include?(:list)
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
- plural_name = clazz.name.pluralize.underscore
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.route('GET', ["/#{singular_name }/:id"], {}) do
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.route('DELETE', ["/#{singular_name}/:id"], {}) do
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.route('POST', ["/#{singular_name}"], {}) do
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.route('PUT', ["/#{singular_name}/:id"], {}) do
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
@@ -1,5 +1,5 @@
1
1
  module Grape
2
2
  module Resources
3
- VERSION = "0.0.1.alpha"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  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
@@ -3,5 +3,4 @@ require "grape/resources"
3
3
 
4
4
  class APIExample < Grape::API
5
5
  format :json
6
- resources_for( User )
7
6
  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.1.alpha
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-09-06 00:00:00.000000000 Z
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: 1.3.1
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: