granola-schema 0.9.0 → 0.10.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 +4 -4
- data/README.md +12 -13
- data/lib/granola/schema.rb +6 -7
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: daabbe93588ef1fbe61574bbf47abed87ec7892f
|
4
|
+
data.tar.gz: e6ad6bc20ea0641593815ce46d347920c16cea22
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0fee957f63aaf9441cda7113acda7b8ab59c6146c979ce69e6697c1500ee009bca583ec604266c9ad558f3241dade963ba1083326c024981d9a8adeea2f89729
|
7
|
+
data.tar.gz: da5b1169178f638a4b79d5de4d9d3898b8d95ab9e2dca7c44ac81342279e18c78681bc8c10e67a6e283556093b805d3ded6520a8d3e5bc83390ee13324b0603b
|
data/README.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
|
-
# Granola, a JSON serializer [](https://travis-ci.org/foca/granola) [](https://rubygems.org/gems/granola)
|
2
2
|
|
3
3
|

|
4
4
|
|
5
|
-
|
6
5
|
Granola aims to provide a simple interface to generate JSON responses based on
|
7
6
|
your application's domain models. It doesn't make assumptions about anything and
|
8
7
|
gets out of your way. You just write plain ruby.
|
@@ -11,7 +10,7 @@ gets out of your way. You just write plain ruby.
|
|
11
10
|
|
12
11
|
``` ruby
|
13
12
|
class PersonSerializer < Granola::Serializer
|
14
|
-
def
|
13
|
+
def data
|
15
14
|
{
|
16
15
|
"name" => object.name,
|
17
16
|
"email" => object.email,
|
@@ -38,7 +37,9 @@ Granola.json = Yajl::Encoder.method(:encode)
|
|
38
37
|
```
|
39
38
|
|
40
39
|
If your project already uses [MultiJson][] then we will default to whatever it's
|
41
|
-
using, so you shouldn't worry.
|
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)).
|
42
43
|
|
43
44
|
[Yajl]: https://github.com/brianmario/yajl-ruby
|
44
45
|
[MultiJson]: https://github.com/intridea/multi_json
|
@@ -63,10 +64,6 @@ granola(person) #=> This will infer PersonSerializer from a Person instance
|
|
63
64
|
granola(person, with: AnotherSerializer)
|
64
65
|
```
|
65
66
|
|
66
|
-
*NOTE* The method relies on being an `env` method that returns the Rack
|
67
|
-
environment Hash in the same context where you call the method. See [the
|
68
|
-
documentation](./lib/granola/rack.rb) for further details.
|
69
|
-
|
70
67
|
This method returns a Rack response tuple that you can use like so (this example
|
71
68
|
uses [Cuba][], but similar code will work for other frameworks):
|
72
69
|
|
@@ -99,7 +96,7 @@ generating the JSON response altogether. For example, using Cuba:
|
|
99
96
|
|
100
97
|
``` ruby
|
101
98
|
class UserSerializer < Granola::Serializer
|
102
|
-
def
|
99
|
+
def data
|
103
100
|
{ "id" => object.id, "name" => object.name, "email" => object.email }
|
104
101
|
end
|
105
102
|
|
@@ -141,12 +138,12 @@ class BaseSerializer < Granola::Serializer
|
|
141
138
|
MIME_TYPES[:msgpack] = "application/x-msgpack".freeze
|
142
139
|
|
143
140
|
def to_msgpack(*)
|
144
|
-
MsgPack.pack(
|
141
|
+
MsgPack.pack(data)
|
145
142
|
end
|
146
143
|
end
|
147
144
|
```
|
148
145
|
|
149
|
-
Now all serializers that inherit from `BaseSerializer` can be
|
146
|
+
Now all serializers that inherit from `BaseSerializer` can be data into
|
150
147
|
MsgPack. In order to use this from our Rack helpers, you'd do:
|
151
148
|
|
152
149
|
``` ruby
|
@@ -159,5 +156,7 @@ This will set the correct MIME type.
|
|
159
156
|
|
160
157
|
## License
|
161
158
|
|
162
|
-
This project is shared under the MIT license. See the attached LICENSE file
|
163
|
-
details.
|
159
|
+
This project is shared under the MIT license. See the attached [LICENSE][] file
|
160
|
+
for details.
|
161
|
+
|
162
|
+
[LICENSE]: ./LICENSE
|
data/lib/granola/schema.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "json-schema"
|
2
|
-
require "granola"
|
2
|
+
require "granola/serializer"
|
3
|
+
require "granola/json"
|
3
4
|
|
4
5
|
module Granola
|
5
6
|
class Serializer
|
@@ -26,7 +27,7 @@ module Granola
|
|
26
27
|
def valid?
|
27
28
|
validation_errors.clear
|
28
29
|
validation_errors.concat(
|
29
|
-
JSON::Validator.fully_validate(self.class.schema,
|
30
|
+
JSON::Validator.fully_validate(self.class.schema, data)
|
30
31
|
)
|
31
32
|
validation_errors.empty?
|
32
33
|
end
|
@@ -52,15 +53,13 @@ module Granola
|
|
52
53
|
# serializer = SchemaSerializer.new(PersonSerializer.schema)
|
53
54
|
# serializer.to_json
|
54
55
|
class SchemaSerializer < Serializer
|
55
|
-
|
56
|
+
MIME_TYPES[:json] = "application/schema+json".freeze
|
57
|
+
|
58
|
+
def data
|
56
59
|
{
|
57
60
|
"$schema".freeze => "http://json-schema.org/schema#".freeze,
|
58
61
|
"type".freeze => "object".freeze
|
59
62
|
}.merge(object)
|
60
63
|
end
|
61
|
-
|
62
|
-
def mime_type
|
63
|
-
"application/schema+json".freeze
|
64
|
-
end
|
65
64
|
end
|
66
65
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: granola-schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.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-03-
|
11
|
+
date: 2015-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: granola
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - '='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: 0.10.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - '='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.
|
26
|
+
version: 0.10.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: json-schema
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|