has_response 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +157 -0
- data/Rakefile +38 -0
- data/lib/has_response/version.rb +3 -0
- data/lib/has_response.rb +4 -0
- metadata +114 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2013 Tom Benner
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
Has Response
|
2
|
+
============
|
3
|
+
Extremely simple API support for Rails models
|
4
|
+
|
5
|
+
Description
|
6
|
+
-----------
|
7
|
+
|
8
|
+
Has Response provides very simple support for turning model instances into JSONifiable hashes for use in API responses. The hashes contain only the attributes that are specified, and they can contain hashes of N-order associations as well.
|
9
|
+
|
10
|
+
Installation
|
11
|
+
------------
|
12
|
+
|
13
|
+
Add Has Response to your Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'has_response'
|
17
|
+
```
|
18
|
+
|
19
|
+
Usage
|
20
|
+
-----
|
21
|
+
|
22
|
+
Include HasResponse and define to\_response in your model:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
class User < ActiveRecord::Base
|
26
|
+
include HasResponse
|
27
|
+
|
28
|
+
def to_response
|
29
|
+
{
|
30
|
+
id: id,
|
31
|
+
username: username,
|
32
|
+
avatar_url: avatar.url(:thumb)
|
33
|
+
}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
There's nothing special here yet, but you can now return JSON of users more easily in controllers:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
class UsersController < ApplicationController
|
42
|
+
def show
|
43
|
+
user = User.find(params[:id])
|
44
|
+
render :json => user.to_response
|
45
|
+
end
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
|
50
|
+
Say you want to include the user's Location in your response, too. Set up Location:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
class Location < ActiveRecord::Base
|
54
|
+
include HasResponse
|
55
|
+
|
56
|
+
def to_response
|
57
|
+
{
|
58
|
+
id: id,
|
59
|
+
city: city,
|
60
|
+
state: state
|
61
|
+
}
|
62
|
+
end
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
And the association:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
class User < ActiveRecord::Base
|
70
|
+
#...
|
71
|
+
belongs_to :location
|
72
|
+
#...
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
Then change `user.to_response` to:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
user.to_response_with(:location)
|
80
|
+
```
|
81
|
+
|
82
|
+
This will return:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
{
|
86
|
+
id: 1,
|
87
|
+
username: "johndoe",
|
88
|
+
avatar_url: "http://mysite.com/myavatar.jpg",
|
89
|
+
location: {
|
90
|
+
id: 1,
|
91
|
+
city: "San Francisco",
|
92
|
+
state: "CA"
|
93
|
+
}
|
94
|
+
}
|
95
|
+
```
|
96
|
+
|
97
|
+
If you want to add additional columns or method values to any responses, those can be included just like associations are included. If User responds to a method named `posts_count`, you can easily include that, too:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
user.to_response_with(:location, :posts_count)
|
101
|
+
```
|
102
|
+
|
103
|
+
This will return:
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
{
|
107
|
+
id: 1,
|
108
|
+
username: "johndoe",
|
109
|
+
avatar_url: "http://mysite.com/myavatar.jpg",
|
110
|
+
location: {
|
111
|
+
id: 1,
|
112
|
+
city: "San Francisco",
|
113
|
+
state: "CA"
|
114
|
+
},
|
115
|
+
posts_count: 2
|
116
|
+
}
|
117
|
+
```
|
118
|
+
|
119
|
+
You can include associations of N-order. To include a User's Posts, and those Posts' Comments, including their User, use:
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
user.to_response_with(:location, :posts => {:comments => :user})
|
123
|
+
```
|
124
|
+
|
125
|
+
This will return:
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
{
|
129
|
+
id: 1,
|
130
|
+
username: "johndoe",
|
131
|
+
avatar_url: "http://mysite.com/myavatar.jpg",
|
132
|
+
location: ...,
|
133
|
+
posts: [
|
134
|
+
{
|
135
|
+
id: 1,
|
136
|
+
title: "My First Post",
|
137
|
+
content: "My first post!",
|
138
|
+
comments: [
|
139
|
+
{
|
140
|
+
id: 1,
|
141
|
+
content: "Great post!",
|
142
|
+
user: {
|
143
|
+
id: 2,
|
144
|
+
username: "Jane Doe",
|
145
|
+
avatar_url: "http://mysite.com/myavatar.jpg"
|
146
|
+
}
|
147
|
+
}
|
148
|
+
]
|
149
|
+
}
|
150
|
+
]
|
151
|
+
}
|
152
|
+
```
|
153
|
+
|
154
|
+
License
|
155
|
+
-------
|
156
|
+
|
157
|
+
Has Response is released under the MIT License. Please see the MIT-LICENSE file for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'HasResponse'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
Bundler::GemHelper.install_tasks
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
|
30
|
+
Rake::TestTask.new(:test) do |t|
|
31
|
+
t.libs << 'lib'
|
32
|
+
t.libs << 'test'
|
33
|
+
t.pattern = 'test/**/*_test.rb'
|
34
|
+
t.verbose = false
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
task :default => :test
|
data/lib/has_response.rb
ADDED
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_response
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tom Benner
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-12-08 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
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activerecord
|
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'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Extremely simple API support for Rails models
|
79
|
+
email:
|
80
|
+
- tombenner@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- lib/has_response/version.rb
|
86
|
+
- lib/has_response.rb
|
87
|
+
- MIT-LICENSE
|
88
|
+
- Rakefile
|
89
|
+
- README.md
|
90
|
+
homepage: https://github.com/tombenner/has_response
|
91
|
+
licenses: []
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 1.8.24
|
111
|
+
signing_key:
|
112
|
+
specification_version: 3
|
113
|
+
summary: Extremely simple API support for Rails models
|
114
|
+
test_files: []
|