api_pack 1.0.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/.coveralls.yml +1 -0
- data/.fasterer.yml +5 -0
- data/.gitignore +13 -0
- data/.overcommit.yml +44 -0
- data/.rspec +3 -0
- data/.rubocop.yml +35 -0
- data/.travis.yml +16 -0
- data/Gemfile +24 -0
- data/LICENSE.txt +21 -0
- data/README.md +42 -0
- data/Rakefile +6 -0
- data/api_pack.gemspec +32 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/api_pack.rb +36 -0
- data/lib/api_pack/constants.rb +3 -0
- data/lib/api_pack/errors/api_errors_serializer.rb +39 -0
- data/lib/api_pack/errors/auth.rb +9 -0
- data/lib/api_pack/errors/error_map.rb +38 -0
- data/lib/api_pack/errors/handle_error.rb +63 -0
- data/lib/api_pack/errors/validation_error_serializer.rb +25 -0
- data/lib/api_pack/errors/validation_errors_serializer.rb +19 -0
- data/lib/api_pack/json_web_token.rb +15 -0
- data/lib/api_pack/pagination_meta_generator.rb +75 -0
- data/lib/api_pack/serializer/adapter/fast_json_api.rb +16 -0
- data/lib/api_pack/serializer/parser.rb +26 -0
- data/lib/api_pack/support/api_helper.rb +12 -0
- data/lib/api_pack/version.rb +3 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a343d0ee7607c4f0c4b075e1646bc773a93cd06559115321f033637b545868e4
|
4
|
+
data.tar.gz: fb14ee9d7150fb83829d8c54a2e92a152a1d9765c5a5fa51b0c6dcb5d58595ac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ca5d0110ce9790804a3daf4830ade07a4e71c8c60e564fd79aca96eac6ee6a16b56854789de00fa4cf601bbb615b6fc13c363deec25197e7d92c1518e19cf9b9
|
7
|
+
data.tar.gz: 5258dbb75b045129a23d249d93586c4d7a93aedc0a9f490c141f551915b5076c574ae6eecbc9a7f47f24680334104c5c000d3a9cd237bc91db655a84a17e1ea6
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.fasterer.yml
ADDED
data/.gitignore
ADDED
data/.overcommit.yml
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
CommitMsg:
|
2
|
+
CapitalizedSubject:
|
3
|
+
enabled: false
|
4
|
+
|
5
|
+
EmptyMessage:
|
6
|
+
enabled: false
|
7
|
+
|
8
|
+
TrailingPeriod:
|
9
|
+
enabled: true
|
10
|
+
|
11
|
+
TextWidth:
|
12
|
+
enabled: false
|
13
|
+
|
14
|
+
PreCommit:
|
15
|
+
ALL:
|
16
|
+
on_warn: fail
|
17
|
+
|
18
|
+
AuthorEmail:
|
19
|
+
enabled: true
|
20
|
+
|
21
|
+
AuthorName:
|
22
|
+
enabled: true
|
23
|
+
|
24
|
+
BundleCheck:
|
25
|
+
enabled: true
|
26
|
+
|
27
|
+
BundleAudit:
|
28
|
+
enabled: true
|
29
|
+
|
30
|
+
Fasterer:
|
31
|
+
enabled: true
|
32
|
+
|
33
|
+
RuboCop:
|
34
|
+
enabled: true
|
35
|
+
|
36
|
+
RubyCritic:
|
37
|
+
enabled: true
|
38
|
+
|
39
|
+
MergeConflicts:
|
40
|
+
enabled: true
|
41
|
+
|
42
|
+
PrePush:
|
43
|
+
RSpec:
|
44
|
+
enabled: true
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- 'spec/**/*'
|
4
|
+
- 'bin/**/*'
|
5
|
+
- api_pack.gemspec
|
6
|
+
- 'lib/api_pack/constants.rb'
|
7
|
+
TargetRubyVersion: 2.7.1
|
8
|
+
NewCops: enable
|
9
|
+
|
10
|
+
Layout/LineLength:
|
11
|
+
Max: 120
|
12
|
+
|
13
|
+
Metrics/MethodLength:
|
14
|
+
Max: 20
|
15
|
+
|
16
|
+
Metrics/AbcSize:
|
17
|
+
Max: 18
|
18
|
+
|
19
|
+
Style/FrozenStringLiteralComment:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
Style/Documentation:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
Style/SpecialGlobalVars:
|
26
|
+
Enabled: false
|
27
|
+
|
28
|
+
Style/SymbolArray:
|
29
|
+
Enabled: true
|
30
|
+
|
31
|
+
Style/StringLiterals:
|
32
|
+
Enabled: false
|
33
|
+
|
34
|
+
Style/GlobalStdStream:
|
35
|
+
Enabled: false
|
data/.travis.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
---
|
2
|
+
sudo: false
|
3
|
+
language: ruby
|
4
|
+
cache: bundler
|
5
|
+
rvm:
|
6
|
+
- 2.4.0
|
7
|
+
- 2.5.0
|
8
|
+
- 2.6.0
|
9
|
+
- 2.7.0
|
10
|
+
before_install:
|
11
|
+
- gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
|
12
|
+
- gem install bundler -v '< 2'
|
13
|
+
install: bundle install --jobs=3 --retry=3
|
14
|
+
|
15
|
+
after_success:
|
16
|
+
- CI=true TRAVIS=true coveralls --verbose
|
data/Gemfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
4
|
+
|
5
|
+
gemspec
|
6
|
+
|
7
|
+
gem 'fast_jsonapi'
|
8
|
+
gem 'jwt'
|
9
|
+
|
10
|
+
group :test do
|
11
|
+
gem 'faker'
|
12
|
+
gem 'rspec', '~> 3.7'
|
13
|
+
end
|
14
|
+
|
15
|
+
group :development, :test do
|
16
|
+
gem 'bundler-audit'
|
17
|
+
gem 'coveralls_reborn', '~> 0.18.0', require: false
|
18
|
+
gem 'fasterer'
|
19
|
+
gem 'overcommit'
|
20
|
+
gem 'pry'
|
21
|
+
gem 'rubocop', require: false
|
22
|
+
gem 'rubycritic', require: false
|
23
|
+
gem 'simplecov', require: false
|
24
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Jorge
|
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,42 @@
|
|
1
|
+
[](https://travis-ci.org/Jllima/api_pack)
|
2
|
+
[](https://coveralls.io/github/Jllima/api_pack?branch=master)
|
3
|
+
|
4
|
+
# ApiPack
|
5
|
+
|
6
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/api_pack`. To experiment with that code, run `bin/console` for an interactive prompt.
|
7
|
+
|
8
|
+
TODO: Delete this and the text above, and describe your gem
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'api_pack'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install api_pack
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
TODO: Write usage instructions here
|
29
|
+
|
30
|
+
## Development
|
31
|
+
|
32
|
+
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.
|
33
|
+
|
34
|
+
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).
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/api_pack.
|
39
|
+
|
40
|
+
## License
|
41
|
+
|
42
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/api_pack.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'api_pack/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'api_pack'
|
8
|
+
spec.version = ApiPack::VERSION
|
9
|
+
spec.authors = ['Jorge']
|
10
|
+
spec.email = ['jlimajorge@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Api requirements'
|
13
|
+
spec.description = 'Api requirements pack for api rails development'
|
14
|
+
spec.homepage = 'https://github.com/Jllima/api_pack'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
raise 'RubyGems 2.4 or newer is required to protect against public gem pushes.' unless spec.respond_to?(:metadata)
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = 'exe'
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ['lib']
|
27
|
+
|
28
|
+
spec.required_ruby_version = '>= 2.4.0'
|
29
|
+
|
30
|
+
spec.add_development_dependency 'bundler'
|
31
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
32
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "api_pack"
|
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(__FILE__)
|
data/bin/setup
ADDED
data/lib/api_pack.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'jwt'
|
2
|
+
require 'fast_jsonapi'
|
3
|
+
require 'api_pack/version'
|
4
|
+
require 'api_pack/json_web_token'
|
5
|
+
require 'api_pack/pagination_meta_generator'
|
6
|
+
require 'api_pack/errors/auth'
|
7
|
+
require 'api_pack/errors/error_map'
|
8
|
+
require 'api_pack/errors/handle_error'
|
9
|
+
require 'api_pack/errors/api_errors_serializer'
|
10
|
+
require 'api_pack/errors/validation_error_serializer'
|
11
|
+
require 'api_pack/errors/validation_errors_serializer'
|
12
|
+
require 'api_pack/constants'
|
13
|
+
require 'api_pack/support/api_helper'
|
14
|
+
require 'api_pack/serializer/parser'
|
15
|
+
|
16
|
+
module ApiPack
|
17
|
+
module_function
|
18
|
+
|
19
|
+
PER_PAGE = 10
|
20
|
+
# 24 hours from now
|
21
|
+
DEFAULT_EXP = (Time.now + 1 * 86_400).to_i
|
22
|
+
|
23
|
+
attr_writer :default_per_page, :exp
|
24
|
+
|
25
|
+
def default_per_page
|
26
|
+
@default_per_page ||= PER_PAGE
|
27
|
+
end
|
28
|
+
|
29
|
+
def exp
|
30
|
+
@exp ||= DEFAULT_EXP
|
31
|
+
end
|
32
|
+
|
33
|
+
def serializer_adapter=(adapter)
|
34
|
+
ApiPack::Serializer::Parser.adapter = adapter
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module ApiPack
|
2
|
+
module Errors
|
3
|
+
class ApiErrorsSerializer
|
4
|
+
def initialize(body)
|
5
|
+
@body = body
|
6
|
+
end
|
7
|
+
|
8
|
+
def serializer
|
9
|
+
serializable
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
attr_accessor :body
|
15
|
+
|
16
|
+
def serializable
|
17
|
+
return serializable_array if body[:details].is_a?(Array)
|
18
|
+
|
19
|
+
serializable_object
|
20
|
+
end
|
21
|
+
|
22
|
+
def serializable_object
|
23
|
+
{
|
24
|
+
errors: [
|
25
|
+
body
|
26
|
+
]
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
def serializable_array
|
31
|
+
{
|
32
|
+
title: body[:title],
|
33
|
+
status: body[:status],
|
34
|
+
errors: body[:details]
|
35
|
+
}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module ApiPack
|
2
|
+
module Errors
|
3
|
+
module ErrorMap
|
4
|
+
ERROR_MAP = {
|
5
|
+
'ActiveRecord::RecordNotFound' => {
|
6
|
+
method: :error_message_body,
|
7
|
+
title: 'Not Found',
|
8
|
+
status: :not_found
|
9
|
+
},
|
10
|
+
'ActionController::ParameterMissing' => {
|
11
|
+
method: :parameter_missing,
|
12
|
+
title: 'Parameter Missing',
|
13
|
+
status: :unprocessable_entity
|
14
|
+
},
|
15
|
+
'ActiveRecord::RecordInvalid' => {
|
16
|
+
method: :record_invalid,
|
17
|
+
title: 'Validations Failed',
|
18
|
+
status: :unprocessable_entity
|
19
|
+
},
|
20
|
+
'ApiPack::Errors::Auth::AuthenticationError' => {
|
21
|
+
method: :error_message_body,
|
22
|
+
title: 'Invalid Credentials',
|
23
|
+
status: :unauthorized
|
24
|
+
},
|
25
|
+
'ApiPack::Errors::Auth::InvalidToken' => {
|
26
|
+
method: :error_message_body,
|
27
|
+
title: 'Invalid Token',
|
28
|
+
status: :unprocessable_entity
|
29
|
+
},
|
30
|
+
'ApiPack::Errors::Auth::MissingToken' => {
|
31
|
+
method: :error_message_body,
|
32
|
+
title: 'Missing Token',
|
33
|
+
status: :unprocessable_entity
|
34
|
+
}
|
35
|
+
}.freeze
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module ApiPack
|
2
|
+
module Errors
|
3
|
+
class HandleError
|
4
|
+
include Errors::ErrorMap
|
5
|
+
|
6
|
+
def initialize(error)
|
7
|
+
@error = error
|
8
|
+
end
|
9
|
+
|
10
|
+
def call
|
11
|
+
handle_error
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
attr_accessor :error
|
17
|
+
|
18
|
+
def handle_error
|
19
|
+
if ERROR_MAP.keys.include? error.class.name
|
20
|
+
handler = ERROR_MAP[error.class.name]
|
21
|
+
body = send(handler[:method], handler: handler, error: error)
|
22
|
+
else
|
23
|
+
body = {
|
24
|
+
title: 'Internal Server Error',
|
25
|
+
details: error.message,
|
26
|
+
status: :internal_server_error
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
{ body: serialize(body), status: body[:status] }
|
31
|
+
end
|
32
|
+
|
33
|
+
def serialize(body)
|
34
|
+
ApiPack::Errors::ApiErrorsSerializer.new(body).serializer
|
35
|
+
end
|
36
|
+
|
37
|
+
def error_message_body(handler:, error:)
|
38
|
+
{
|
39
|
+
title: handler[:title],
|
40
|
+
status: handler[:status],
|
41
|
+
details: error.message
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
def parameter_missing(handler:, error:)
|
46
|
+
{
|
47
|
+
title: handler[:title],
|
48
|
+
details: error.message,
|
49
|
+
status: handler[:status],
|
50
|
+
parameter: error.param
|
51
|
+
}
|
52
|
+
end
|
53
|
+
|
54
|
+
def record_invalid(handler:, error:)
|
55
|
+
{
|
56
|
+
title: handler[:title],
|
57
|
+
status: handler[:status],
|
58
|
+
details: ApiPack::Errors::ValidationErrorsSerializer.new(error.record).serialize
|
59
|
+
}
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module ApiPack
|
2
|
+
module Errors
|
3
|
+
class ValidationErrorSerializer
|
4
|
+
def initialize(record, field, message)
|
5
|
+
@record = record
|
6
|
+
@field = field
|
7
|
+
@message = message
|
8
|
+
end
|
9
|
+
|
10
|
+
def serialize
|
11
|
+
{
|
12
|
+
resource: underscored_resource_name,
|
13
|
+
field: @field,
|
14
|
+
detail: @message
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def underscored_resource_name
|
21
|
+
@record.class.to_s.gsub('::', '').underscore
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ApiPack
|
2
|
+
module Errors
|
3
|
+
class ValidationErrorsSerializer
|
4
|
+
attr_reader :record
|
5
|
+
|
6
|
+
def initialize(record)
|
7
|
+
@record = record
|
8
|
+
end
|
9
|
+
|
10
|
+
def serialize
|
11
|
+
record.errors.messages.map do |field, messages|
|
12
|
+
messages.map do |error_messages|
|
13
|
+
ApiPack::Errors::ValidationErrorSerializer.new(record, field, error_messages).serialize
|
14
|
+
end
|
15
|
+
end.flatten
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ApiPack
|
2
|
+
class JsonWebToken
|
3
|
+
def self.encode(payload, exp: ApiPack.exp)
|
4
|
+
payload[:exp] = exp.to_i
|
5
|
+
|
6
|
+
JWT.encode(payload, ApiPack::HMAC_SECRET)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.decode(token)
|
10
|
+
JWT.decode(token, ApiPack::HMAC_SECRET).first
|
11
|
+
rescue JWT::DecodeError => e
|
12
|
+
raise ApiPack::Errors::Auth::InvalidToken, e.message
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require './lib/api_pack/support/api_helper'
|
2
|
+
|
3
|
+
module ApiPack
|
4
|
+
class PaginationMetaGenerator
|
5
|
+
include Support::ApiHelper
|
6
|
+
|
7
|
+
def initialize(request:, total_pages:)
|
8
|
+
@url = "#{request.base_url}#{request.path}"
|
9
|
+
@page = request.params[:page].to_i
|
10
|
+
@per_page = request.params[:per_page].to_i
|
11
|
+
@total_pages = total_pages
|
12
|
+
end
|
13
|
+
|
14
|
+
def call
|
15
|
+
{
|
16
|
+
links: links,
|
17
|
+
meta: {
|
18
|
+
current_page: current_page,
|
19
|
+
total_pages: @total_pages
|
20
|
+
}
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
attr_accessor :url, :hash
|
27
|
+
|
28
|
+
attr_reader :per_page, :page, :total_pages
|
29
|
+
|
30
|
+
def links
|
31
|
+
links = {
|
32
|
+
self: generate_url,
|
33
|
+
first: generate_url(number_page: 1),
|
34
|
+
last: generate_url(number_page: total_pages)
|
35
|
+
}
|
36
|
+
|
37
|
+
links[:next] = generate_url(number_page: next_number(page)) if current_page != total_pages
|
38
|
+
links[:prev] = generate_url(number_page: page - 1) if page > 1
|
39
|
+
|
40
|
+
links
|
41
|
+
end
|
42
|
+
|
43
|
+
def generate_url(number_page: 0)
|
44
|
+
return url if number_page.zero?
|
45
|
+
|
46
|
+
[url, url_params(number_page)].join("?")
|
47
|
+
end
|
48
|
+
|
49
|
+
def url_params(number_page)
|
50
|
+
url_params = {}
|
51
|
+
url_params[:per_page] = include_per_page
|
52
|
+
url_params[:page] = number_page
|
53
|
+
|
54
|
+
to_query_api(url_params)
|
55
|
+
end
|
56
|
+
|
57
|
+
def include_per_page
|
58
|
+
return ApiPack.default_per_page if per_page.zero?
|
59
|
+
|
60
|
+
per_page
|
61
|
+
end
|
62
|
+
|
63
|
+
def next_number(number_page)
|
64
|
+
return number_page + 2 if number_page.zero?
|
65
|
+
|
66
|
+
number_page + 1
|
67
|
+
end
|
68
|
+
|
69
|
+
def current_page
|
70
|
+
return 1 if page.zero?
|
71
|
+
|
72
|
+
page
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module ApiPack
|
2
|
+
module Serializer
|
3
|
+
module Adapter
|
4
|
+
module FastJsonApi
|
5
|
+
module_function
|
6
|
+
|
7
|
+
def serializer_hash(resource, klass, opt: {})
|
8
|
+
name_klass = klass.to_s.split('_').collect(&:capitalize).join
|
9
|
+
|
10
|
+
serializer = "#{name_klass}Serializer"
|
11
|
+
FastJsonapi.const_get(serializer).new(resource, opt).serializable_hash
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module ApiPack
|
2
|
+
module Serializer
|
3
|
+
module Parser
|
4
|
+
module_function
|
5
|
+
|
6
|
+
def serializer_hash(resource, klass, opt: {})
|
7
|
+
adapter.serializer_hash(resource, klass, opt: opt)
|
8
|
+
end
|
9
|
+
|
10
|
+
def adapter
|
11
|
+
return @adapter if @adapter
|
12
|
+
|
13
|
+
self.adapter = :fast_json_api
|
14
|
+
@adapter
|
15
|
+
end
|
16
|
+
|
17
|
+
def adapter=(adapter)
|
18
|
+
require "./lib/api_pack/serializer/adapter/#{adapter}"
|
19
|
+
|
20
|
+
adapter_name = adapter.to_s.split('_').collect(&:capitalize).join
|
21
|
+
|
22
|
+
@adapter = Serializer::Adapter.const_get(adapter_name)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: api_pack
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jorge
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-10-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
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: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '13.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '13.0'
|
41
|
+
description: Api requirements pack for api rails development
|
42
|
+
email:
|
43
|
+
- jlimajorge@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".coveralls.yml"
|
49
|
+
- ".fasterer.yml"
|
50
|
+
- ".gitignore"
|
51
|
+
- ".overcommit.yml"
|
52
|
+
- ".rspec"
|
53
|
+
- ".rubocop.yml"
|
54
|
+
- ".travis.yml"
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- api_pack.gemspec
|
60
|
+
- bin/console
|
61
|
+
- bin/setup
|
62
|
+
- lib/api_pack.rb
|
63
|
+
- lib/api_pack/constants.rb
|
64
|
+
- lib/api_pack/errors/api_errors_serializer.rb
|
65
|
+
- lib/api_pack/errors/auth.rb
|
66
|
+
- lib/api_pack/errors/error_map.rb
|
67
|
+
- lib/api_pack/errors/handle_error.rb
|
68
|
+
- lib/api_pack/errors/validation_error_serializer.rb
|
69
|
+
- lib/api_pack/errors/validation_errors_serializer.rb
|
70
|
+
- lib/api_pack/json_web_token.rb
|
71
|
+
- lib/api_pack/pagination_meta_generator.rb
|
72
|
+
- lib/api_pack/serializer/adapter/fast_json_api.rb
|
73
|
+
- lib/api_pack/serializer/parser.rb
|
74
|
+
- lib/api_pack/support/api_helper.rb
|
75
|
+
- lib/api_pack/version.rb
|
76
|
+
homepage: https://github.com/Jllima/api_pack
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 2.4.0
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubygems_version: 3.0.6
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Api requirements
|
99
|
+
test_files: []
|