granola 0.10.1 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -19
- data/lib/granola.rb +1 -1
- data/lib/granola/rack.rb +1 -2
- data/lib/granola/serializer.rb +0 -15
- data/lib/granola/util.rb +5 -7
- data/lib/granola/version.rb +1 -1
- metadata +3 -4
- data/lib/granola/json.rb +0 -37
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 889ae25405985efae9deccf2120d07a6c9e18562
|
4
|
+
data.tar.gz: faa849493f9d6e280d370a3e28852e153ac594f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2f705f01edc749d319c9e5a0cdf6eb0689cea3575152e9ba1696138c48a979a4e86d8b5fc5a69079060166162af3bc6efa79219e0ca61f9ff73115bae9aee22
|
7
|
+
data.tar.gz: 3c711e64c563d5c68e18a7e4490c96ad2889ca2d3c9b2147c5c375bdafa04c72082454e5045551c73513398cb5bf6db7d5975eb482c808f0c2703d14bfc6242b
|
data/README.md
CHANGED
@@ -30,21 +30,16 @@ PersonSerializer.new(person).to_json #=> '{"name":"John Doe",...}'
|
|
30
30
|
|
31
31
|
Granola doesn't make assumptions about your code, so it shouldn't depend on a
|
32
32
|
specific JSON backend. It defaults to the native JSON backend, but you're free
|
33
|
-
to change it. For example, if you were using [
|
33
|
+
to change it. For example, if you were using [Oj][]:
|
34
34
|
|
35
35
|
``` ruby
|
36
|
-
Granola.json
|
36
|
+
Granola.render :json, via: Oj.method(:dump),
|
37
|
+
content_type: "application/json"
|
37
38
|
```
|
38
39
|
|
39
|
-
|
40
|
-
using, so you shouldn't worry. Be warned that using MultiJson instead of
|
41
|
-
using a library (such as Yajl) straight away incurs a small performance penalty
|
42
|
-
(see, and run, [the benchmark](./benchmarks/multi_json.rb)).
|
40
|
+
[Oj]: https://github.com/ohler55/oj
|
43
41
|
|
44
|
-
|
45
|
-
[MultiJson]: https://github.com/intridea/multi_json
|
46
|
-
|
47
|
-
## Handling lists of models
|
42
|
+
## Handling lists of entities
|
48
43
|
|
49
44
|
A Granola serializer can handle a list of entities of the same type by using the
|
50
45
|
`Serializer.list` method (instead of `Serializer.new`). For example:
|
@@ -134,17 +129,12 @@ support (via the [msgpack-ruby][] library), you'd do this:
|
|
134
129
|
``` ruby
|
135
130
|
require "msgpack"
|
136
131
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
def to_msgpack(*)
|
141
|
-
MsgPack.pack(data)
|
142
|
-
end
|
143
|
-
end
|
132
|
+
Granola.render :msgpack, via: MessagePack.method(:pack),
|
133
|
+
content_type: "application/x-msgpack"
|
144
134
|
```
|
145
135
|
|
146
|
-
Now all serializers
|
147
|
-
|
136
|
+
Now all serializers can be serialized into MsgPack using a `to_msgpack` method.
|
137
|
+
In order to use this from our Rack helpers, you'd do:
|
148
138
|
|
149
139
|
``` ruby
|
150
140
|
granola(object, as: :msgpack)
|
data/lib/granola.rb
CHANGED
data/lib/granola/rack.rb
CHANGED
@@ -3,7 +3,6 @@ require "time"
|
|
3
3
|
require "granola/serializer"
|
4
4
|
require "granola/util"
|
5
5
|
require "granola/caching"
|
6
|
-
require "granola/json"
|
7
6
|
|
8
7
|
module Granola
|
9
8
|
# Mixin to render JSON in the context of a Rack application. See the #json
|
@@ -51,7 +50,7 @@ module Granola
|
|
51
50
|
headers["Content-Type".freeze] = serializer.mime_type(as)
|
52
51
|
|
53
52
|
body = Enumerator.new do |yielder|
|
54
|
-
yielder << serializer.
|
53
|
+
yielder << serializer.render(as, opts)
|
55
54
|
end
|
56
55
|
|
57
56
|
[status, headers, body]
|
data/lib/granola/serializer.rb
CHANGED
@@ -4,12 +4,6 @@ module Granola
|
|
4
4
|
class Serializer
|
5
5
|
attr_reader :object
|
6
6
|
|
7
|
-
# Public: Map of the default MIME type for each given type of serialization
|
8
|
-
# for this object. If your API returns a different MIME type for your
|
9
|
-
# responses (for example, `application/vnd.company+json`) you should replace
|
10
|
-
# it here.
|
11
|
-
MIME_TYPES = {}
|
12
|
-
|
13
7
|
# Public: Instantiates a list serializer that wraps around an iterable of
|
14
8
|
# objects of the type expected by this serializer class.
|
15
9
|
#
|
@@ -39,15 +33,6 @@ module Granola
|
|
39
33
|
def data
|
40
34
|
fail NotImplementedError
|
41
35
|
end
|
42
|
-
|
43
|
-
# Internal: Returns the MIME type generated by this serializer.
|
44
|
-
#
|
45
|
-
# type - A Symbol describing the expected mime type.
|
46
|
-
#
|
47
|
-
# Returns a String.
|
48
|
-
def mime_type(type = :json)
|
49
|
-
MIME_TYPES.fetch(type)
|
50
|
-
end
|
51
36
|
end
|
52
37
|
|
53
38
|
# Internal: The List serializer provides an interface for serializing lists of
|
data/lib/granola/util.rb
CHANGED
@@ -33,20 +33,18 @@ module Granola
|
|
33
33
|
# Raises NameError if no matching class exists.
|
34
34
|
# Returns a Class.
|
35
35
|
def self.serializer_class_for(object)
|
36
|
-
object = object.respond_to?(:to_ary) ? object.to_ary
|
37
|
-
|
38
|
-
Object.const_get("%sSerializer" % name)
|
39
|
-
rescue NameError
|
36
|
+
object = object.respond_to?(:to_ary) ? object.to_ary[0] : object
|
37
|
+
|
40
38
|
case object
|
41
39
|
when NilClass, TrueClass, FalseClass, Numeric, String
|
42
40
|
PrimitiveTypesSerializer
|
43
41
|
else
|
44
|
-
|
42
|
+
name = object.class.name
|
43
|
+
Object.const_get("%sSerializer" % name)
|
45
44
|
end
|
46
45
|
end
|
47
46
|
|
48
|
-
# Internal:
|
49
|
-
# types.
|
47
|
+
# Internal: Serializer that transparently handles rendering primitive types.
|
50
48
|
class PrimitiveTypesSerializer < Granola::Serializer
|
51
49
|
def data
|
52
50
|
object
|
data/lib/granola/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: granola
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.11.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicolas Sanguinetti
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-11-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: cutest
|
@@ -49,7 +49,6 @@ files:
|
|
49
49
|
- README.md
|
50
50
|
- lib/granola.rb
|
51
51
|
- lib/granola/caching.rb
|
52
|
-
- lib/granola/json.rb
|
53
52
|
- lib/granola/rack.rb
|
54
53
|
- lib/granola/serializer.rb
|
55
54
|
- lib/granola/util.rb
|
@@ -74,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
73
|
version: '0'
|
75
74
|
requirements: []
|
76
75
|
rubyforge_project:
|
77
|
-
rubygems_version: 2.
|
76
|
+
rubygems_version: 2.5.1
|
78
77
|
signing_key:
|
79
78
|
specification_version: 4
|
80
79
|
summary: 'Granola: JSON Serializers for your app.'
|
data/lib/granola/json.rb
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
require "granola/serializer"
|
2
|
-
require "json"
|
3
|
-
|
4
|
-
module Granola
|
5
|
-
class << self
|
6
|
-
# Public: Get/Set a Proc that takes an Object and a Hash of options and
|
7
|
-
# returns a JSON String.
|
8
|
-
#
|
9
|
-
# The default implementation uses the standard library's JSON module, but
|
10
|
-
# you're welcome to swap it out.
|
11
|
-
#
|
12
|
-
# Example:
|
13
|
-
#
|
14
|
-
# require "yajl"
|
15
|
-
# Granola.json = ->(obj, **opts) { Yajl::Encoder.encode(obj, opts) }
|
16
|
-
attr_accessor :json
|
17
|
-
end
|
18
|
-
|
19
|
-
if defined?(MultiJson)
|
20
|
-
self.json = MultiJson.method(:dump)
|
21
|
-
else
|
22
|
-
self.json = JSON.method(:generate)
|
23
|
-
end
|
24
|
-
|
25
|
-
class Serializer
|
26
|
-
MIME_TYPES[:json] = "application/json".freeze
|
27
|
-
|
28
|
-
# Public: Generate the JSON String.
|
29
|
-
#
|
30
|
-
# **options - Any options to be passed to the `Granola.json` Proc.
|
31
|
-
#
|
32
|
-
# Returns a String.
|
33
|
-
def to_json(**options)
|
34
|
-
Granola.json.(data, options)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|