rest_area 1.0.0 → 1.1.0

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: 072ac3365b0365f2606e8713e02a3af8124a80cd
4
- data.tar.gz: fae9015327e25c0a0b939bb57a06b0ab97e796b3
3
+ metadata.gz: c8d9e73c8c0943efc39ab8689f835455406947d6
4
+ data.tar.gz: b3671c058b27f88b7143ed21bc48ae71f0b71707
5
5
  SHA512:
6
- metadata.gz: 84e4e4a7e23661f4d24ae3bbc22d580aa44c9b2047ae0c2a4060c0a13451c33373f5238425c614649e5026c03c5fb29aa582bd0460a05ed3c7e68793d19b2e86
7
- data.tar.gz: 46b397192acede9d404f9805e255a88058b391fb300a3f245f3deb7401af3484c9e67c8c2d273a1987c392a92bd2cd2bd52d3d14e8672ec7780db5f6e6fc8690
6
+ metadata.gz: e5b554f756679350708d67e521861a5e3248f10cc465eeb77159af7bbc7ff5116ad2328343d98f5bd2cf72ef8def6b168d50a475994751157484d2a5111b7a3d
7
+ data.tar.gz: d1826eb7b2baac37a9767680898b811370a74a61f2859fe0b189dc7ef26634e7efc167892d1d720608b823c5335278a5cc3645e3984c72f13cc1340abcaf222a
@@ -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 < ActionController::Base
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
- before_filter :get_class, :set_class_serializer
4
+ include GetsKlass
5
+ before_filter :set_class_serializer
5
6
 
6
7
  # GET
7
8
  def index
@@ -60,22 +61,10 @@ module RestArea
60
61
  def set_class_serializer
61
62
  @serializer = ( '::' + @klass.to_s + "Serializer" ).constantize
62
63
  rescue NameError => e
63
- if e.message =~ /uninitialized constant/
64
- @serializer = false
65
- else
64
+ unless e.message =~ /uninitialized constant/
66
65
  throw e
67
66
  end
68
67
  end
69
68
 
70
- def get_class
71
- @klass = params[:klass].singularize.camelize.constantize
72
- unless RestArea.class_whitelist.include? @klass
73
- raise ActionController::RoutingError.new("Resource Does Not Exist")
74
- end
75
-
76
- @roots = ActionController::Base.helpers.sanitize(params[:klass])
77
- @root = @roots.singularize
78
- end
79
-
80
69
  end
81
70
  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"
@@ -1,3 +1,3 @@
1
1
  module RestArea
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
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: 1.0.0
4
+ version: 1.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-04-30 00:00:00.000000000 Z
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