rest_area 2.0.1 → 2.1.0
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/app/controllers/concerns/gets_klass.rb +24 -0
- data/app/controllers/rest_area/application_controller.rb +12 -1
- data/app/controllers/rest_area/message_controller.rb +36 -0
- data/app/controllers/rest_area/rest_controller.rb +3 -14
- data/config/routes.rb +1 -0
- data/lib/rest_area/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b7ddacb4e8cc560d42e798fa02371c760c259e5f
|
4
|
+
data.tar.gz: 3b1a92684187bdd3846c48bbdb67651b6114d9f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ecf738c6d571330ac91a4a1cf26e185a793a3ac46880054b68e6ea0335245d54cc292fafbb4f98406cd08cc6cd497a2378fce7cee0b6b1471844efe6967f6cdd
|
7
|
+
data.tar.gz: 66d189e4ca9163789a9f394e31d573746878a71e156b03a2bbd0b1ff0e8f85ec69a768713fca968655f33848ff2d4e27f778e455eaeafd85bd17d628601baf50
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module GetsKlass
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
before_filter :get_klass
|
6
|
+
end
|
7
|
+
|
8
|
+
def get_klass
|
9
|
+
rescue_uninitialized_constant do
|
10
|
+
@klass = params[:klass].classify.constantize
|
11
|
+
end
|
12
|
+
test_class(@klass)
|
13
|
+
|
14
|
+
@roots = ActionController::Base.helpers.sanitize(params[:klass]).pluralize
|
15
|
+
@root = @roots.singularize
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_class(klass)
|
19
|
+
if klass.nil? || !RestArea.class_whitelist.include?(klass)
|
20
|
+
raise ActionController::RoutingError.new("Resource Does Not Exist")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -1,4 +1,15 @@
|
|
1
1
|
module RestArea
|
2
|
-
class ApplicationController <
|
2
|
+
class ApplicationController < ::ApplicationController
|
3
|
+
|
4
|
+
private
|
5
|
+
|
6
|
+
def rescue_uninitialized_constant
|
7
|
+
yield
|
8
|
+
rescue NameError => e
|
9
|
+
unless e.message =~ /uninitialized constant/
|
10
|
+
throw e
|
11
|
+
end
|
12
|
+
end
|
3
13
|
end
|
14
|
+
|
4
15
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module RestArea
|
2
|
+
class MessageController < ApplicationController
|
3
|
+
include GetsKlass
|
4
|
+
before_filter :get_message, :set_message_class, :set_message_serializer
|
5
|
+
|
6
|
+
def get
|
7
|
+
if @message_serializer
|
8
|
+
render json: @klass.find(params[:id]).send(@message).all, each_serializer: @message_serializer, root:@message
|
9
|
+
elsif @message_class
|
10
|
+
render json: { @message => @klass.find(params[:id]).send(@message).all }.to_json(root:false)
|
11
|
+
else
|
12
|
+
raise NotImplementedError
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def get_message
|
19
|
+
@message = params[:message]
|
20
|
+
end
|
21
|
+
|
22
|
+
def set_message_class
|
23
|
+
rescue_uninitialized_constant do
|
24
|
+
@message_class = @message.classify.constantize
|
25
|
+
end
|
26
|
+
test_class(@message_class) if @message_class
|
27
|
+
end
|
28
|
+
|
29
|
+
def set_message_serializer
|
30
|
+
return unless @message_class
|
31
|
+
rescue_uninitialized_constant do
|
32
|
+
@message_serializer = ( '::' + @message.classify + "Serializer" ).constantize
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -1,7 +1,8 @@
|
|
1
1
|
module RestArea
|
2
2
|
class RestController < ApplicationController
|
3
3
|
skip_before_filter :verify_authenticity_token
|
4
|
-
|
4
|
+
include GetsKlass
|
5
|
+
before_filter :set_class_serializer
|
5
6
|
|
6
7
|
# GET
|
7
8
|
def index
|
@@ -64,22 +65,10 @@ module RestArea
|
|
64
65
|
def set_class_serializer
|
65
66
|
@serializer = ( '::' + @klass.to_s + "Serializer" ).constantize
|
66
67
|
rescue NameError => e
|
67
|
-
|
68
|
-
@serializer = false
|
69
|
-
else
|
68
|
+
unless e.message =~ /uninitialized constant/
|
70
69
|
throw e
|
71
70
|
end
|
72
71
|
end
|
73
72
|
|
74
|
-
def get_class
|
75
|
-
@klass = params[:klass].singularize.camelize.constantize
|
76
|
-
unless RestArea.class_whitelist.include? @klass
|
77
|
-
raise ActionController::RoutingError.new("Resource Does Not Exist")
|
78
|
-
end
|
79
|
-
|
80
|
-
@roots = ActionController::Base.helpers.sanitize(params[:klass]).pluralize
|
81
|
-
@root = @roots.singularize
|
82
|
-
end
|
83
|
-
|
84
73
|
end
|
85
74
|
end
|
data/config/routes.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
RestArea::Engine.routes.draw do
|
2
2
|
get '/:klass', :to => "rest#index"
|
3
3
|
get '/:klass/:id', :to => "rest#show"
|
4
|
+
get '/:klass/:id/:message', :to => 'message#get'
|
4
5
|
post '/:klass', :to => "rest#create"
|
5
6
|
put '/:klass/:id', :to => "rest#update"
|
6
7
|
delete '/:klass/:id', :to => "rest#delete"
|
data/lib/rest_area/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rest_area
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin Guest
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -93,7 +93,9 @@ files:
|
|
93
93
|
- Rakefile
|
94
94
|
- app/assets/javascripts/rest_area/application.js
|
95
95
|
- app/assets/stylesheets/rest_area/application.css
|
96
|
+
- app/controllers/concerns/gets_klass.rb
|
96
97
|
- app/controllers/rest_area/application_controller.rb
|
98
|
+
- app/controllers/rest_area/message_controller.rb
|
97
99
|
- app/controllers/rest_area/rest_controller.rb
|
98
100
|
- app/helpers/rest_area/application_helper.rb
|
99
101
|
- app/views/layouts/rest_area/application.html.erb
|