make_restful 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,33 +8,26 @@ module MakeRestful::ClassMethods
8
8
  resource_name.singularize
9
9
  end
10
10
 
11
- def supported_methods
12
- methods ||= []
13
- methods << { method: :get, path: "/", returns: "collection" } if allow_method?(:list) || allow_method?(:index)
14
- methods << { method: :post, path: "/", returns: "resource" } if allow_method?(:create) || allow_method?(:post)
15
- methods << { method: :get, path: "/:id", returns: "resource" } if allow_method?(:show) || allow_method?(:get)
16
- methods << { method: :put, path: "/:id", returns: "resource" } if allow_method?(:update) || allow_method?(:put)
17
- methods << { method: :delete, path: "/:id", returns: "success" } if allow_method?(:destroy) || allow_method?(:delete)
18
- methods
19
- end
20
-
21
11
  # Is a format allowed?
22
12
  def allow_format?(*formats)
23
- true if formats.map(&:to_sym).select { |i| allowed_formats.map(&:to_sym).include? i }.present?
13
+ true if formats.flatten.map(&:to_sym).select { |i| allowed_formats.map(&:to_sym).include? i }.present?
24
14
  end
25
15
 
26
16
  # Is a method allowed?
27
17
  def allow_method?(*methods)
28
- true if methods.map(&:to_sym).select { |i| allowed_methods.map(&:to_sym).include? i }.present?
18
+ true if methods.flatten.map(&:to_sym).select { |i| allowed_methods.map(&:to_sym).include? i }.present?
29
19
  end
30
20
 
21
+ alias :allow_action? :allow_method?
22
+
31
23
  private
32
24
 
33
25
  def resource(klass)
34
26
 
27
+ before_filter :verify_method!
35
28
  before_filter :load_resource
36
- before_filter :load_collection, on: [:index]
37
- before_filter :load_instance, on: [:show, :destroy, :update]
29
+ before_filter :load_collection, only: [:index]
30
+ before_filter :load_instance, only: [:show, :destroy, :update]
38
31
 
39
32
  begin
40
33
  klass = klass.to_s.camelize.constantize
@@ -52,13 +45,25 @@ module MakeRestful::ClassMethods
52
45
  self.finder = key.to_sym
53
46
  end
54
47
 
55
- def allow_methods(*methods)
56
- self.allowed_methods = methods.flatten
48
+ def allow_method(*methods)
49
+ self.allowed_methods += methods.flatten
50
+ end
51
+ alias :allow_methods :allow_method
52
+
53
+ def allow_format(*formats)
54
+ self.allowed_formats += formats.flatten
55
+ end
56
+ alias :allow_formats :allow_format
57
+
58
+ def deny_method(*methods)
59
+ self.allowed_methods -= methods
57
60
  end
61
+ alias :deny_methods :deny_method
58
62
 
59
- def allow_formats(*formats)
60
- self.allowed_formats = formats.flatten
63
+ def deny_format(*methods)
64
+ self.allowed_formats -= methods
61
65
  end
66
+ alias :deny_formats :deny_format
62
67
 
63
68
  def paginates!(opts={})
64
69
  opts.reverse_merge!({ per_page: 25 })
@@ -1,5 +1,7 @@
1
1
  module MakeRestful::Loaders
2
2
 
3
+ private
4
+
3
5
  def load_resource
4
6
  @resource = self.class.resource_class
5
7
  @resource.params = params.except(:action, :controller, :id) if @resource.respond_to?(:params=)
@@ -7,19 +9,64 @@ module MakeRestful::Loaders
7
9
 
8
10
  def load_collection
9
11
  @collection = if self.class.pagination && !params.has_key?(:all)
10
- @resource.paginate page: params[:page], per_page: (params[:per_page] || self.class.pagination[:per_page] || 25)
11
- else
12
- @resource.all
13
- end
12
+ paginated_collection = @resource.paginate page: params[:page], per_page: (params[:per_page] || self.class.pagination[:per_page] || 25)
13
+ { :"#{self.class.resource_name}" => collection,
14
+ pagination: {
15
+ current_page: paginated_collection.current_page,
16
+ per_page: paginated_collection.per_page,
17
+ total_entries: paginated_collection.total_entries,
18
+ total_pages: paginated_collection.total_pages
19
+ }
20
+ }
21
+ else
22
+ @resource.all
23
+ end
24
+
25
+ if @collection.empty?
26
+ @response = { error: 'no results found' }
27
+ @status = :not_found
28
+ end
29
+
14
30
  eval("@#{self.class.resource_name} = @collection")
15
31
  end
16
32
 
17
33
  def load_instance(opts={})
18
- if (params[opts[:finder]] || params[:id]) && @resource.respond_to?(:find)
19
- finder = (opts[:finder] || self.class.finder) == :id ? :find : "find_by_#{(opts[:finder] || self.class.finder)}".to_sym
20
- @instance = @resource.send finder, (params[opts[:finder]] || params[:id])
34
+
35
+ finder = opts[:finder] || self.class.finder
36
+ finder_value = params[finder]
37
+
38
+ if finder && @resource.respond_to?(:find)
39
+ @instance = @resource.where(finder.to_sym => finder_value).first
21
40
  eval "@#{self.class.instance_name} = @instance"
22
41
  end
42
+
43
+ unless @instance
44
+ @response = { error: "Could not find record with #{finder}: #{finder_value}" }
45
+ @status = :not_found
46
+ render_formats(load_template: false) and return
47
+ end
48
+
49
+ end
50
+
51
+ def verify_method!
52
+ return if allow_method? params[:action]
53
+ @response = { error: "Method not allowed"}
54
+ @status = :method_not_allowed
55
+ render
56
+ end
57
+
58
+ alias :verify_action! :verify_method!
59
+
60
+ def method_missing(m, *args, &block)
61
+ if /load_instance_by_(?<finder>[a-z_]+)/ =~ m.to_s
62
+ load_instance(finder: finder.to_sym)
63
+ else
64
+ super
65
+ end
66
+ end
67
+
68
+ def respond_to_missing?(method_name, include_private = false)
69
+ true if /load_instance_by_(?<finder>[a-z_]+)/ =~ method_name.to_s
23
70
  end
24
71
 
25
72
  end
@@ -3,11 +3,17 @@ module MakeRestful::Rendering
3
3
  private
4
4
 
5
5
  ## Rendering
6
- def render_formats
6
+ def render(options = {}, deprecated_status = nil, &block)
7
+
8
+ super and return if template_exists?([params[:controller], params[:action]].join('/')) && allow_format?(formats) unless options[:load_template] == false
9
+
10
+ # Set the response from the chain
11
+ response = @response || @instance || @collection
12
+
7
13
  respond_to do |format|
8
- format.json { render json: @response, status: @status, callback: ( params[:callback] if allow_format? :jsonp )} if allow_format? :json, :jsonp
9
- format.xml { render xml: @response, status: @status } if allow_format? :xml
10
- format.html { render status: @status } if allow_format? :html
14
+ format.json { super json: response, status: @status, callback: ( params[:callback] if allow_format? :jsonp )} if allow_format? :json, :jsonp
15
+ format.xml { super xml: response, status: @status } if allow_format? :xml
16
+ format.any { super }
11
17
  end
12
18
  end
13
19
 
@@ -5,64 +5,33 @@ module MakeRestful::RestMethods
5
5
  ####################################
6
6
 
7
7
  # GET /resources
8
+ # Verified by :list
8
9
  def index
9
- head :method_not_allowed and return unless allow_method? :index, :list
10
- @response = { :"#{self.class.resource_name}" => @collection }
11
- @response.merge!(
12
- {
13
- pagination: {
14
- current_page: @collection.current_page,
15
- per_page: @collection.per_page,
16
- total_entries: @collection.total_entries,
17
- total_pages: @collection.total_pages
18
- }
19
- }
20
- ) if self.class.pagination && !params.has_key?(:all)
21
-
22
- if @collection.empty?
23
- @response = { error: 'no results found' }
24
- @status = :not_found
25
- end
26
-
27
- render_formats
28
10
  end
29
11
 
30
12
  # POST /resources
13
+ # Verified by :create
31
14
  def create
32
- head :method_not_allowed and return unless allow_method? :post, :create
33
- @response = @resource.create
34
- render_formats
15
+ @instance = @resource.create
35
16
  end
36
17
 
37
- # PUT /resources/:id
18
+ # GET /resources/:id
38
19
  def show
39
- head :method_not_allowed and return unless allow_method? :get, :show
40
- @response = @instance
41
- unless @response
42
- @response = { error: 'record_not_found' }
43
- @status = :not_found
44
- end
45
- render_formats
46
20
  end
47
21
 
48
22
  # PUT /resources/:id
49
23
  def update
50
- head :method_not_allowed and return unless allow_method? :put, :patch, :update
51
- @response = @instance.update_attributes(params[@resource.to_s.underscore])
52
- render_formats
24
+ @instance.update_attributes(params[@resource.to_s.underscore])
53
25
  end
54
26
 
55
27
  # DELETE /resources/:id
56
28
  def destroy
57
- head :method_not_allowed and return unless allow_method? :delete, :destroy
58
- @response = @instance.destroy
59
- render_formats
29
+ @response = { success: @instance.destroy }
60
30
  end
61
31
 
62
32
  # OPTIONS /resources
63
33
  def spec
64
34
  @response = specification
65
- render_formats
66
35
  end
67
36
 
68
37
  end
@@ -1,20 +1,20 @@
1
1
  class MakeRestful::Specification::Builder
2
2
 
3
- attr_reader :controller, :_methods, :_description, :_formats, :_attributes, :_resource, :_default_methods, :_default_attributes, :_default_formats
3
+ attr_reader :_controller_, :_methods_, :_description_, :_formats_, :_attributes_, :_resource_, :_default_methods_, :_default_attributes_, :_default_formats_
4
+
5
+ private
4
6
 
5
7
  def initialize(controller, &block)
6
- @_controller = controller
7
- @_methods = []
8
- @_default_methods = controller.supported_methods.collect do |m|
9
- method m[:method], path: m[:path], returns: m[:returns]
10
- end
11
- @_methods = []
12
- @_attributes = []
13
- @_default_attributes = controller.resource_class.new.attributes.keys
14
- @_default_formats = controller.allowed_formats
15
- @_formats = []
16
- @_description = "Api for #{controller.resource_name.titleize}"
17
- @_resource = controller.instance_name
8
+ @_controller_ = controller
9
+ @_methods_ = []
10
+ @_default_methods_ = default_supported_methods
11
+ @_methods_ = []
12
+ @_attributes_ = []
13
+ @_default_attributes_ = controller.resource_class.new.attributes.keys
14
+ @_default_formats_ = controller.allowed_formats
15
+ @_formats_ = []
16
+ @_description_ = "Api for #{controller.resource_name.titleize}"
17
+ @_resource_ = controller.instance_name
18
18
  instance_eval(&block) if block_given?
19
19
  end
20
20
 
@@ -24,43 +24,51 @@ class MakeRestful::Specification::Builder
24
24
  path: options[:path],
25
25
  returns: options[:returns]
26
26
  }
27
- @_methods << method
27
+ @_methods_ << method
28
28
  method
29
29
  end
30
30
 
31
31
  def description(string)
32
- @_description = string
32
+ @_description_ = string
33
33
  end
34
34
 
35
35
  def resource(string)
36
- @_resource = string
36
+ @_resource_ = string
37
37
  end
38
38
 
39
39
  def attribute(attr)
40
- @_attributes << attr
40
+ @_attributes_ << attr
41
41
  attr
42
42
  end
43
43
 
44
44
  def attributes(*args)
45
- @_attributes += args.flatten
45
+ @_attributes_ += args.flatten
46
46
  end
47
47
 
48
48
  def format(attr)
49
- @_formats << attr
49
+ @_formats_ << attr
50
50
  attr
51
51
  end
52
52
 
53
53
  def formats(*args)
54
- @_formats += args
54
+ @_formats_ += args
55
+ end
56
+
57
+ def default_supported_methods
58
+ method :get, path: "/", returns: "collection" if _controller_.allow_method?(:list) || _controller_.allow_method?(:index)
59
+ method :post, path: "/", returns: "resource" if _controller_.allow_method?(:create) || _controller_.allow_method?(:post)
60
+ method :get, path: "/:id", returns: "resource" if _controller_.allow_method?(:show) || _controller_.allow_method?(:get)
61
+ method :put, path: "/:id", returns: "resource" if _controller_.allow_method?(:update) || _controller_.allow_method?(:put)
62
+ method :delete, path: "/:id", returns: "success" if _controller_.allow_method?(:destroy) || _controller_.allow_method?(:delete)
55
63
  end
56
64
 
57
65
  def render
58
66
  {
59
- resource: _resource,
60
- formats: _formats.present? ? _formats : _default_formats,
61
- description: _description,
62
- attributes: _attributes.present? ? _attributes : _default_attributes,
63
- methods: _methods.present? ? _methods : _default_methods
67
+ resource: _resource_,
68
+ formats: _formats_.present? ? _formats_ : _default_formats_,
69
+ description: _description_,
70
+ attributes: _attributes_.present? ? _attributes_ : _default_attributes_,
71
+ methods: _methods_.present? ? _methods_ : _default_methods_
64
72
  }
65
73
  end
66
74
 
@@ -1,3 +1,3 @@
1
1
  module MakeRestful
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/make_restful.rb CHANGED
@@ -5,7 +5,6 @@ module MakeRestful
5
5
  extend ActiveSupport::Autoload
6
6
 
7
7
  autoload :ClassMethods
8
- autoload :ControllerAdditions
9
8
  autoload :Loaders
10
9
  autoload :RestMethods
11
10
  autoload :Rendering
@@ -24,9 +23,9 @@ module MakeRestful
24
23
  attr_reader :resource, :instance, :collection
25
24
 
26
25
  resource name.underscore.gsub(/(.*)_controller/,'\1').singularize
27
- allow_formats :json, :jsonp, :xml
28
- allow_methods :index, :get, :put, :post, :delete
29
- find_by :id
26
+ self.allowed_formats = [:json, :jsonp, :xml]
27
+ self.allowed_methods = [:index, :get, :put, :post, :delete]
28
+ self.finder = :id
30
29
 
31
30
  end
32
31
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: make_restful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-28 00:00:00.000000000 Z
12
+ date: 2012-10-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -73,8 +73,6 @@ files:
73
73
  - Rakefile
74
74
  - lib/make_restful.rb
75
75
  - lib/make_restful/class_methods.rb
76
- - lib/make_restful/controller_additions.rb
77
- - lib/make_restful/controller_additions/oauth_resource.rb
78
76
  - lib/make_restful/loaders.rb
79
77
  - lib/make_restful/rendering.rb
80
78
  - lib/make_restful/rest_methods.rb
@@ -1,20 +0,0 @@
1
- module MakeRestful::ControllerAdditions::OauthResource
2
- require 'oauth_resource'
3
- extend ActiveSupport::Concern
4
-
5
- included do ; end
6
-
7
- module ClassMethods
8
-
9
- def authorize_resource!(token, opts={})
10
- skip_before_filter :load_instance, :load_collection
11
- before_filter(opts) do
12
- @resource = @resource.authorize(send(token))
13
- load_instance if params[:action] =~ /show|destroy|update/
14
- load_collection if params[:action] =~ /index/
15
- end
16
- end
17
-
18
- end
19
-
20
- end
@@ -1,6 +0,0 @@
1
- module MakeRestful::ControllerAdditions
2
- extend ActiveSupport::Autoload
3
-
4
- autoload :OauthResource
5
-
6
- end