jsonapi-params 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/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +134 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/jsonapi-params.gemspec +26 -0
- data/lib/jsonapi-params/param.rb +117 -0
- data/lib/jsonapi-params/version.rb +5 -0
- data/lib/jsonapi-params.rb +8 -0
- metadata +125 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e5a249c1dc7ab63566967bc11cfda5a9cadcf9c3
|
4
|
+
data.tar.gz: 227888eacf0c5c0f538451b031d1344d63b42a5a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d07526167def4f51cf4f7cfaa8e854028e95891d66a1c41f6a81ba205ac71cf672b3fccc015f930865a70ef0a127e4120fa45b8960358a2cf790559a5ef45d4e
|
7
|
+
data.tar.gz: 772e64b470a3ff2f43da7724ace3898123b3b9033ca9e54447d0907643415d91af1e1a9cf0ae873b086b61e5b4847290774ff7d32d13b69c554ca7907677a827
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
# Jsonapi::Params
|
2
|
+
|
3
|
+
This gem handles the parameters of a request that uses the jsonapi specification and provides simple control over input parameters and manipulation of attributes, relationships and other...
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'jsonapi-params'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install jsonapi-params
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
To use jsonapi-params you should create a class to handle with your parameters. Example:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class AuthorParam
|
27
|
+
include JSONAPI::Param
|
28
|
+
|
29
|
+
params :name
|
30
|
+
end
|
31
|
+
|
32
|
+
class ArticleParam
|
33
|
+
include JSONAPI::Param
|
34
|
+
|
35
|
+
params :title, :text, :other_text
|
36
|
+
|
37
|
+
belongs_to :author
|
38
|
+
end
|
39
|
+
|
40
|
+
article = ArticleParam.new(
|
41
|
+
'data' => {
|
42
|
+
'id' => 1,
|
43
|
+
'type' => 'x',
|
44
|
+
'attributes' => {
|
45
|
+
'title' => 'The guy',
|
46
|
+
'text' => 'Loren Ipsun',
|
47
|
+
'other-text' => 'Hello'
|
48
|
+
},
|
49
|
+
'relationships' => {
|
50
|
+
'author' => {
|
51
|
+
'data' => {
|
52
|
+
'id' => 123,
|
53
|
+
'type' => 'authors',
|
54
|
+
'attributes' => {
|
55
|
+
'name' => 'Antonio'
|
56
|
+
}
|
57
|
+
}
|
58
|
+
}
|
59
|
+
}
|
60
|
+
}
|
61
|
+
)
|
62
|
+
```
|
63
|
+
|
64
|
+
### Available methods to handle with params
|
65
|
+
|
66
|
+
#### param
|
67
|
+
|
68
|
+
Adds a parameter to whitelist attributes
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
class AuthorParam
|
72
|
+
param :name
|
73
|
+
param :gender
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
#### params
|
78
|
+
|
79
|
+
Adds a list of parameters to whitelist attributes
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
class AuthorParam
|
83
|
+
params :name, :gender
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
#### belongs_to
|
88
|
+
|
89
|
+
Creates a one-to-one relationship to update or create objects
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
class AuthorParam
|
93
|
+
param :name
|
94
|
+
end
|
95
|
+
|
96
|
+
class ArticleParam
|
97
|
+
param :title
|
98
|
+
|
99
|
+
belongs_to :author
|
100
|
+
end
|
101
|
+
```
|
102
|
+
|
103
|
+
#### attributes
|
104
|
+
|
105
|
+
Returns a list of attributes based on whitelist.
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
class ArticleParam
|
109
|
+
param :title
|
110
|
+
end
|
111
|
+
|
112
|
+
article_params = ArticleParam.new('data' => { 'attributes' => { 'title' => 'The day' }})
|
113
|
+
article_params.attributes # { 'title' => 'The day' }
|
114
|
+
```
|
115
|
+
|
116
|
+
## TODO
|
117
|
+
|
118
|
+
* one-to-many relationships
|
119
|
+
* many-to-many relationships
|
120
|
+
* metadata
|
121
|
+
|
122
|
+
## Development
|
123
|
+
|
124
|
+
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.
|
125
|
+
|
126
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
127
|
+
|
128
|
+
## Contributing
|
129
|
+
|
130
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jsonapi-params.
|
131
|
+
|
132
|
+
## License
|
133
|
+
|
134
|
+
jsonapi-params is released under the MIT License.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "jsonapi-params"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'jsonapi-params/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "jsonapi-params"
|
8
|
+
spec.version = JSONAPI::Param::VERSION
|
9
|
+
spec.authors = ["Noverde Team"]
|
10
|
+
spec.email = ["dev@noverde.com.br"]
|
11
|
+
|
12
|
+
spec.summary = %q{Gem to handle with parameters according to jsonapi specification}
|
13
|
+
spec.homepage = "https://github.com/Noverde/jsonapi-params"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "activesupport", "> 4"
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
spec.add_development_dependency "pry-byebug"
|
26
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require 'active_support/core_ext/hash/slice'
|
2
|
+
require 'active_support/core_ext/string/inflections'
|
3
|
+
|
4
|
+
module JSONAPI
|
5
|
+
module Param
|
6
|
+
def self.included(target)
|
7
|
+
target.send(:include, InstanceMethods)
|
8
|
+
target.extend ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
attr_accessor :whitelist_attributes, :whitelist_relationships
|
13
|
+
|
14
|
+
# Adds the parameters to whitelist of parameters
|
15
|
+
#
|
16
|
+
# @param name [Symbol] The name of parameter
|
17
|
+
# @return [nil]
|
18
|
+
def param(name)
|
19
|
+
add_param(name)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Adds a list of parameters to whitelist of parameters
|
23
|
+
#
|
24
|
+
# @param name [Array<Symbol>] Names of parameters
|
25
|
+
# @return [nil]
|
26
|
+
def params(*names)
|
27
|
+
names.each { |name| add_param(name) }
|
28
|
+
end
|
29
|
+
|
30
|
+
# Adds a relationship one-to-one to whitelist of relationships
|
31
|
+
#
|
32
|
+
# @param name [Array<Symbol>] Names of relationships
|
33
|
+
# @return [nil]
|
34
|
+
def belongs_to(relationship_names)
|
35
|
+
@whitelist_relationships ||= []
|
36
|
+
@whitelist_relationships << relationship_names.to_s.dasherize
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
# Creates the whitelist of attributes
|
42
|
+
#
|
43
|
+
# @param [Symbol]
|
44
|
+
# @return [nil]
|
45
|
+
# @!scope class
|
46
|
+
# @!visibility private
|
47
|
+
def add_param(name)
|
48
|
+
@whitelist_attributes ||= []
|
49
|
+
@whitelist_attributes << name.to_s.dasherize
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
module InstanceMethods
|
54
|
+
def initialize(params)
|
55
|
+
raise InvalidParam, 'Data is required' if params.nil? || params['data'].nil?
|
56
|
+
|
57
|
+
@data = params['data']
|
58
|
+
end
|
59
|
+
|
60
|
+
# @returns [Integer]
|
61
|
+
# @!attribute [r]
|
62
|
+
def id
|
63
|
+
@data['id']
|
64
|
+
end
|
65
|
+
|
66
|
+
# @returns [String]
|
67
|
+
# @!attribute [r]
|
68
|
+
def type
|
69
|
+
@data['type']
|
70
|
+
end
|
71
|
+
|
72
|
+
# Handles parameters to return sanitized attributes and their relationships
|
73
|
+
#
|
74
|
+
# @return [Hash]
|
75
|
+
def attributes
|
76
|
+
attributes = @data['attributes'] || {}
|
77
|
+
attributes = attributes.slice(*self.class.whitelist_attributes)
|
78
|
+
attributes = attributes.merge(relationships)
|
79
|
+
attributes
|
80
|
+
end
|
81
|
+
|
82
|
+
# Handles parameters to return relationships
|
83
|
+
#
|
84
|
+
# @return [Hash]
|
85
|
+
# @raise [RuntimeError] if the relationship is a one-to-many relationship.
|
86
|
+
def relationships
|
87
|
+
relationships = @data['relationships'] || {}
|
88
|
+
relationships = relationships.slice(*self.class.whitelist_relationships)
|
89
|
+
|
90
|
+
relationships.inject({}) do |relationships, (relationship_key, relationship_object)|
|
91
|
+
data = relationship_object['data']
|
92
|
+
|
93
|
+
if data.is_a?(Array)
|
94
|
+
raise 'One-to-many relationship is not supported'
|
95
|
+
elsif data.is_a?(Hash)
|
96
|
+
params = params_klass(relationship_key).new(relationship_object)
|
97
|
+
|
98
|
+
relationships["#{relationship_key}_id"] = params.id
|
99
|
+
end
|
100
|
+
|
101
|
+
relationships
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
# Get the key to create a constant of param class.
|
108
|
+
#
|
109
|
+
# @param [String]
|
110
|
+
# @return [Object]
|
111
|
+
# @!visibility private
|
112
|
+
def params_klass(key)
|
113
|
+
"#{key}Param".classify.constantize
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jsonapi-params
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Noverde Team
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-26 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'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.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: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry-byebug
|
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
|
+
description:
|
84
|
+
email:
|
85
|
+
- dev@noverde.com.br
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- bin/console
|
97
|
+
- bin/setup
|
98
|
+
- jsonapi-params.gemspec
|
99
|
+
- lib/jsonapi-params.rb
|
100
|
+
- lib/jsonapi-params/param.rb
|
101
|
+
- lib/jsonapi-params/version.rb
|
102
|
+
homepage: https://github.com/Noverde/jsonapi-params
|
103
|
+
licenses: []
|
104
|
+
metadata: {}
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 2.5.1
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: Gem to handle with parameters according to jsonapi specification
|
125
|
+
test_files: []
|