transit-rails 0.8.0.beta → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +2 -0
- data/README.md +30 -3
- data/lib/transit/rails/version.rb +1 -1
- data/spec/transit/rails/renderer_spec.rb +74 -0
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 54fb8c5fd98a4f514d0137ec258bd8bd42b31fe6
|
4
|
+
data.tar.gz: 14beb974c6c91c146774e0d8930fac9fb2882393
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29d568f11beba71eda87c61d1dbc12f3fe44ca108528d255df2ee03bfb21aeaa2d2099b89ec72054fb09adeddd322943f8858c798931663540a3deabdf0f6f07
|
7
|
+
data.tar.gz: f119b7dfee76bb8fe18542076dcfcef2c951e965a38eebb6572be13e96a3c19f73772d8e79825797b56e43a7c469344783c7f6510bcd978af84cf51c2fe04f5d
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
# transit-rails
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+

|
4
|
+
|
5
|
+
Use [transit format][] in your Rails application, with minimal friction.
|
5
6
|
|
6
7
|
Currently only works with C-based Ruby implementations.
|
7
8
|
|
@@ -21,7 +22,7 @@ Or install it yourself as:
|
|
21
22
|
|
22
23
|
## Usage
|
23
24
|
|
24
|
-
In
|
25
|
+
In a Rails controller:
|
25
26
|
|
26
27
|
```ruby
|
27
28
|
def index
|
@@ -33,6 +34,28 @@ def index
|
|
33
34
|
end
|
34
35
|
```
|
35
36
|
|
37
|
+
The renderer can take a `verbose` flag, which when true will output
|
38
|
+
transit in the `json-verbose` format.
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
def index
|
42
|
+
@posts = Post.all
|
43
|
+
render transit: @posts, verbose: true
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
You can supply any custom handlers using the `handlers` option.
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
def index
|
51
|
+
@person = Person.new
|
52
|
+
render transit: @person, handlers: { Person => PersonHandler.new }
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
Read more about [custom handlers][] on the
|
57
|
+
[transit-ruby][] README.
|
58
|
+
|
36
59
|
## Contributing
|
37
60
|
|
38
61
|
1. Fork it ( https://github.com/jgdavey/transit-rails/fork )
|
@@ -40,3 +63,7 @@ end
|
|
40
63
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
41
64
|
4. Push to the branch (`git push origin my-new-feature`)
|
42
65
|
5. Create a new Pull Request
|
66
|
+
|
67
|
+
[transit format]: https://github.com/cognitect/transit-format
|
68
|
+
[custom handlers]: https://github.com/cognitect/transit-ruby#custom-handlers
|
69
|
+
[transit-ruby]: https://github.com/cognitect/transit-ruby
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'transit/rails/renderer'
|
3
|
+
require 'active_support/core_ext/time'
|
4
|
+
|
5
|
+
describe Transit::Rails::Renderer do
|
6
|
+
def render(obj, opts={}, io=StringIO.new)
|
7
|
+
Transit::Rails::Renderer.new(obj, opts, io).render
|
8
|
+
end
|
9
|
+
|
10
|
+
def decode(io, handlers={})
|
11
|
+
io = StringIO.new(io) unless io.respond_to?(:rewind)
|
12
|
+
io.rewind
|
13
|
+
Transit::Reader.new(:json, io, handlers: handlers).read
|
14
|
+
end
|
15
|
+
|
16
|
+
context "with custom handlers" do
|
17
|
+
Point = Struct.new(:x, :y)
|
18
|
+
point_handler = Class.new do
|
19
|
+
def tag(o)
|
20
|
+
"point"
|
21
|
+
end
|
22
|
+
|
23
|
+
def rep(o)
|
24
|
+
[o.x, o.y]
|
25
|
+
end
|
26
|
+
|
27
|
+
def string_rep(o)
|
28
|
+
rep(o).to_s
|
29
|
+
end
|
30
|
+
|
31
|
+
def from_rep(o)
|
32
|
+
Point.new(o[0], o[1])
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it "correctly renders custom handlers at top level" do
|
37
|
+
handlers = { Point => point_handler.new }
|
38
|
+
|
39
|
+
io = StringIO.new
|
40
|
+
point = Point.new(1,2)
|
41
|
+
rendered = render(point, {handlers: handlers}, io)
|
42
|
+
expect(point).to eq decode(rendered, {"point" => point_handler.new})
|
43
|
+
end
|
44
|
+
|
45
|
+
it "correctly uses custom handlers in an Array" do
|
46
|
+
handlers = { Point => point_handler.new }
|
47
|
+
|
48
|
+
io = StringIO.new
|
49
|
+
points = [Point.new(1,2), Point.new(3,4)]
|
50
|
+
rendered = render(points, {handlers: handlers}, io)
|
51
|
+
expect(points).to eq decode(rendered, {"point" => point_handler.new})
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context "verbosity" do
|
56
|
+
it "can render verbose JSON" do
|
57
|
+
obj = [Time.now, Time.now - 1]
|
58
|
+
verbose = render(obj, verbose: true)
|
59
|
+
terse = render(obj)
|
60
|
+
expect(verbose.length).to be > terse.length
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context "time with zone" do
|
65
|
+
it "encodes ActiveSupport::TimeWithZone as a UTC time" do
|
66
|
+
Time.zone = "EST"
|
67
|
+
obj = Time.zone.now
|
68
|
+
rendered = render(obj)
|
69
|
+
# There's some truncation/rounding happening here, so strict equality
|
70
|
+
# won't suffice
|
71
|
+
expect(obj).to be_within(1).of(decode(rendered))
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: transit-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Davey
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: transit-ruby
|
@@ -81,6 +81,7 @@ files:
|
|
81
81
|
- lib/transit/rails/renderer.rb
|
82
82
|
- lib/transit/rails/version.rb
|
83
83
|
- spec/spec_helper.rb
|
84
|
+
- spec/transit/rails/renderer_spec.rb
|
84
85
|
- spec/transit/rails_spec.rb
|
85
86
|
- transit-rails.gemspec
|
86
87
|
homepage: https://github.com/jgdavey/transit-rails
|
@@ -98,9 +99,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
98
99
|
version: '0'
|
99
100
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
101
|
requirements:
|
101
|
-
- - "
|
102
|
+
- - ">="
|
102
103
|
- !ruby/object:Gem::Version
|
103
|
-
version:
|
104
|
+
version: '0'
|
104
105
|
requirements: []
|
105
106
|
rubyforge_project:
|
106
107
|
rubygems_version: 2.2.2
|
@@ -109,4 +110,5 @@ specification_version: 4
|
|
109
110
|
summary: Transit format helpers for Rails
|
110
111
|
test_files:
|
111
112
|
- spec/spec_helper.rb
|
113
|
+
- spec/transit/rails/renderer_spec.rb
|
112
114
|
- spec/transit/rails_spec.rb
|