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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 34693c2b7b5744a24cb8d8b5996a0e71f864c224
4
- data.tar.gz: 05751994c3c8c46fa048b739ef440f0b61a1b6df
3
+ metadata.gz: 55d03c905414bb0fbac7cbc3752191feebee35da
4
+ data.tar.gz: 57d837db184f2493ce2a719c1ecfa9f4da2d05e2
5
5
  SHA512:
6
- metadata.gz: ed5df57787d9ae463a3b9f0163470c613769efc904add512f243d06db0a4b2ff421236d7909976accd30a02ede8a90a3fba64389a1e2e79f13e90093fd7bd91f
7
- data.tar.gz: f19f4861e947ea6b6b1cb94907ee48f22c8a7654c21d35def38d68137b97629bacaa377b7d1b0302918e49f5466c8c5d6c205289708838ef7674e2a56d4f3083
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 controllers and routes
22
- Controllers and routes are auto-defined from your config/restaurant.yml.
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
- public: # User with "public" scope token
27
- recipes: #
28
- actions: #
29
- - show # can access to /recipes/:id
30
- attributes: #
31
- - title # can read recipe.title
32
- admin: # User with "admin" scope token
33
- recipes: #
34
- actions: #
35
- - index # can access to /recipes
36
- - show # can access to /recipes/:id
37
- where: #
38
- - id # can filter recipes by id
39
- - title # can filter recipes by title
40
- order: #
41
- - id # can sort recipes by id
42
- - title # can sort recipes by title
43
- attributes: #
44
- - id # can read recipe.id
45
- - title # can read recipe.title
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
@@ -1,5 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
-
4
3
  RSpec::Core::RakeTask.new(:spec)
5
4
  task :default => :spec
5
+
6
+ require File.expand_path("../spec/dummy/config/application", __FILE__)
7
+ Dummy::Application.load_tasks
@@ -1,13 +1,19 @@
1
1
  module Restaurant::Config
2
2
  class << self
3
- def roles
4
- @roles ||= YAML.load_file(path)
3
+ def versions
4
+ @versions ||= YAML.load_file(path)
5
5
  end
6
6
 
7
7
  def controllers
8
- roles.inject([]) do |result, (role, controllers)|
9
- result += controllers.keys
10
- end.uniq
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)
@@ -9,5 +9,6 @@ module Restaurant::ControllerHelper
9
9
  include Restaurant::Authorization
10
10
  include Restaurant::RoleProvider
11
11
  self.responder = Restaurant::ParamsQueryResponder
12
+ respond_to :json
12
13
  end
13
14
  end
@@ -2,11 +2,7 @@ module Restaurant::ControllerProvider
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  included do
5
- Restaurant::Config.controllers.each do |controller|
6
- class_name = "#{controller.camelize.pluralize}Controller"
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(model_param), :only => current_role.allowed_attributes
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(model_param)
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
- @model_name ||= self.class.name.sub(/Controller$/, "").singularize
45
+ self.class.name.sub(/Controller$/, "").singularize
40
46
  end
41
47
 
42
- def model_param
43
- params[model_name.underscore]
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
- Object.const_set(model_name, Class.new(ActiveRecord::Base))
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.roles.map do |role, controllers|
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
@@ -10,9 +10,13 @@ class Restaurant::Router
10
10
  end
11
11
 
12
12
  def route
13
- Restaurant::Config.roles.each do |role, controllers|
14
- controllers.each do |controller_name, definitions|
15
- router.resources controller_name, :only => definitions["actions"]
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
@@ -1,3 +1,3 @@
1
1
  module Restaurant
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
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
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-14 00:00:00.000000000 Z
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: