granola 0.10.1 → 0.11.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 83004a0de438c36e72d1b6e9a42a4247532d54ac
4
- data.tar.gz: f393e6a7bc0b330978a89dffdf6bb39aca568716
3
+ metadata.gz: 889ae25405985efae9deccf2120d07a6c9e18562
4
+ data.tar.gz: faa849493f9d6e280d370a3e28852e153ac594f3
5
5
  SHA512:
6
- metadata.gz: 8b8767a91cfafb4fcbe99475988ae6a4de500eb8c71f08acde98833ebbd1ad7a1805d5b79aa8b43770b45cdd7b03b9517e8452d9c004a29c40b473db9dc4e041
7
- data.tar.gz: f97a298c5cbfa89d5ff3d7358fa803347c226f247113174ef28439f39bda9d2cae5505e092cde5d5e03a08e4edd6fa730f7b06ada2adac8d19a315a8efcc28e1
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 [Yajl][]:
33
+ to change it. For example, if you were using [Oj][]:
34
34
 
35
35
  ``` ruby
36
- Granola.json = Yajl::Encoder.method(:encode)
36
+ Granola.render :json, via: Oj.method(:dump),
37
+ content_type: "application/json"
37
38
  ```
38
39
 
39
- If your project already uses [MultiJson][] then we will default to whatever it's
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
- [Yajl]: https://github.com/brianmario/yajl-ruby
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
- class BaseSerializer < Granola::Serializer
138
- MIME_TYPES[:msgpack] = "application/x-msgpack".freeze
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 that inherit from `BaseSerializer` can be data into
147
- MsgPack. In order to use this from our Rack helpers, you'd do:
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
@@ -1,4 +1,4 @@
1
1
  require "granola/version"
2
2
  require "granola/serializer"
3
- require "granola/json"
3
+ require "granola/rendering"
4
4
  require "granola/rack"
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.public_send(:"to_#{as}", opts)
53
+ yielder << serializer.render(as, opts)
55
54
  end
56
55
 
57
56
  [status, headers, body]
@@ -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.fetch(0, nil) : object
37
- name = object.class.name
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
- raise
42
+ name = object.class.name
43
+ Object.const_get("%sSerializer" % name)
45
44
  end
46
45
  end
47
46
 
48
- # Internal: Null serializer that transparently handles rendering primitive
49
- # types.
47
+ # Internal: Serializer that transparently handles rendering primitive types.
50
48
  class PrimitiveTypesSerializer < Granola::Serializer
51
49
  def data
52
50
  object
@@ -1,3 +1,3 @@
1
1
  module Granola
2
- VERSION = "0.10.1"
2
+ VERSION = "0.11.0"
3
3
  end
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.10.1
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: 2015-04-01 00:00:00.000000000 Z
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.2.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