serviceable 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +24 -0
- data/README.md +37 -0
- data/lib/serviceable.rb +92 -0
- metadata +66 -0
data/LICENSE
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
LICENSE
|
2
|
+
|
3
|
+
The MIT License
|
4
|
+
|
5
|
+
Copyright (c) 2011-2013 Aubrey Goodman and VerifIP, LLC
|
6
|
+
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
9
|
+
in the Software without restriction, including without limitation the rights
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
12
|
+
furnished to do so, subject to the following conditions:
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall be included in
|
15
|
+
all copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
23
|
+
THE SOFTWARE.
|
24
|
+
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
Serviceable
|
2
|
+
=========
|
3
|
+
|
4
|
+
Serviceable aims to reduce code duplication for common patterns, such as JSON/XML
|
5
|
+
API endpoints. Instead of repeating the same patterns in multiple controllers and
|
6
|
+
trying to maintain that over time, we extracted those patterns into a module.
|
7
|
+
|
8
|
+
|
9
|
+
class PostsController < ApplicationController
|
10
|
+
|
11
|
+
include Serviceable
|
12
|
+
acts_as_service :post
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
resources :posts
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
Standard CRUD
|
21
|
+
-------------
|
22
|
+
|
23
|
+
POST /posts.json
|
24
|
+
GET /posts.json
|
25
|
+
GET /posts/1.json
|
26
|
+
PUT /posts/1.json
|
27
|
+
DELETE /posts/1.json
|
28
|
+
|
29
|
+
Query Params
|
30
|
+
------------
|
31
|
+
|
32
|
+
GET /posts.json
|
33
|
+
[{"id":1,"title":"First Post!","body":"Feels good to be first","created_at":"20130727T16:26:00Z"}]
|
34
|
+
|
35
|
+
GET /posts.json?only=id,title
|
36
|
+
[{"id":1,"title","First post!"}]
|
37
|
+
|
data/lib/serviceable.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
module Serviceable
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
# Serviceable Usage
|
10
|
+
#
|
11
|
+
# Controller:
|
12
|
+
# class PostsController
|
13
|
+
# acts_as_service :post
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
def acts_as_service(object,options={})
|
17
|
+
|
18
|
+
before_filter :assign_new_instance, :only => :create
|
19
|
+
before_filter :assign_existing_instance, :only => [ :show, :update, :destroy ]
|
20
|
+
before_filter :assign_instances, :only => :index
|
21
|
+
|
22
|
+
define_method("index") do
|
23
|
+
respond_to do |format|
|
24
|
+
format.json { eval "render :json => @#{object.to_s.pluralize}.to_json(merge_options(options[:index]))" }
|
25
|
+
format.xml { eval "render :xml => @#{object.to_s.pluralize}.to_xml(merge_options(options[:index]))" }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
define_method("create") do
|
30
|
+
respond_to do |format|
|
31
|
+
if eval "@#{object}.save"
|
32
|
+
format.json { eval "render :json => @#{object}" }
|
33
|
+
format.xml { eval "render :xml => @#{object}" }
|
34
|
+
else
|
35
|
+
format.json { eval "render :json => { :errors => @#{object}.errors.full_messages }, :status => :unprocessable_entity" }
|
36
|
+
format.xml { eval "render :xml => { :errors => @#{object}.errors.full_messages }, :status => :unprocessable_entity" }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
define_method("show") do
|
42
|
+
respond_to do |format|
|
43
|
+
format.json { eval "render :json => @#{object}.to_json(options[:show])" }
|
44
|
+
format.xml { eval "render :xml => @#{object}.to_xml(options[:show])" }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
define_method("update") do
|
49
|
+
respond_to do |format|
|
50
|
+
if eval "@#{object}.update_attributes(params[object])"
|
51
|
+
format.json { head :ok }
|
52
|
+
format.xml { head :ok }
|
53
|
+
else
|
54
|
+
format.json { eval "render :json => { :errors => @#{object}.errors.full_messages }, :status => :unprocessable_entity" }
|
55
|
+
format.xml { eval "render :xml => { :errors => @#{object}.errors.full_messages }, :status => :unprocessable_entity" }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
define_method("destroy") do
|
61
|
+
eval "@#{object}.destroy"
|
62
|
+
|
63
|
+
respond_to do |format|
|
64
|
+
format.json { head :ok }
|
65
|
+
format.xml { head :ok }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
define_method("merge_options") do |options={}|
|
70
|
+
merged_options = options || {}
|
71
|
+
for key in [:only, :except]
|
72
|
+
merged_options = merged_options.merge({key => params[key].split(",")}) if params[key]
|
73
|
+
end
|
74
|
+
return merged_options
|
75
|
+
end
|
76
|
+
|
77
|
+
define_method("assign_existing_instance") do
|
78
|
+
eval "@#{object} = object.to_s.camelize.constantize.find(params[:id])"
|
79
|
+
end
|
80
|
+
|
81
|
+
define_method("assign_new_instance") do
|
82
|
+
eval "@#{object} = object.to_s.camelize.constantize.new(params[:#{object}])"
|
83
|
+
end
|
84
|
+
|
85
|
+
define_method("assign_instances") do
|
86
|
+
eval "@#{object.to_s.pluralize} = object.to_s.camelize.constantize.scoped"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: serviceable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 9
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: "0.1"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Aubrey Goodman
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2013-07-27 00:00:00 Z
|
18
|
+
dependencies: []
|
19
|
+
|
20
|
+
description: Decorate your controller classes with acts_as_service :model_name, and instantly support JSON/XML CRUD interface. Override any callback method to customize your endpoint. Allow client to specify response contents using query string filter parameters.
|
21
|
+
email: aubrey.goodman@gmail.com
|
22
|
+
executables: []
|
23
|
+
|
24
|
+
extensions: []
|
25
|
+
|
26
|
+
extra_rdoc_files: []
|
27
|
+
|
28
|
+
files:
|
29
|
+
- lib/serviceable.rb
|
30
|
+
- LICENSE
|
31
|
+
- README.md
|
32
|
+
homepage: https://github.com/verifip/serviceable
|
33
|
+
licenses: []
|
34
|
+
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
hash: 3
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.8.24
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: Standardized Rails web services with design-time configuration and query string filtering support
|
65
|
+
test_files: []
|
66
|
+
|