make_restful 0.0.1.pre1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/make_restful/class_methods.rb +34 -0
- data/lib/make_restful/version.rb +3 -0
- data/lib/make_restful.rb +98 -0
- data/make_restful.gemspec +20 -0
- metadata +71 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jason Waldrip
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# RestfulResource
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'restful_resource'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install restful_resource
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module MakeRestful::ClassMethods
|
2
|
+
|
3
|
+
def authorize_resource!
|
4
|
+
before_filter do
|
5
|
+
@resource = @resource.authorize(auth_token)
|
6
|
+
@resource.params = params if resource.respond_to?(:params=)
|
7
|
+
@instance = @resource.find(params[:id]) if params[:id] && @resource.respond_to?(:find)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def resource(klass)
|
14
|
+
before_filter do
|
15
|
+
resource.params = params.except(:action, :controller, :id) if resource.respond_to?(:params=)
|
16
|
+
klass = klass.to_s.camelize.constantize
|
17
|
+
@resource ||= klass
|
18
|
+
@instance = resource.find(params[:id]) if params[:id] && @resource.respond_to?(:find)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def allow_methods(*methods)
|
23
|
+
self.send :define_method, :allowed_methods do
|
24
|
+
methods.flatten
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def allow_formats(*formats)
|
29
|
+
self.send :define_method, :allowed_formats do
|
30
|
+
formats.flatten
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
data/lib/make_restful.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
require "make_restful/version"
|
2
|
+
|
3
|
+
module MakeRestful
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
extend ActiveSupport::Autoload
|
6
|
+
|
7
|
+
autoload :ClassMethods
|
8
|
+
|
9
|
+
included do
|
10
|
+
|
11
|
+
resource name.underscore.gsub(/(.*\/)?(.*)_controller/,'\2').singularize
|
12
|
+
allow_formats :json, :jsonp, :xml
|
13
|
+
allow_methods :index, :get, :put, :post, :delete
|
14
|
+
|
15
|
+
####################################
|
16
|
+
## RESTful Methods
|
17
|
+
####################################
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
# GET /resources
|
22
|
+
def index
|
23
|
+
head :not_supported and return unless allow_method? :index, :list
|
24
|
+
@response = resource.all
|
25
|
+
render_formats
|
26
|
+
end
|
27
|
+
|
28
|
+
# POST /resources
|
29
|
+
def create
|
30
|
+
head :not_supported and return unless allow_method? :post, :create
|
31
|
+
@response = resource.create
|
32
|
+
render_formats
|
33
|
+
end
|
34
|
+
|
35
|
+
# PUT /resources/:id
|
36
|
+
def show
|
37
|
+
head :not_supported and return unless allow_method? :get, :show
|
38
|
+
@response = instance
|
39
|
+
render_formats
|
40
|
+
end
|
41
|
+
|
42
|
+
# PUT /resources/:id
|
43
|
+
def update
|
44
|
+
head :not_supported and return unless allow_method? :put, :patch, :update
|
45
|
+
@response = instance.update_attributes(params[resource.to_s.underscore])
|
46
|
+
render_formats
|
47
|
+
end
|
48
|
+
|
49
|
+
# DELETE /resources/:id
|
50
|
+
def destroy
|
51
|
+
head :not_supported and return unless allow_method? :delete, :destroy
|
52
|
+
@response = instance.destroy
|
53
|
+
render_formats
|
54
|
+
end
|
55
|
+
|
56
|
+
# OPTIONS /resources
|
57
|
+
def spec
|
58
|
+
@response = {
|
59
|
+
supports: self.class.supported_methods,
|
60
|
+
specification: ( resource.specification if resource.respond_to?(:specification) ) || "No Specification"
|
61
|
+
}
|
62
|
+
render_formats
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
## Rendering
|
68
|
+
|
69
|
+
def render_formats
|
70
|
+
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
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Is a format allowed?
|
78
|
+
|
79
|
+
def allow_format?(*formats)
|
80
|
+
true if formats.map(&:to_sym).select { |i| allowed_formats.map(&:to_sym).include? i }.present?
|
81
|
+
end
|
82
|
+
|
83
|
+
# Is a method allowed?
|
84
|
+
|
85
|
+
def allow_method?(*methods)
|
86
|
+
true if methods.map(&:to_sym).select { |i| allowed_methods.map(&:to_sym).include? i }.present?
|
87
|
+
end
|
88
|
+
|
89
|
+
# The Instance
|
90
|
+
def instance
|
91
|
+
@instance
|
92
|
+
end
|
93
|
+
|
94
|
+
def resource
|
95
|
+
@resource
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'make_restful/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "make_restful"
|
8
|
+
gem.version = MakeRestful::VERSION
|
9
|
+
gem.authors = ["Jason Waldrip"]
|
10
|
+
gem.email = ["jason@waldrip.net"]
|
11
|
+
gem.description = %q{ Enables rest on a resource }
|
12
|
+
gem.summary = %q{ Enables rest on a resource }
|
13
|
+
gem.homepage = "http://github.com/jwaldrip/make_restful"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.add_dependency "activesupport"
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: make_restful
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.pre1
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jason Waldrip
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: ! ' Enables rest on a resource '
|
31
|
+
email:
|
32
|
+
- jason@waldrip.net
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/make_restful.rb
|
43
|
+
- lib/make_restful/class_methods.rb
|
44
|
+
- lib/make_restful/version.rb
|
45
|
+
- make_restful.gemspec
|
46
|
+
homepage: http://github.com/jwaldrip/make_restful
|
47
|
+
licenses: []
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>'
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 1.3.1
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.8.23
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Enables rest on a resource
|
70
|
+
test_files: []
|
71
|
+
has_rdoc:
|