rabl 0.7.10 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +3 -1
- data/README.md +9 -7
- data/fixtures/padrino_test/app/app.rb +8 -36
- data/fixtures/rails3_2/Gemfile +1 -1
- data/fixtures/sinatra_test/app.rb +8 -0
- data/lib/rabl.rb +0 -1
- data/lib/rabl/configuration.rb +9 -8
- data/lib/rabl/engine.rb +1 -1
- data/lib/rabl/version.rb +1 -1
- data/rabl.gemspec +0 -1
- data/test/configuration_test.rb +1 -9
- metadata +2 -19
- data/lib/rabl/json_engine.rb +0 -33
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -23,6 +23,10 @@ For a breakdown of common misconceptions about RABL, please check out our guide
|
|
23
23
|
|
24
24
|
## Breaking Changes ##
|
25
25
|
|
26
|
+
* v0.8.0 (released Feb 14, 2012) removes multi_json dependency and
|
27
|
+
relies on Oj (or JSON) as the json parser. Simplifies code, removes a dependency
|
28
|
+
but you might want to remove any references to MultiJson.
|
29
|
+
|
26
30
|
* v0.6.14 (released June 28, 2012) requires the use of render_views
|
27
31
|
with RSpec to test templates. Otherwise, the controller will simply
|
28
32
|
pass through the render command as it does with ERB templates.
|
@@ -117,7 +121,7 @@ Rabl.configure do |config|
|
|
117
121
|
# config.cache_engine = Rabl::CacheEngine.new # Defaults to Rails cache
|
118
122
|
# config.perform_caching = false
|
119
123
|
# config.escape_all_output = false
|
120
|
-
# config.json_engine = nil #
|
124
|
+
# config.json_engine = nil # Class with #dump class method (defaults JSON)
|
121
125
|
# config.msgpack_engine = nil # Defaults to ::MessagePack
|
122
126
|
# config.bson_engine = nil # Defaults to ::BSON
|
123
127
|
# config.plist_engine = nil # Defaults to ::Plist::Emit
|
@@ -165,8 +169,7 @@ attempts to render an attribute that does not exist. Otherwise, the attribute wi
|
|
165
169
|
Setting this to true during development may help increase the robustness of your code, but using `true` in
|
166
170
|
production code is not recommended.
|
167
171
|
|
168
|
-
|
169
|
-
defaults so that in most cases you **don't need to configure this** directly. For example, if you wish to use [oj](https://github.com/ohler55/oj) as
|
172
|
+
If you wish to use [oj](https://github.com/ohler55/oj) as
|
170
173
|
the primary JSON encoding engine simply add that to your Gemfile:
|
171
174
|
|
172
175
|
```ruby
|
@@ -174,10 +177,9 @@ the primary JSON encoding engine simply add that to your Gemfile:
|
|
174
177
|
gem 'oj'
|
175
178
|
```
|
176
179
|
|
177
|
-
and RABL will
|
178
|
-
|
179
|
-
|
180
|
-
supports `encode` method and set `json_engine` option to the engine's Class name:
|
180
|
+
and RABL will use that engine automatically for encoding your JSON responses.
|
181
|
+
Set your own custom json_engine which define a `dump` or `encode`
|
182
|
+
method for converting to JSON from ruby data:
|
181
183
|
|
182
184
|
```ruby
|
183
185
|
config.json_engine = ActiveSupport::JSON
|
@@ -5,21 +5,6 @@ class PadrinoTest < Padrino::Application
|
|
5
5
|
|
6
6
|
enable :sessions
|
7
7
|
|
8
|
-
##
|
9
|
-
# Caching support
|
10
|
-
#
|
11
|
-
# register Padrino::Cache
|
12
|
-
# enable :caching
|
13
|
-
#
|
14
|
-
# You can customize caching store engines:
|
15
|
-
#
|
16
|
-
# set :cache, Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211', :exception_retry_limit => 1))
|
17
|
-
# set :cache, Padrino::Cache::Store::Memcache.new(::Dalli::Client.new('127.0.0.1:11211', :exception_retry_limit => 1))
|
18
|
-
# set :cache, Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0))
|
19
|
-
# set :cache, Padrino::Cache::Store::Memory.new(50)
|
20
|
-
# set :cache, Padrino::Cache::Store::File.new(Padrino.root('tmp', app_name.to_s, 'cache')) # default choice
|
21
|
-
#
|
22
|
-
|
23
8
|
##
|
24
9
|
# Application configuration options
|
25
10
|
#
|
@@ -35,25 +20,12 @@ class PadrinoTest < Padrino::Application
|
|
35
20
|
# disable :flash # Disables rack-flash (enabled by default if Rack::Flash is defined)
|
36
21
|
# layout :my_layout # Layout can be in views/layouts/foo.ext or views/foo.ext (default :application)
|
37
22
|
#
|
38
|
-
|
39
|
-
##
|
40
|
-
# You can configure for a specified environment like:
|
41
|
-
#
|
42
|
-
# configure :development do
|
43
|
-
# set :foo, :bar
|
44
|
-
# disable :asset_stamp # no asset timestamping for dev
|
45
|
-
# end
|
46
|
-
#
|
47
|
-
|
48
|
-
##
|
49
|
-
# You can manage errors like:
|
50
|
-
#
|
51
|
-
# error 404 do
|
52
|
-
# render 'errors/404'
|
53
|
-
# end
|
54
|
-
#
|
55
|
-
# error 505 do
|
56
|
-
# render 'errors/505'
|
57
|
-
# end
|
58
|
-
#
|
59
23
|
end
|
24
|
+
|
25
|
+
# Patch times to return as iso8601
|
26
|
+
class Time
|
27
|
+
alias_method :old_to_s, :to_s
|
28
|
+
def to_s(format=nil)
|
29
|
+
format ? old_to_s(format) : iso8601
|
30
|
+
end
|
31
|
+
end
|
data/fixtures/rails3_2/Gemfile
CHANGED
@@ -41,4 +41,12 @@ class SinatraTest < Sinatra::Application
|
|
41
41
|
@user = User.find(params[:id])
|
42
42
|
render :rabl, :"users/show.json", :format => "json"
|
43
43
|
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Patch times to return as iso8601
|
47
|
+
class Time
|
48
|
+
alias_method :old_to_s, :to_s
|
49
|
+
def to_s(format=nil)
|
50
|
+
format ? old_to_s(format) : iso8601
|
51
|
+
end
|
44
52
|
end
|
data/lib/rabl.rb
CHANGED
data/lib/rabl/configuration.rb
CHANGED
@@ -16,6 +16,13 @@ begin
|
|
16
16
|
rescue LoadError
|
17
17
|
end
|
18
18
|
|
19
|
+
# Set default options for Oj json parser (if exists)
|
20
|
+
begin
|
21
|
+
require 'oj'
|
22
|
+
Oj.default_options = { :mode => :compat, :time_format => :ruby }
|
23
|
+
rescue LoadError
|
24
|
+
end
|
25
|
+
|
19
26
|
module Rabl
|
20
27
|
# Rabl.host
|
21
28
|
class Configuration
|
@@ -28,6 +35,7 @@ module Rabl
|
|
28
35
|
attr_accessor :enable_json_callbacks
|
29
36
|
attr_accessor :bson_check_keys
|
30
37
|
attr_accessor :bson_move_id
|
38
|
+
attr_writer :json_engine
|
31
39
|
attr_writer :msgpack_engine
|
32
40
|
attr_writer :bson_engine
|
33
41
|
attr_writer :plist_engine
|
@@ -65,16 +73,9 @@ module Rabl
|
|
65
73
|
@perform_caching = false
|
66
74
|
end
|
67
75
|
|
68
|
-
# @param [Symbol, Class] engine_name The name of a JSON engine,
|
69
|
-
# or class that responds to `encode`, to use to encode Rabl templates
|
70
|
-
# into JSON. For more details, see the MultiJson gem.
|
71
|
-
def json_engine=(engine_name_or_class)
|
72
|
-
JsonEngine.instance.set(engine_name_or_class)
|
73
|
-
end
|
74
|
-
|
75
76
|
# @return The JSON engine used to encode Rabl templates into JSON
|
76
77
|
def json_engine
|
77
|
-
|
78
|
+
@json_engine || (defined?(::Oj) ? ::Oj : ::JSON)
|
78
79
|
end
|
79
80
|
|
80
81
|
##
|
data/lib/rabl/engine.rb
CHANGED
@@ -232,7 +232,7 @@ module Rabl
|
|
232
232
|
# format_json("{ foo : "bar" }") => "test({ foo : 'bar' })"
|
233
233
|
def format_json(json_output)
|
234
234
|
json_engine = Rabl.configuration.json_engine
|
235
|
-
json_method = json_engine.respond_to?(:dump) ? 'dump' : 'encode'
|
235
|
+
json_method = json_engine.respond_to?(:dump) ? 'dump' : 'encode'
|
236
236
|
json_output = json_engine.send(json_method, json_output) unless json_output.is_a?(String)
|
237
237
|
use_callback = Rabl.configuration.enable_json_callbacks && request_params[:callback].present?
|
238
238
|
use_callback ? "#{request_params[:callback]}(#{json_output})" : json_output
|
data/lib/rabl/version.rb
CHANGED
data/rabl.gemspec
CHANGED
data/test/configuration_test.rb
CHANGED
@@ -3,7 +3,7 @@ require File.expand_path('../teststrap', __FILE__)
|
|
3
3
|
context 'Rabl::Configuration' do
|
4
4
|
context 'defaults' do
|
5
5
|
# multi_json compatibility TODO
|
6
|
-
helper(:json_engine) {
|
6
|
+
helper(:json_engine) { ::Oj }
|
7
7
|
setup { Rabl.configuration }
|
8
8
|
|
9
9
|
asserts(:include_json_root).equals true
|
@@ -35,14 +35,6 @@ context 'Rabl::Configuration' do
|
|
35
35
|
asserts('uses a custom JSON engine') { topic.json_engine.to_s == 'ActiveSupport::JSON' }
|
36
36
|
end # custom JSON, class
|
37
37
|
|
38
|
-
context 'invalid JSON engine configured' do
|
39
|
-
asserts {
|
40
|
-
Rabl.configure do |c|
|
41
|
-
c.json_engine = Kernel
|
42
|
-
end
|
43
|
-
}.raises(RuntimeError)
|
44
|
-
end # invalid
|
45
|
-
|
46
38
|
context 'raise on missing attributes' do
|
47
39
|
setup do
|
48
40
|
Rabl.configure do |c|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rabl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -27,22 +27,6 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: 2.3.14
|
30
|
-
- !ruby/object:Gem::Dependency
|
31
|
-
name: multi_json
|
32
|
-
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
|
-
requirements:
|
35
|
-
- - ~>
|
36
|
-
- !ruby/object:Gem::Version
|
37
|
-
version: '1.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: '1.0'
|
46
30
|
- !ruby/object:Gem::Dependency
|
47
31
|
name: riot
|
48
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -399,7 +383,6 @@ files:
|
|
399
383
|
- lib/rabl/configuration.rb
|
400
384
|
- lib/rabl/engine.rb
|
401
385
|
- lib/rabl/helpers.rb
|
402
|
-
- lib/rabl/json_engine.rb
|
403
386
|
- lib/rabl/partials.rb
|
404
387
|
- lib/rabl/railtie.rb
|
405
388
|
- lib/rabl/renderer.rb
|
data/lib/rabl/json_engine.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
# Defines the default JSON engine for RABL when rendering JSON is invoked on a template.
|
2
|
-
# You can define your own json engine by creating an object that responds to the `encode` method
|
3
|
-
# and setting the corresponding configuration option:
|
4
|
-
#
|
5
|
-
# config.json_engine = ActiveSupport::JSON
|
6
|
-
#
|
7
|
-
|
8
|
-
require 'multi_json'
|
9
|
-
require 'singleton'
|
10
|
-
|
11
|
-
module Rabl
|
12
|
-
class JsonEngine
|
13
|
-
include Singleton
|
14
|
-
|
15
|
-
attr_reader :current_engine
|
16
|
-
|
17
|
-
def initialize
|
18
|
-
@current_engine = MultiJson.respond_to?(:adapter) ? MultiJson.adapter : MultiJson.engine
|
19
|
-
end
|
20
|
-
|
21
|
-
def set(engine_name_or_class)
|
22
|
-
@current_engine = begin
|
23
|
-
MultiJson.respond_to?(:use) ?
|
24
|
-
MultiJson.use(engine_name_or_class) :
|
25
|
-
MultiJson.engine = engine_name_or_class
|
26
|
-
rescue RuntimeError => e #
|
27
|
-
# Re-raise if engine_name_or_class is invalid
|
28
|
-
raise e unless engine_name_or_class.respond_to?(:encode)
|
29
|
-
engine_name_or_class
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end # JsonEngine
|
33
|
-
end # Rabl
|