json_api_helpers 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.MD +111 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/json_api_helpers/action_dispatch_request_wrapper.rb +29 -0
- data/lib/json_api_helpers/version.rb +8 -0
- metadata +149 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ee5c767e4160048ed62bcbc7b9f8285142c7042f
|
4
|
+
data.tar.gz: c85ffe5e93fa30146724cd367420d1cf2c7641e1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e3145a51ab89a918439563acade05081f2bf8e7056653e53881d7fdba8bff2f4315a184b98430400fca1ebe00e86f8e2b51877487a70b18a61196ac7886b15d8
|
7
|
+
data.tar.gz: cb95f80bc149217ee25ecf5266f96034d07a7e511e5bcb89936cdfb454640a760d31750ee18732682578027c5bbcb882588abb3ca65fdbac7c3ac6355a21e1d9
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Jacob Burenstam Linder
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.MD
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
# JsonApiHelpers
|
2
|
+
|
3
|
+
A set of helpers for generating JSON API compliant responses with together with the active_model_serializers gem.
|
4
|
+
|
5
|
+
:warning: The API for this is still pretty rough.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'json_api_helpers'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install json_api_helpers
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
:warning: You must configure `deserializer_klass` and `params_klass`, if you want to use the `Dersializer`.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'json_api_helpers'
|
29
|
+
|
30
|
+
include JsonApiHelpers::Alias
|
31
|
+
|
32
|
+
JsonApiHelpers.configure do |config|
|
33
|
+
config.deserializer_klass = ActiveModelSerializers::Deserialization
|
34
|
+
config.params_klass = ActionController::Parameters
|
35
|
+
# optional (unaltered is the default)
|
36
|
+
config.key_transform = :dash # camel, camel_lower, underscore, unaltered
|
37
|
+
end
|
38
|
+
|
39
|
+
# Error
|
40
|
+
errors = JsonApiHelpers::Serializers::Errors.new
|
41
|
+
errors.add(status: 422, detail: 'too short', attribute: :first_name).to_h
|
42
|
+
errors.to_h
|
43
|
+
# => {
|
44
|
+
# errors: [{
|
45
|
+
# :status => 422,
|
46
|
+
# :detail => 'too short',
|
47
|
+
# :source => {
|
48
|
+
# :pointer => "/data/attributes/first-name"
|
49
|
+
# }
|
50
|
+
# }]
|
51
|
+
# }
|
52
|
+
|
53
|
+
# In ApplicationController you can define
|
54
|
+
class ApplicationController < ActionController::Base
|
55
|
+
# Define these constants in your controllers that you'd like to have
|
56
|
+
# different/custom behavior
|
57
|
+
DEFAULT_SORTING = { created_at: :desc }.freeze
|
58
|
+
SORTABLE_FIELDS = [].freeze
|
59
|
+
|
60
|
+
ALLOWED_INCLUDES = [].freeze
|
61
|
+
|
62
|
+
TRANSFORMABLE_FILTERS = { created_at: :date_range }.freeze
|
63
|
+
ALLOWED_FILTERS = %i(created_at).freeze
|
64
|
+
|
65
|
+
def jsonapi_params
|
66
|
+
@_deserialized_params ||= JsonApiHelpers::Serializers::Deserializer.parse(params)
|
67
|
+
end
|
68
|
+
|
69
|
+
def include_params
|
70
|
+
@_include_params ||= JsonApiHelpers::Params::Includes.new(params[:include])
|
71
|
+
end
|
72
|
+
|
73
|
+
def fields_params
|
74
|
+
@_fields_params ||= JsonApiHelpers::Params::Fields.new(params[:fields])
|
75
|
+
end
|
76
|
+
|
77
|
+
def sort_params
|
78
|
+
sortable_fields = self.class::SORTABLE_FIELDS
|
79
|
+
default_sorting = self.class::DEFAULT_SORTING
|
80
|
+
@_sort_params = JsonApiHelpers::Params::Sort.build(params[:sort], sortable_fields, default_sorting)
|
81
|
+
end
|
82
|
+
|
83
|
+
def filter_params
|
84
|
+
filterable_fields = self.class::ALLOWED_FILTERS
|
85
|
+
transformable = self.class::TRANSFORMABLE_FILTERS
|
86
|
+
@_filter_params = JsonApiHelpers::Params::Filter.build(params[:filter], filterable_fields, transformable)
|
87
|
+
end
|
88
|
+
|
89
|
+
# ...
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
## Development
|
94
|
+
|
95
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
96
|
+
|
97
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
98
|
+
|
99
|
+
## Future
|
100
|
+
|
101
|
+
* Testing the `Serializer` class is currently not "possible" and the code in there is hard to follow and not very well extracted, this should be fixed :)
|
102
|
+
* Better dependency injection
|
103
|
+
* Test `JsonApiHelpers::Serializer` (currently those tests areas in the parent repos tests
|
104
|
+
|
105
|
+
## Contributing
|
106
|
+
|
107
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/buren/json_api_helpers/issues.
|
108
|
+
|
109
|
+
## License
|
110
|
+
|
111
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'json_api_helpers'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JsonApiHelpers
|
4
|
+
# NOTE:
|
5
|
+
# For some weird reason, ActiveModelSerializers call #original_url on
|
6
|
+
# ActionDispatch::Request, which isn't defined. In order to avoid monkey matching,
|
7
|
+
# this wrapper object delegates all methods to the request object and only adds one
|
8
|
+
# method #request_url
|
9
|
+
class ActionDispatchRequestWrapper < Delegator
|
10
|
+
attr_reader :action_dispatch_request
|
11
|
+
|
12
|
+
def initialize(request)
|
13
|
+
super
|
14
|
+
@action_dispatch_request = request
|
15
|
+
end
|
16
|
+
|
17
|
+
def __getobj__
|
18
|
+
@action_dispatch_request
|
19
|
+
end
|
20
|
+
|
21
|
+
def __setobj__(request)
|
22
|
+
@action_dispatch_request = request
|
23
|
+
end
|
24
|
+
|
25
|
+
def request_url
|
26
|
+
"#{@action_dispatch_request.base_url}#{@action_dispatch_request.path}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json_api_helpers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jacob Burenstam
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: active_model_serializers
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activemodel
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.12'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.12'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: byebug
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '9.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '9.0'
|
111
|
+
description: A set of helpers for generating JSON API compliant responses with together
|
112
|
+
with the active_model_serializers gem.
|
113
|
+
email:
|
114
|
+
- burenstam@gmail.com
|
115
|
+
executables: []
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- LICENSE.txt
|
120
|
+
- README.MD
|
121
|
+
- bin/console
|
122
|
+
- bin/setup
|
123
|
+
- lib/json_api_helpers/action_dispatch_request_wrapper.rb
|
124
|
+
- lib/json_api_helpers/version.rb
|
125
|
+
homepage: https://github.com/buren/json_api_helpers
|
126
|
+
licenses:
|
127
|
+
- MIT
|
128
|
+
metadata: {}
|
129
|
+
post_install_message:
|
130
|
+
rdoc_options: []
|
131
|
+
require_paths:
|
132
|
+
- lib
|
133
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - ">="
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
requirements: []
|
144
|
+
rubyforge_project:
|
145
|
+
rubygems_version: 2.6.13
|
146
|
+
signing_key:
|
147
|
+
specification_version: 4
|
148
|
+
summary: A set of helpers for generating JSON API compliant responses.
|
149
|
+
test_files: []
|