make_restful 0.0.1.pre1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,34 +1,48 @@
|
|
1
1
|
module MakeRestful::ClassMethods
|
2
2
|
|
3
|
-
def
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
def resource_name
|
4
|
+
resource_class.name.gsub(/(.*::)?(.*)/,"\\2").underscore.pluralize
|
5
|
+
end
|
6
|
+
|
7
|
+
def instance_name
|
8
|
+
resource_name.singularize
|
9
9
|
end
|
10
10
|
|
11
11
|
private
|
12
12
|
|
13
13
|
def resource(klass)
|
14
|
-
|
15
|
-
|
14
|
+
|
15
|
+
before_filter :load_resource
|
16
|
+
before_filter :load_collection, on: [:index]
|
17
|
+
before_filter :load_instance, on: [:show, :destroy, :update]
|
18
|
+
|
19
|
+
begin
|
16
20
|
klass = klass.to_s.camelize.constantize
|
17
|
-
|
18
|
-
|
21
|
+
rescue NameError
|
22
|
+
klass = false
|
23
|
+
end
|
24
|
+
|
25
|
+
if klass
|
26
|
+
self.resource_class = klass
|
19
27
|
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
def find_by(key)
|
32
|
+
self.finder = key.to_sym
|
20
33
|
end
|
21
34
|
|
22
35
|
def allow_methods(*methods)
|
23
|
-
self.
|
24
|
-
methods.flatten
|
25
|
-
end
|
36
|
+
self.allowed_methods = methods.flatten
|
26
37
|
end
|
27
38
|
|
28
39
|
def allow_formats(*formats)
|
29
|
-
self.
|
30
|
-
|
31
|
-
|
40
|
+
self.allowed_formats = formats.flatten
|
41
|
+
end
|
42
|
+
|
43
|
+
def paginates!(opts={})
|
44
|
+
opts.reverse_merge!({ per_page: 25 })
|
45
|
+
self.pagination = opts
|
32
46
|
end
|
33
47
|
|
34
48
|
end
|
@@ -0,0 +1,20 @@
|
|
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
|
data/lib/make_restful/version.rb
CHANGED
data/lib/make_restful.rb
CHANGED
@@ -5,12 +5,16 @@ module MakeRestful
|
|
5
5
|
extend ActiveSupport::Autoload
|
6
6
|
|
7
7
|
autoload :ClassMethods
|
8
|
+
autoload :ControllerAdditions
|
8
9
|
|
9
10
|
included do
|
10
11
|
|
11
|
-
|
12
|
+
cattr_accessor :allowed_methods, :allowed_formats, :finder, :pagination, :resource_class
|
13
|
+
|
14
|
+
resource name.underscore.gsub(/(.*)_controller/,'\1').singularize
|
12
15
|
allow_formats :json, :jsonp, :xml
|
13
16
|
allow_methods :index, :get, :put, :post, :delete
|
17
|
+
find_by :id
|
14
18
|
|
15
19
|
####################################
|
16
20
|
## RESTful Methods
|
@@ -20,36 +24,53 @@ module MakeRestful
|
|
20
24
|
|
21
25
|
# GET /resources
|
22
26
|
def index
|
23
|
-
head :
|
24
|
-
@response =
|
27
|
+
head :method_not_allowed and return unless allow_method? :index, :list
|
28
|
+
@response = { :"#{self.class.resource_name}" => @collection }
|
29
|
+
@response.merge!(
|
30
|
+
{
|
31
|
+
current_page: results.current_page,
|
32
|
+
per_page: results.per_page,
|
33
|
+
total_entries: results.total_entries,
|
34
|
+
}
|
35
|
+
) if self.class.pagination && !params.has_key?(:all)
|
36
|
+
|
37
|
+
if @collection.empty?
|
38
|
+
@response = { error: 'no results found' }
|
39
|
+
@status = :not_found
|
40
|
+
end
|
41
|
+
|
25
42
|
render_formats
|
26
43
|
end
|
27
44
|
|
28
45
|
# POST /resources
|
29
46
|
def create
|
30
|
-
head :
|
31
|
-
@response = resource.create
|
47
|
+
head :method_not_allowed and return unless allow_method? :post, :create
|
48
|
+
@response = @resource.create
|
32
49
|
render_formats
|
33
50
|
end
|
34
51
|
|
35
52
|
# PUT /resources/:id
|
36
53
|
def show
|
37
|
-
head :
|
38
|
-
@response = instance
|
54
|
+
head :method_not_allowed and return unless allow_method? :get, :show
|
55
|
+
@response = @instance
|
56
|
+
unless @response
|
57
|
+
@response = { error: 'record_not_found' }
|
58
|
+
@status = :not_found
|
59
|
+
end
|
39
60
|
render_formats
|
40
61
|
end
|
41
62
|
|
42
63
|
# PUT /resources/:id
|
43
64
|
def update
|
44
|
-
head :
|
45
|
-
@response = instance.update_attributes(params[resource.to_s.underscore])
|
65
|
+
head :method_not_allowed and return unless allow_method? :put, :patch, :update
|
66
|
+
@response = @instance.update_attributes(params[@resource.to_s.underscore])
|
46
67
|
render_formats
|
47
68
|
end
|
48
69
|
|
49
70
|
# DELETE /resources/:id
|
50
71
|
def destroy
|
51
|
-
head :
|
52
|
-
@response = instance.destroy
|
72
|
+
head :method_not_allowed and return unless allow_method? :delete, :destroy
|
73
|
+
@response = @instance.destroy
|
53
74
|
render_formats
|
54
75
|
end
|
55
76
|
|
@@ -57,7 +78,7 @@ module MakeRestful
|
|
57
78
|
def spec
|
58
79
|
@response = {
|
59
80
|
supports: self.class.supported_methods,
|
60
|
-
specification: ( resource.specification if resource.respond_to?(:specification) ) || "No Specification"
|
81
|
+
specification: ( @resource.specification if @resource.respond_to?(:specification) ) || "No Specification"
|
61
82
|
}
|
62
83
|
render_formats
|
63
84
|
end
|
@@ -68,9 +89,9 @@ module MakeRestful
|
|
68
89
|
|
69
90
|
def render_formats
|
70
91
|
respond_to do |format|
|
71
|
-
format.json { render json: @response, callback: ( params[:callback] if allow_format? :jsonp )} if allow_format? :json, :jsonp
|
72
|
-
format.xml { render xml: @response } if allow_format? :xml
|
73
|
-
format.html
|
92
|
+
format.json { render json: @response, status: @status, callback: ( params[:callback] if allow_format? :jsonp )} if allow_format? :json, :jsonp
|
93
|
+
format.xml { render xml: @response, status: @status } if allow_format? :xml
|
94
|
+
format.html { render status: @status } if allow_format? :html
|
74
95
|
end
|
75
96
|
end
|
76
97
|
|
@@ -86,13 +107,34 @@ module MakeRestful
|
|
86
107
|
true if methods.map(&:to_sym).select { |i| allowed_methods.map(&:to_sym).include? i }.present?
|
87
108
|
end
|
88
109
|
|
89
|
-
|
90
|
-
|
91
|
-
@
|
110
|
+
def load_resource
|
111
|
+
@resource = self.class.resource_class
|
112
|
+
@resource.params = params.except(:action, :controller, :id) if @resource.respond_to?(:params=)
|
113
|
+
end
|
114
|
+
|
115
|
+
def load_collection
|
116
|
+
@collection = if self.class.pagination && !params.has_key?(:all)
|
117
|
+
@resource.paginate page: params[:page], per_page: (params[:per_page] || self.class.pagination[:per_page] || 25)
|
118
|
+
else
|
119
|
+
@resource.all
|
120
|
+
end
|
121
|
+
eval("@#{self.class.resource_name} = @collection")
|
122
|
+
end
|
123
|
+
|
124
|
+
def load_instance
|
125
|
+
@instance = eval "@#{self.class.instance_name} = #{@resource.send(:"find_by_#{self.class.finder}", params[:id])}" if params[:id] && @resource.respond_to?(:find)
|
92
126
|
end
|
93
127
|
|
94
128
|
def resource
|
95
129
|
@resource
|
96
130
|
end
|
97
131
|
|
132
|
+
def instance
|
133
|
+
@instance
|
134
|
+
end
|
135
|
+
|
136
|
+
def collection
|
137
|
+
@collection
|
138
|
+
end
|
139
|
+
|
98
140
|
end
|
data/make_restful.gemspec
CHANGED
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: make_restful
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jason Waldrip
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-09-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -27,6 +27,22 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: will_paginate
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
30
46
|
description: ! ' Enables rest on a resource '
|
31
47
|
email:
|
32
48
|
- jason@waldrip.net
|
@@ -41,6 +57,8 @@ files:
|
|
41
57
|
- Rakefile
|
42
58
|
- lib/make_restful.rb
|
43
59
|
- lib/make_restful/class_methods.rb
|
60
|
+
- lib/make_restful/controller_additions.rb
|
61
|
+
- lib/make_restful/controller_additions/oauth_resource.rb
|
44
62
|
- lib/make_restful/version.rb
|
45
63
|
- make_restful.gemspec
|
46
64
|
homepage: http://github.com/jwaldrip/make_restful
|
@@ -58,9 +76,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
76
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
77
|
none: false
|
60
78
|
requirements:
|
61
|
-
- - ! '
|
79
|
+
- - ! '>='
|
62
80
|
- !ruby/object:Gem::Version
|
63
|
-
version:
|
81
|
+
version: '0'
|
64
82
|
requirements: []
|
65
83
|
rubyforge_project:
|
66
84
|
rubygems_version: 1.8.23
|