jrb_handler 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +11 -0
- data/LICENSE +20 -0
- data/README.markdown +102 -0
- data/jrb_handler.gemspec +33 -0
- data/lib/active_support_extensions.rb +11 -0
- data/lib/jrb_handler.rb +18 -0
- data/lib/jrb_handler/jrb_partial_renderer.rb +33 -0
- data/lib/jrb_handler/renderers.rb +31 -0
- data/lib/jrb_handler/template_handler.rb +16 -0
- data/lib/jrb_handler/version.rb +4 -0
- metadata +149 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Paul Sadauskas
|
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.markdown
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# JRB Templates
|
2
|
+
|
3
|
+
JRB Templates makes it easy to write JSON web services in Rails. Rather than maintaining two views for the collection and the individual resource, or rendering a third via a partial, you only have to write a single `.jrb` view.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Either
|
8
|
+
|
9
|
+
$ gem install jrb_template
|
10
|
+
|
11
|
+
or add
|
12
|
+
|
13
|
+
gem jrb_template
|
14
|
+
|
15
|
+
to your `./Gemfile`
|
16
|
+
|
17
|
+
|
18
|
+
## Examples
|
19
|
+
|
20
|
+
Using the new Ruby 1.9 hash format, you can just write something that looks a lot like JSON:
|
21
|
+
|
22
|
+
# app/views/books/book.json.jrb
|
23
|
+
{
|
24
|
+
href: book_url(book),
|
25
|
+
_type: "Book",
|
26
|
+
title: book.title
|
27
|
+
author_href: author_url(book.author),
|
28
|
+
synopsis: book.summary,
|
29
|
+
isbn: format_isbn(book.isbn),
|
30
|
+
created_at: book.created_at,
|
31
|
+
updated_at: book.updated_at
|
32
|
+
}
|
33
|
+
|
34
|
+
|
35
|
+
In Ruby 1.8, you have to use the old hash format, and the order gets jumbled.
|
36
|
+
|
37
|
+
Then, in your controller:
|
38
|
+
|
39
|
+
# app/controllers/books_controller.rb
|
40
|
+
|
41
|
+
class BooksController < ApplicationController
|
42
|
+
|
43
|
+
def index
|
44
|
+
@books = Book.all
|
45
|
+
|
46
|
+
respond_to do |format|
|
47
|
+
format.html
|
48
|
+
format.json { render :jrb_collection => @books }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def show
|
53
|
+
@book = Book.find(params[:id])
|
54
|
+
|
55
|
+
respond_to do |format|
|
56
|
+
format.html
|
57
|
+
format.json { render :jrb => @book }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
*Note: I do not recommend using application/json as the mime type extension. Please see [Versioning REST Web Services][].*
|
64
|
+
|
65
|
+
Results in the following json:
|
66
|
+
|
67
|
+
# GET /books
|
68
|
+
{
|
69
|
+
href: "http://example.com/books",
|
70
|
+
count: 1,
|
71
|
+
items: [
|
72
|
+
{
|
73
|
+
href: "http://example.com/books/1
|
74
|
+
_type: "Book",
|
75
|
+
title: "Death From the Skies!",
|
76
|
+
author_href: "http://example.com/authors/plait_philip_c".
|
77
|
+
synopsis: " These Are the Ways the World Will End...".
|
78
|
+
isbn: "978-0670019977",
|
79
|
+
created_at: "2010-12-13T18:44:22Z",
|
80
|
+
updated_at: "2010-12-13T18:44:22Z"
|
81
|
+
}
|
82
|
+
]
|
83
|
+
}
|
84
|
+
|
85
|
+
# GET /books/1
|
86
|
+
{
|
87
|
+
href: "http://example.com/books/1
|
88
|
+
_type: "Book",
|
89
|
+
title: "Death From the Skies!",
|
90
|
+
author_href: "http://example.com/authors/plait_philip_c".
|
91
|
+
synopsis: " These Are the Ways the World Will End...".
|
92
|
+
isbn: "978-0670019977",
|
93
|
+
created_at: "2010-12-13T18:44:22Z",
|
94
|
+
updated_at: "2010-12-13T18:44:22Z"
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
[Versioning REST Web Services]: http://barelyenough.org/blog/2008/05/versioning-rest-web-services/
|
data/jrb_handler.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
|
2
|
+
# -*- encoding: utf-8 -*-
|
3
|
+
lib = File.expand_path('../lib/', __FILE__)
|
4
|
+
$:.unshift lib unless $:.include?(lib)
|
5
|
+
|
6
|
+
require "jrb_handler/version"
|
7
|
+
|
8
|
+
Gem::Specification.new do |s|
|
9
|
+
s.name = "jrb_handler"
|
10
|
+
s.version = JrbHandler::VERSION
|
11
|
+
s.platform = Gem::Platform::RUBY
|
12
|
+
s.authors = ["Paul Sadauskas"]
|
13
|
+
s.email = ["psadauskas@gmail.com"]
|
14
|
+
s.homepage = "http://github.com/paul/jrb_handler"
|
15
|
+
s.summary = "A Rails template handler and renderer for JSON"
|
16
|
+
s.description = <<-DESC
|
17
|
+
Jrb Handler makes it easy to write JSON web services in Rails 3. Keep your views in views, not in your Model#to_json.
|
18
|
+
DESC
|
19
|
+
|
20
|
+
s.required_rubygems_version = ">= 1.3.6"
|
21
|
+
|
22
|
+
s.add_development_dependency "rspec", ">= 2.0.0"
|
23
|
+
s.add_development_dependency "rspec-rails", ">= 2.0.0"
|
24
|
+
s.add_development_dependency "bundler"
|
25
|
+
|
26
|
+
s.add_dependency "activesupport", ">= 3.0.3"
|
27
|
+
s.add_dependency "actionpack", ">= 3.0.3"
|
28
|
+
|
29
|
+
s.files = `git ls-files`.split("\n")
|
30
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
31
|
+
s.require_paths = ['lib']
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
|
2
|
+
require 'active_support/time_with_zone'
|
3
|
+
|
4
|
+
# Personal preference, I like my JSON timestamps in UTC, and xmlschema
|
5
|
+
# format, eg "2010-12-13T18:00:00Z"
|
6
|
+
class ActiveSupport::TimeWithZone
|
7
|
+
def to_json(options = nil)
|
8
|
+
utc.xmlschema.to_json
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
data/lib/jrb_handler.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
|
2
|
+
module JrbHandler
|
3
|
+
|
4
|
+
end
|
5
|
+
|
6
|
+
unless respond_to?(:require_relative)
|
7
|
+
# Ripped right from 1.9 source
|
8
|
+
def require_relative(path)
|
9
|
+
require File.join(File.dirname(caller[0]), path.to_str)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
require_relative 'jrb_handler/template_handler'
|
14
|
+
require_relative 'jrb_handler/jrb_partial_renderer'
|
15
|
+
require_relative 'jrb_handler/renderers'
|
16
|
+
|
17
|
+
|
18
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module JrbHandler
|
2
|
+
|
3
|
+
# Since I can't get the Rails team interested in making these changes, I have to monkey
|
4
|
+
# patch some privatish methods in AV::Partials::PartialRenderer. Works in 3.0.1, but I
|
5
|
+
# expect it to break frequently.
|
6
|
+
class JrbPartialRenderer < ActionView::Partials::PartialRenderer
|
7
|
+
|
8
|
+
# Skip rendering the results into a string, just leave it as an array.
|
9
|
+
def render_collection
|
10
|
+
return nil if @collection.blank?
|
11
|
+
|
12
|
+
if @options.key?(:spacer_template)
|
13
|
+
spacer = find_template(@options[:spacer_template]).render(@view, @locals)
|
14
|
+
end
|
15
|
+
|
16
|
+
result = @template ? collection_with_template : collection_without_template
|
17
|
+
# Skip forcing the result into a sting. Everything above this is just like the original method
|
18
|
+
#result.join(spacer).html_safe
|
19
|
+
end
|
20
|
+
|
21
|
+
# Tell the template finder we're not a partial, so it doesnt try to find the underscored method
|
22
|
+
def find_template(path=@path)
|
23
|
+
return path unless path.is_a?(String)
|
24
|
+
prefix = @view.controller_path unless path.include?(?/)
|
25
|
+
|
26
|
+
##########
|
27
|
+
@view.find_template(path, prefix, false)
|
28
|
+
##########
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module JrbHandler::Renderers
|
2
|
+
|
3
|
+
EPOCH = Time.utc(2000, 1, 1).iso8601
|
4
|
+
|
5
|
+
::ActionController::Renderers.add(:jrb_collection) do |resources, options|
|
6
|
+
output = {
|
7
|
+
:href => request.url,
|
8
|
+
:count => resources.size,
|
9
|
+
:items => render(:ssaj1 => resources)
|
10
|
+
}
|
11
|
+
|
12
|
+
output = ActiveSupport::JSON.encode(output, :pretty => !Rails.env.production?)
|
13
|
+
self.response_body = output
|
14
|
+
end
|
15
|
+
|
16
|
+
::ActionController::Renderers.add(:jrb) do |resource, options|
|
17
|
+
options[:partial] ||= resource
|
18
|
+
#options.delete(:layout)
|
19
|
+
|
20
|
+
output = JrbPartialRenderer.new(self.view_context, options, nil).render
|
21
|
+
|
22
|
+
# If we got a set of resources, we're being rendered inside a collection,
|
23
|
+
# so just return the results and let the collection renderer handle the rest
|
24
|
+
return [] if output.nil?
|
25
|
+
return output if output.is_a?(Array)
|
26
|
+
|
27
|
+
output = ActiveSupport::JSON.encode(output, :pretty => !Rails.env.production?)
|
28
|
+
self.response_body = output
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module JrbHandler
|
2
|
+
class TemplateHandler < ActionView::Template::Handler
|
3
|
+
include ActionView::Template::Handlers::Compilable
|
4
|
+
|
5
|
+
ActionView::Template.register_template_handler(:jrb, self)
|
6
|
+
|
7
|
+
self.default_format = Mime::JSON
|
8
|
+
|
9
|
+
def compile(template)
|
10
|
+
template.source
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
metadata
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jrb_handler
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Paul Sadauskas
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-12-13 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 0
|
31
|
+
- 0
|
32
|
+
version: 2.0.0
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rspec-rails
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 2
|
45
|
+
- 0
|
46
|
+
- 0
|
47
|
+
version: 2.0.0
|
48
|
+
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: bundler
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
type: :development
|
62
|
+
version_requirements: *id003
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: activesupport
|
65
|
+
prerelease: false
|
66
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
segments:
|
72
|
+
- 3
|
73
|
+
- 0
|
74
|
+
- 3
|
75
|
+
version: 3.0.3
|
76
|
+
type: :runtime
|
77
|
+
version_requirements: *id004
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: actionpack
|
80
|
+
prerelease: false
|
81
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
segments:
|
87
|
+
- 3
|
88
|
+
- 0
|
89
|
+
- 3
|
90
|
+
version: 3.0.3
|
91
|
+
type: :runtime
|
92
|
+
version_requirements: *id005
|
93
|
+
description: " Jrb Handler makes it easy to write JSON web services in Rails 3. Keep your views in views, not in your Model#to_json.\n"
|
94
|
+
email:
|
95
|
+
- psadauskas@gmail.com
|
96
|
+
executables: []
|
97
|
+
|
98
|
+
extensions: []
|
99
|
+
|
100
|
+
extra_rdoc_files: []
|
101
|
+
|
102
|
+
files:
|
103
|
+
- .gitignore
|
104
|
+
- Gemfile
|
105
|
+
- LICENSE
|
106
|
+
- README.markdown
|
107
|
+
- jrb_handler.gemspec
|
108
|
+
- lib/active_support_extensions.rb
|
109
|
+
- lib/jrb_handler.rb
|
110
|
+
- lib/jrb_handler/jrb_partial_renderer.rb
|
111
|
+
- lib/jrb_handler/renderers.rb
|
112
|
+
- lib/jrb_handler/template_handler.rb
|
113
|
+
- lib/jrb_handler/version.rb
|
114
|
+
has_rdoc: true
|
115
|
+
homepage: http://github.com/paul/jrb_handler
|
116
|
+
licenses: []
|
117
|
+
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
none: false
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
segments:
|
129
|
+
- 0
|
130
|
+
version: "0"
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
none: false
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
segments:
|
137
|
+
- 1
|
138
|
+
- 3
|
139
|
+
- 6
|
140
|
+
version: 1.3.6
|
141
|
+
requirements: []
|
142
|
+
|
143
|
+
rubyforge_project:
|
144
|
+
rubygems_version: 1.3.7
|
145
|
+
signing_key:
|
146
|
+
specification_version: 3
|
147
|
+
summary: A Rails template handler and renderer for JSON
|
148
|
+
test_files: []
|
149
|
+
|