restaurant 0.0.4 → 0.0.5
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 +31 -28
- data/Rakefile +3 -1
- data/lib/restaurant/config.rb +39 -5
- data/lib/restaurant/controller_helper.rb +1 -0
- data/lib/restaurant/controller_provider.rb +2 -6
- data/lib/restaurant/restful_actions.rb +20 -6
- data/lib/restaurant/role_provider.rb +3 -1
- data/lib/restaurant/router.rb +7 -3
- data/lib/restaurant/version.rb +1 -1
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 55d03c905414bb0fbac7cbc3752191feebee35da
|
4
|
+
data.tar.gz: 57d837db184f2493ce2a719c1ecfa9f4da2d05e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 364da1e3ae41a17dd979c958f8a62019593ecf526acb170aaa18651a2abfcc3f72006128f3b1a3c2e9f2df24369c3058a0b9b8dc24d8f557fd6b0d97cbd8ec12
|
7
|
+
data.tar.gz: 4e8458965b3d116cc5ee364978af53a72e9928519ece7e57994c827972b92fb3c298282a4b464c071db286fac8fe6b8fe69ec0c1cd395eb4ea23bb766deac334
|
data/README.md
CHANGED
@@ -2,8 +2,10 @@
|
|
2
2
|
Restaurant serves your data via auto-defined RESTful API on your rails application.
|
3
3
|
|
4
4
|
## Features
|
5
|
+
* Auto-defined models
|
5
6
|
* Auto-defined controllers
|
6
7
|
* Auto-defined routes
|
8
|
+
* Versioning
|
7
9
|
* SQL-like URI query
|
8
10
|
* OAuth authentication
|
9
11
|
* Scope based authorization
|
@@ -12,37 +14,38 @@ Restaurant serves your data via auto-defined RESTful API on your rails applicati
|
|
12
14
|
* restrict filtering
|
13
15
|
* restrict sorting
|
14
16
|
* RESTful APIs
|
15
|
-
* GET /:resources
|
16
|
-
* GET /:resources/:id
|
17
|
-
* POST /:resources
|
18
|
-
* PUT /:resources/:id
|
19
|
-
* DELETE /:resources/:id
|
17
|
+
* GET /v1/:resources
|
18
|
+
* GET /v1/:resources/:id
|
19
|
+
* POST /v1/:resources
|
20
|
+
* PUT /v1/:resources/:id
|
21
|
+
* DELETE /v1/:resources/:id
|
20
22
|
|
21
|
-
## Auto-defined
|
22
|
-
|
23
|
+
## Auto-defined application
|
24
|
+
Models, controllers, and routes are auto-defined from your config/restaurant.yml.
|
23
25
|
|
24
26
|
```yaml
|
25
27
|
# config/restaurant.yml
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
28
|
+
v1: # Namespaced by v1
|
29
|
+
public: # User with "public" scope token
|
30
|
+
recipes: #
|
31
|
+
actions: #
|
32
|
+
- show # can access to /recipes/:id
|
33
|
+
attributes: #
|
34
|
+
- title # can read recipe.title
|
35
|
+
admin: # User with "admin" scope token
|
36
|
+
recipes: #
|
37
|
+
actions: #
|
38
|
+
- index # can access to /recipes
|
39
|
+
- show # can access to /recipes/:id
|
40
|
+
where: #
|
41
|
+
- id # can filter recipes by id
|
42
|
+
- title # can filter recipes by title
|
43
|
+
order: #
|
44
|
+
- id # can sort recipes by id
|
45
|
+
- title # can sort recipes by title
|
46
|
+
attributes: #
|
47
|
+
- id # can read recipe.id
|
48
|
+
- title # can read recipe.title
|
46
49
|
```
|
47
50
|
|
48
51
|
## SQL-like URI query
|
@@ -51,7 +54,7 @@ You can filter and sort resources by SQL-like URI query.
|
|
51
54
|
```ruby
|
52
55
|
context "with where params" do
|
53
56
|
it "returns recipes filtered by given query" do
|
54
|
-
get "/recipes", { where: { title: { eq: recipe.title } } }, env
|
57
|
+
get "/v1/recipes", { where: { title: { eq: recipe.title } } }, env
|
55
58
|
response.should be_ok
|
56
59
|
response.body.should be_json(
|
57
60
|
"id" => 1,
|
data/Rakefile
CHANGED
data/lib/restaurant/config.rb
CHANGED
@@ -1,13 +1,19 @@
|
|
1
1
|
module Restaurant::Config
|
2
2
|
class << self
|
3
|
-
def
|
4
|
-
@
|
3
|
+
def versions
|
4
|
+
@versions ||= YAML.load_file(path)
|
5
5
|
end
|
6
6
|
|
7
7
|
def controllers
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
result = []
|
9
|
+
versions.each do |version, scopes|
|
10
|
+
scopes.each do |scope, controllers|
|
11
|
+
controllers.keys.each do |controller|
|
12
|
+
result << "#{version.camelize}::#{controller.camelize}Controller"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
result.uniq
|
11
17
|
end
|
12
18
|
|
13
19
|
def path
|
@@ -15,6 +21,34 @@ module Restaurant::Config
|
|
15
21
|
raise NoRolesError, "#{path} is not found" unless path.exist?
|
16
22
|
end
|
17
23
|
end
|
24
|
+
|
25
|
+
def define_version_modules
|
26
|
+
version_module_names.each do |version_module_name|
|
27
|
+
unless Object.const_defined?(version_module_name)
|
28
|
+
Object.const_set(version_module_name, Module.new)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def define_controller_classes(base)
|
34
|
+
versions.each do |version, scopes|
|
35
|
+
scopes.each do |scope, controllers|
|
36
|
+
controllers.keys.each do |controller|
|
37
|
+
version_module = version.camelize.constantize
|
38
|
+
controller_class_name = "#{controller.camelize}Controller"
|
39
|
+
unless version_module.const_defined?(controller_class_name)
|
40
|
+
version_module.const_set(controller_class_name, Class.new(base))
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def version_module_names
|
48
|
+
versions.keys.map do |version_name|
|
49
|
+
version_name.camelize
|
50
|
+
end
|
51
|
+
end
|
18
52
|
end
|
19
53
|
|
20
54
|
NoRolesError = Class.new(StandardError)
|
@@ -2,11 +2,7 @@ module Restaurant::ControllerProvider
|
|
2
2
|
extend ActiveSupport::Concern
|
3
3
|
|
4
4
|
included do
|
5
|
-
Restaurant::Config.
|
6
|
-
|
7
|
-
unless Object.const_defined?(class_name)
|
8
|
-
Object.const_set(class_name, Class.new(self))
|
9
|
-
end
|
10
|
-
end
|
5
|
+
Restaurant::Config.define_version_modules
|
6
|
+
Restaurant::Config.define_controller_classes(self)
|
11
7
|
end
|
12
8
|
end
|
@@ -5,6 +5,8 @@ module Restaurant::RestfulActions
|
|
5
5
|
rescue_from ActiveRecord::RecordNotFound do
|
6
6
|
head 404
|
7
7
|
end
|
8
|
+
|
9
|
+
hide_action :current_version
|
8
10
|
end
|
9
11
|
|
10
12
|
def index
|
@@ -16,17 +18,21 @@ module Restaurant::RestfulActions
|
|
16
18
|
end
|
17
19
|
|
18
20
|
def create
|
19
|
-
respond_with model.create(
|
21
|
+
respond_with model.create(resource_param), :only => current_role.allowed_attributes
|
20
22
|
end
|
21
23
|
|
22
24
|
def update
|
23
|
-
respond_with resource.update_attributes(
|
25
|
+
respond_with resource.update_attributes(resource_param)
|
24
26
|
end
|
25
27
|
|
26
28
|
def destroy
|
27
29
|
respond_with resource.delete
|
28
30
|
end
|
29
31
|
|
32
|
+
def current_version
|
33
|
+
@current_version ||= self.class.name.split("::").first.underscore
|
34
|
+
end
|
35
|
+
|
30
36
|
private
|
31
37
|
|
32
38
|
def model
|
@@ -36,18 +42,26 @@ module Restaurant::RestfulActions
|
|
36
42
|
end
|
37
43
|
|
38
44
|
def model_name
|
39
|
-
|
45
|
+
self.class.name.sub(/Controller$/, "").singularize
|
40
46
|
end
|
41
47
|
|
42
|
-
def
|
43
|
-
params[
|
48
|
+
def resource_param
|
49
|
+
params[resource_class_name.underscore]
|
44
50
|
end
|
45
51
|
|
46
52
|
def resource
|
47
53
|
model.find(params[:id])
|
48
54
|
end
|
49
55
|
|
56
|
+
def resource_class_name
|
57
|
+
model_name.split("::").last
|
58
|
+
end
|
59
|
+
|
50
60
|
def define_model
|
51
|
-
|
61
|
+
current_version_module.const_set(resource_class_name, Class.new(ActiveRecord::Base))
|
62
|
+
end
|
63
|
+
|
64
|
+
def current_version_module
|
65
|
+
current_version.camelize.constantize
|
52
66
|
end
|
53
67
|
end
|
@@ -1,4 +1,6 @@
|
|
1
1
|
module Restaurant::RoleProvider
|
2
|
+
private
|
3
|
+
|
2
4
|
def current_role
|
3
5
|
@current_role ||= Role.new(self)
|
4
6
|
end
|
@@ -23,7 +25,7 @@ module Restaurant::RoleProvider
|
|
23
25
|
end
|
24
26
|
|
25
27
|
def abilities
|
26
|
-
@abilities ||= Restaurant::Config.
|
28
|
+
@abilities ||= Restaurant::Config.versions[controller.current_version].map do |role, controllers|
|
27
29
|
if doorkeeper_token.scopes.include?(role.to_sym)
|
28
30
|
controllers[controller_name]
|
29
31
|
end
|
data/lib/restaurant/router.rb
CHANGED
@@ -10,9 +10,13 @@ class Restaurant::Router
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def route
|
13
|
-
Restaurant::Config.
|
14
|
-
|
15
|
-
|
13
|
+
Restaurant::Config.versions.each do |version, scopes|
|
14
|
+
router.namespace(version) do
|
15
|
+
scopes.each do |scope, controllers|
|
16
|
+
controllers.each do |controller, values|
|
17
|
+
router.resources controller, :only => values["actions"]
|
18
|
+
end
|
19
|
+
end
|
16
20
|
end
|
17
21
|
end
|
18
22
|
end
|
data/lib/restaurant/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restaurant
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryo Nakamura
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-05-
|
11
|
+
date: 2013-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -213,4 +213,3 @@ signing_key:
|
|
213
213
|
specification_version: 4
|
214
214
|
summary: A rails plugin to auto-define RESTful API
|
215
215
|
test_files: []
|
216
|
-
has_rdoc:
|