granola-rails 0.1.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 +7 -0
- data/LICENSE +20 -0
- data/README.md +90 -0
- data/lib/granola/rails/version.rb +7 -0
- data/lib/granola/rails.rb +77 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8563859c55e7e38a16263a1b06c620b36bbe099b
|
4
|
+
data.tar.gz: 2cfaa303a65ebc7d1ce909faeff653b2df73b3f2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 01a368327ebd252226fb9f9307caadf84048736cc4d6a2e2767b508ef0fa14bdcd0fb7e35b21d1ba6a5b678868f22ea61f1673e74d249d5036eb4fc64f578814
|
7
|
+
data.tar.gz: 0b98441bf576d117e30d9f22183c0ee6702bb0ee0f889c0fc9a2e5ef15368442be517679e809b99ac68dd5de08937454c2a797dff295ccdf5b86e400d468b284
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 Nicolas Sanguinetti
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# Granola::Rails [](https://travis-ci.org/foca/granola-rails) [](https://rubygems.org/gems/granola-rails)
|
2
|
+
|
3
|
+
Integrate [Granola](https://github.com/foca/granola) into Rails' rendering.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Once you install it, you can just use `render` to use Granola serializers:
|
8
|
+
|
9
|
+
``` ruby
|
10
|
+
class UsersController < ApplicationController
|
11
|
+
def show
|
12
|
+
user = User.find(params[:id])
|
13
|
+
render json: user
|
14
|
+
end
|
15
|
+
end
|
16
|
+
```
|
17
|
+
|
18
|
+
This would infer a `UserSerializer` (in `app/serializers/user_serializer.rb`).
|
19
|
+
You can pass any options to this method that you could pass to
|
20
|
+
[`Granola::Rack#granola`][granola-rack]. For example, to use a different
|
21
|
+
serializer:
|
22
|
+
|
23
|
+
``` ruby
|
24
|
+
class UsersController < ApplicationController
|
25
|
+
def show
|
26
|
+
user = User.find(params[:id])
|
27
|
+
render json: user, with: DetailedUserSerializer
|
28
|
+
end
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
[granola-rack]: https://github.com/foca/granola/blob/master/lib/granola/rack.rb
|
33
|
+
|
34
|
+
### Serialization formats
|
35
|
+
|
36
|
+
Any serialization format you add to Granola via `Granola.render` will be
|
37
|
+
available to render through rails. For example:
|
38
|
+
|
39
|
+
``` ruby
|
40
|
+
# config/initializers/granola.rb
|
41
|
+
Granola.render :yaml, via: YAML.method(:dump), content_type: Mime[:yaml].to_s
|
42
|
+
|
43
|
+
# app/controllers/users_controller.rb
|
44
|
+
class UsersController < ApplicationController
|
45
|
+
def show
|
46
|
+
user = User.find(params[:id])
|
47
|
+
|
48
|
+
respond_to do |format|
|
49
|
+
format.json { render json: user }
|
50
|
+
format.yaml { render yaml: user }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
## Rails Generators
|
57
|
+
|
58
|
+
This library provides a `serializer` generator:
|
59
|
+
|
60
|
+
``` bash
|
61
|
+
$ rails generate serializer user
|
62
|
+
create app/serializers/user_serializer.rb
|
63
|
+
invoke test_unit
|
64
|
+
create test/serializers/user_serializer_test.rb
|
65
|
+
```
|
66
|
+
|
67
|
+
## Installation
|
68
|
+
|
69
|
+
Add this line to your application's Gemfile:
|
70
|
+
|
71
|
+
``` ruby
|
72
|
+
gem 'granola-rails'
|
73
|
+
```
|
74
|
+
|
75
|
+
And then execute:
|
76
|
+
|
77
|
+
``` sh
|
78
|
+
$ bundle
|
79
|
+
```
|
80
|
+
|
81
|
+
Or install it yourself as:
|
82
|
+
|
83
|
+
``` sh
|
84
|
+
$ gem install granola-rails
|
85
|
+
```
|
86
|
+
|
87
|
+
## License
|
88
|
+
|
89
|
+
The gem is available as open source under the terms of the MIT License. See the
|
90
|
+
[LICENSE](./LICENSE) for detjsonails.
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "rails/version"
|
4
|
+
|
5
|
+
require "granola/rack"
|
6
|
+
require "rails/railtie"
|
7
|
+
require "active_support/concern"
|
8
|
+
|
9
|
+
module Granola
|
10
|
+
# Helpers to integrate Granola into Rails' rendering flow.
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
#
|
14
|
+
# class API::UsersController < ApplicationController
|
15
|
+
# def show
|
16
|
+
# user = User.find(params[:id])
|
17
|
+
# render json: user, with: API::UserSerializer
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
module Rails
|
21
|
+
# Internal: Let ActionController know about any renderers you add to Granola
|
22
|
+
# by defining an ActionController::Renderer based on Granola's.
|
23
|
+
#
|
24
|
+
# format - A Symbol with a rendering format (e.g. `:json`).
|
25
|
+
#
|
26
|
+
# Returns nothing.
|
27
|
+
def self.install_renderer(format)
|
28
|
+
ActionController::Renderers.remove(format)
|
29
|
+
ActionController::Renderers.add(format) do |object, options|
|
30
|
+
granola(object, **options.merge(as: format))
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
include Granola::Rack
|
35
|
+
|
36
|
+
# Public: Low-level API to respond with a serialized object. You should
|
37
|
+
# prefer using ActionController's `render` with the serialization format
|
38
|
+
# (e.g. `render json: object, with: SomeSerializer`).
|
39
|
+
#
|
40
|
+
# object - An Object to be passed to a Granola::Serializer.
|
41
|
+
# **options - Keywords to configure this response. See
|
42
|
+
# `Granola::Rack#granola` for a full list.
|
43
|
+
#
|
44
|
+
# Returns nothing.
|
45
|
+
def granola(object, **options)
|
46
|
+
options = { status: self.status, env: request.env }.update(options)
|
47
|
+
status, headers, body = super(object, **options)
|
48
|
+
|
49
|
+
response.status = status
|
50
|
+
response.headers.update(headers)
|
51
|
+
self.response_body = body
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
module RendererRegistration # :nodoc:
|
56
|
+
def self.render(format, *) # :nodoc:
|
57
|
+
super.tap { Granola::Rails.install_renderer(format) }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
class << self
|
62
|
+
# Defining a new rendering format should add it to Rails' rendering.
|
63
|
+
prepend RendererRegistration
|
64
|
+
end
|
65
|
+
|
66
|
+
class Railtie < ::Rails::Railtie # :nodoc:
|
67
|
+
initializer "granola.action_controller" do
|
68
|
+
ActiveSupport.on_load(:action_controller) do
|
69
|
+
include Granola::Rails
|
70
|
+
|
71
|
+
Granola.renderable_formats.each do |format|
|
72
|
+
Granola::Rails.install_renderer(format)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: granola-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nicolas Sanguinetti
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '6'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4.0'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '6'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: granola
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0.13'
|
40
|
+
- - "~>"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0.13'
|
50
|
+
- - "~>"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
description: |
|
54
|
+
Granola provides a simple and fast API to serialize objects into multiple
|
55
|
+
formats.
|
56
|
+
Granola::Rails just simplifies using Granola from Rails apps.
|
57
|
+
email:
|
58
|
+
- contacto@nicolassanguinetti.info
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- LICENSE
|
64
|
+
- README.md
|
65
|
+
- lib/granola/rails.rb
|
66
|
+
- lib/granola/rails/version.rb
|
67
|
+
homepage: https://github.com/foca/granola-rails
|
68
|
+
licenses:
|
69
|
+
- MIT
|
70
|
+
metadata: {}
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 2.5.1
|
88
|
+
signing_key:
|
89
|
+
specification_version: 4
|
90
|
+
summary: Rails interface to Granola serializers.
|
91
|
+
test_files: []
|