grape_fast_jsonapi 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ed838d297ef0c44b39588e77a204a37a8c8b4b4f
4
+ data.tar.gz: 0e57addeb1bfeac8b6ff3e5c54ceb85d20f9cddb
5
+ SHA512:
6
+ metadata.gz: f65646281a1da90c7fb6e4d38918f67fe50c1467c3a54546b3a7a7d39e50189c907bf851c02b8bac01723196f038a842336e8f972a0772fd05d0b7b618fc9fa0
7
+ data.tar.gz: 62e22336e61cec1388e2980dac12e045facf7fd77c7bb0a8c3ee94b9e1a7735904bfa21d2fa819425af9669758ef6f7e23003e712abf27a03f70c553d2d55cc9
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'grape'
6
+ gem 'fast_jsonapi'
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Emmanuel Cousin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Grape::FastJsonapi
2
+
3
+ Use [fast_jsonapi](https://github.com/Netflix/fast_jsonapi) with [Grape](https://github.com/ruby-grape/grape).
4
+
5
+ ## Installation
6
+
7
+ Add the `grape` and `grape_fast_jsonapi` gems to Gemfile.
8
+
9
+ ```ruby
10
+ gem 'grape'
11
+ # gem is not published to rubygems yet
12
+ gem 'grape_fast_jsonapi', git: 'git@github.com:EmCousin/grape_fast_jsonapi.git'
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### Require grape_fast_jsonapi
18
+
19
+ ### Tell your API to use Grape::Formatter::FastJsonapi
20
+
21
+ ```ruby
22
+ class API < Grape::API
23
+ format :json
24
+ formatter :json, Grape::Formatter::FastJsonapi
25
+ end
26
+ ```
27
+
28
+ ### Use `render` to specify JSONAPI options
29
+
30
+ ```ruby
31
+ get "/" do
32
+ user = User.find("123")
33
+ render user, include: [:account]
34
+ end
35
+ ```
36
+
37
+ ## Credit
38
+
39
+ Code adapted from [grape-jsonapi-resources](https://github.com/cdunn/grape-jsonapi-resources)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ # frozen_string_literal: true
2
+ require 'bundler/gem_tasks'
@@ -0,0 +1,28 @@
1
+ # Generated by EmCousin
2
+ # -*- encoding: utf-8 -*-
3
+
4
+ require File.expand_path('../lib/grape_fast_jsonapi/version', __FILE__)
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.authors = ['Emmanuel Cousin']
8
+ gem.email = ['emmanuel_cousin@hotmail.fr']
9
+ gem.summary = 'Use fast_jsonapi in grape'
10
+ gem.description = 'Provides a Formatter for the Grape API DSL to emit objects serialized with fast_jsonapi.'
11
+ gem.homepage = 'https://github.com/EmCousin/grape_fast_jsonapi'
12
+
13
+ gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
14
+ gem.files = `git ls-files`.split("\n")
15
+ gem.test_files = `git ls-files -- {test,spec}/*`.split("\n")
16
+ gem.name = 'grape_fast_jsonapi'
17
+ gem.require_paths = ['lib']
18
+ gem.version = Grape::FastJsonapi::VERSION
19
+ gem.licenses = ['MIT']
20
+
21
+ gem.add_dependency 'grape'
22
+ gem.add_dependency 'fast_jsonapi'
23
+
24
+ gem.add_development_dependency 'rails', '>= 5.0.0'
25
+ gem.add_development_dependency 'rspec'
26
+ gem.add_development_dependency 'rack-test'
27
+ gem.add_development_dependency 'rake'
28
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Grape
4
+ module EndpointExtension
5
+ def render(resources, options = {})
6
+ env['fast_jsonapi_options'] = options
7
+ resources
8
+ end
9
+ end
10
+
11
+ Endpoint.send(:include, EndpointExtension)
12
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Grape
4
+ module Formatter
5
+ module FastJsonapi
6
+ class << self
7
+ def call(object, env)
8
+ return object if object.is_a?(String)
9
+ return ::Grape::Json.dump(serialize(object, env)) if serializable?(object)
10
+ return object.to_json if object.respond_to?(:to_json)
11
+ ::Grape::Json.dump(object)
12
+ end
13
+
14
+ private
15
+
16
+ def serializable?(object)
17
+ object.respond_to?(:serializable_hash) || object.respond_to?(:to_a) && object.all? { |o| o.respond_to? :serializable_hash } || object.is_a?(Hash)
18
+ end
19
+
20
+ def serialize(object, env)
21
+ if object.respond_to? :serializable_hash
22
+ serializable_object(object, fast_jsonapi_options(env)).serializable_hash
23
+ elsif object.respond_to?(:to_a) && object.all? { |o| o.respond_to? :serializable_hash }
24
+ fast_jsonapi_serializable(object, fast_jsonapi_options(env)).serializable_hash || object.map(&:serializable_hash)
25
+ elsif object.is_a?(Hash)
26
+ serialize_each_pair(object, env)
27
+ else
28
+ object
29
+ end
30
+ end
31
+
32
+ def serializable_object(object, options)
33
+ fast_jsonapi_serializable(object, options) || object
34
+ end
35
+
36
+ def fast_jsonapi_serializable(object, options)
37
+ serializable_class(object)&.new(object, options)
38
+ end
39
+
40
+ def serializable_class(object)
41
+ (object.model_name.name + 'Serializer').safe_constantize
42
+ end
43
+
44
+ def serialize_each_pair(object, env)
45
+ h = {}
46
+ object.each_pair { |k, v| h[k] = serialize(v, env) }
47
+ h
48
+ end
49
+
50
+ def fast_jsonapi_options(env)
51
+ env['fast_jsonapi_options'] || {}
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Grape
4
+ module FastJsonapi
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ require 'fast_jsonapi'
2
+ require 'grape'
3
+ require 'grape_fast_jsonapi/endpoint_extension'
4
+ require 'grape_fast_jsonapi/formatter'
5
+ require 'grape_fast_jsonapi/version'
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape_fast_jsonapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Emmanuel Cousin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-06-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: grape
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fast_jsonapi
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 5.0.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 5.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Provides a Formatter for the Grape API DSL to emit objects serialized
98
+ with fast_jsonapi.
99
+ email:
100
+ - emmanuel_cousin@hotmail.fr
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - Gemfile
106
+ - LICENSE
107
+ - README.md
108
+ - Rakefile
109
+ - grape_fast_jsonapi.gemspec
110
+ - lib/grape_fast_jsonapi.rb
111
+ - lib/grape_fast_jsonapi/endpoint_extension.rb
112
+ - lib/grape_fast_jsonapi/formatter.rb
113
+ - lib/grape_fast_jsonapi/version.rb
114
+ homepage: https://github.com/EmCousin/grape_fast_jsonapi
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.5.2
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: Use fast_jsonapi in grape
138
+ test_files: []