granola-schema 0.0.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.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +22 -0
  3. data/README.md +87 -0
  4. data/lib/granola/schema.rb +66 -0
  5. metadata +89 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 201eb2e3105a257b28ded44d77e9cae937e36893
4
+ data.tar.gz: 2444b5b3082c5c5161c72d01d365f27f84885cd0
5
+ SHA512:
6
+ metadata.gz: 2194f846f4c7cdaf6ceff6f2eefe2bc1c2d66c110f9b6b5cc2d83bb07421b825b1e282df1596ca937a6500a07159b3ad43864da75ac8dbfca1bbc17831775bed
7
+ data.tar.gz: a974513f758315a4ed10df2979b0decb3197b4807fc5de03804f5b89711e51fd488d2da71d4e787dedaa8071e812965c7fd8aac24782c67b6dd87c9fe04c3f94
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Nicolas Sanguinetti <hi@nicolassanguinetti.info>
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # Granola, a JSON serializer
2
+
3
+ Granola aims to provide a simple interface to generate JSON responses based on
4
+ your application's domain models. It doesn't make assumptions about anything and
5
+ gets out of your way. You just write plain ruby.
6
+
7
+ ## Example
8
+
9
+ ``` ruby
10
+ class PersonSerializer < Granola::Serializer
11
+ def attributes
12
+ {
13
+ "name" => object.name,
14
+ "email" => object.email,
15
+ "age" => object.age
16
+ }
17
+ end
18
+ end
19
+
20
+ PersonSerializer.new(person).to_json #=> '{"name":"John Doe",...}'
21
+ ```
22
+
23
+ ## Install
24
+
25
+ gem install granola
26
+
27
+ ## JSON serialization
28
+
29
+ Granola doesn't make assumptions about your code, so it shouldn't depend on a
30
+ specific JSON backend. It uses [MultiJson][] to serialize your objects with your
31
+ favorite backend.
32
+
33
+ Try to avoid using the default, which is the `stdlib`'s pure-ruby JSON library,
34
+ since it's slow. If in doubt, I like [Yajl][].
35
+
36
+ If you want to pass options to `MultiJson` (like `pretty: true`), any keywords
37
+ passed to `#to_json` will be forwarded to `MultiJson.dump`.
38
+
39
+ [MultiJson]: https://github.com/intridea/multi_json
40
+ [Yajl]: https://github.com/brianmario/yajl-ruby
41
+
42
+ ## Handling lists of models
43
+
44
+ A Granola serializer can handle a list of entities of the same type by using the
45
+ `Serializer.list` method (instead of `Serializer.new`). For example:
46
+
47
+ ``` ruby
48
+ serializer = PersonSerializer.list(Person.all)
49
+ serializer.to_json #=> '[{"name":"John Doe",...},{...}]'
50
+ ```
51
+
52
+ ## Rack Helpers
53
+
54
+ If your application is based on rack, you have a `Rack::Response` called `res`
55
+ in your context, and you have an `env` method that returns the Rack env hash,
56
+ you can simply `include Granola::Rack` and you get access to the following
57
+ interface:
58
+
59
+ ``` ruby
60
+ json(person) #=> This will try to infer PersonSerializer from a Person instance
61
+ json(person, with: AnotherSerializer)
62
+ ```
63
+
64
+ *NOTE*: This works out of the box in frameworks like [Cuba][] or [Roda][]. You
65
+ might need to provide glue code to make `res` and/or `env` available in other
66
+ frameworks.
67
+
68
+ [Cuba]: https://github.com/soveran/cuba
69
+ [Roda]: https://github.com/jeremyevans/roda
70
+
71
+ ## Caching
72
+
73
+ `Granola::Serializer` gives you to methods that you can implement in your
74
+ serializers, `last_modified` and `cache_key`, that will be used to prevent
75
+ rendering JSON at all if possible, when using the `Granola::Rack#json` helper.
76
+
77
+ If your serializer implements this method, and the `env` has the appropriate
78
+ `If-Modified-Since` or `If-None-Match` headers, the helper will automatically
79
+ return a 304 response.
80
+
81
+ Plus, it sets appropriate `ETag` and `Last-Modified` so your clients can avoid
82
+ hitting the endpoint altogether.
83
+
84
+ ## License
85
+
86
+ This project is shared under the MIT license. See the attached LICENSE file for
87
+ details.
@@ -0,0 +1,66 @@
1
+ require "json-schema"
2
+ require "granola"
3
+
4
+ module Granola
5
+ class Serializer
6
+ # This module adds support for JSON Schema to Granola serializers. You
7
+ # should define the schema for each serializer
8
+ module Schema
9
+ def self.included(base)
10
+ base.extend ClassMethods
11
+ end
12
+
13
+ module ClassMethods
14
+ # Public: The schema definition for entities emitted by this serializer.
15
+ #
16
+ # Returns a Hash that complies to JSON schema.
17
+ def schema
18
+ fail NotImplementedError
19
+ end
20
+ end
21
+
22
+ # Public: Validate this serializer against it's own schema.
23
+ #
24
+ # Returns Boolean indicating whether it's valid. In case it's not, it
25
+ # populates the `#validation_errors` Array.
26
+ def valid?
27
+ validation_errors.clear
28
+ validation_errors.concat(
29
+ JSON::Validator.fully_validate(self.class.schema, attributes)
30
+ )
31
+ validation_errors.empty?
32
+ end
33
+
34
+ # Public: List any errors that arose from validating this against its
35
+ # schema.
36
+ #
37
+ # See #valid?
38
+ #
39
+ # Returns an Array.
40
+ def validation_errors
41
+ @validation_errors ||= []
42
+ end
43
+ end
44
+
45
+ include Schema
46
+ end
47
+
48
+ # Public: Schema serializer to render your JSON schemas.
49
+ #
50
+ # Example:
51
+ #
52
+ # serializer = SchemaSerializer.new(PersonSerializer.schema)
53
+ # serializer.to_json
54
+ class SchemaSerializer < Serializer
55
+ def attributes
56
+ {
57
+ "$schema".freeze => "http://json-schema.org/schema#".freeze,
58
+ "type".freeze => "object".freeze
59
+ }.merge(object)
60
+ end
61
+
62
+ def mime_type
63
+ "application/schema+json".freeze
64
+ end
65
+ end
66
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: granola-schema
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Sanguinetti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: granola
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: json-schema
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: cutest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.2'
55
+ description: Handles JSON Schema support for Granola serializers.
56
+ email:
57
+ - contacto@nicolassanguinetti.info
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE
63
+ - README.md
64
+ - lib/granola/schema.rb
65
+ homepage: http://github.com/foca/granola
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.2.2
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Granola::Schema adds JSON schema support to Granola
89
+ test_files: []