api_pack 1.0.0 → 1.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +181 -8
- data/lib/api_pack.rb +15 -10
- data/lib/api_pack/api_helper.rb +23 -0
- data/lib/api_pack/errors/auth.rb +1 -0
- data/lib/api_pack/errors/error_map.rb +2 -2
- data/lib/api_pack/json_web_token.rb +8 -3
- data/lib/api_pack/pagination_meta_generator.rb +2 -2
- data/lib/api_pack/serializer/adapter/fast_json_api.rb +3 -0
- data/lib/api_pack/serializer/parser.rb +1 -1
- data/lib/api_pack/support/{api_helper.rb → helper.rb} +1 -1
- data/lib/api_pack/version.rb +1 -1
- metadata +8 -8
- data/lib/api_pack/constants.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5e149e010ed940c7c698c350fd2e7198ba9e51be516bdb7acb31f641b9b1c318
|
4
|
+
data.tar.gz: 3ff8a7976be4754ec1bbc2cd1bb1eed677c6d9a49470c6a1c3163f77a34d924b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8fc5aefe79bfb3edd53c1249afcb6b95c65092d111ef296589c4f6d5d2d9065bea188cce8e194d16dbb2a9a6b6a9edb42b2acfa97485e729fb148d8cc2d1b15e
|
7
|
+
data.tar.gz: 41ce89817c9053c426bb52b617a757ccc4953ecaf0ba65b0da5d8798c39288bf5242eb1d5ddb0c7160c18202067646f4e2558e2939aefa610b826a449243a373
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,18 +1,30 @@
|
|
1
|
-
[![
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/api_pack.svg)](https://badge.fury.io/rb/api_pack)
|
2
|
+
[![Build Status](https://www.travis-ci.com/Jllima/api_pack.svg?branch=master)](https://www.travis-ci.com/Jllima/api_pack)
|
2
3
|
[![Coverage Status](https://coveralls.io/repos/github/Jllima/api_pack/badge.svg?branch=master)](https://coveralls.io/github/Jllima/api_pack?branch=master)
|
3
4
|
|
4
5
|
# ApiPack
|
5
6
|
|
6
|
-
|
7
|
+
ApiPack is a set of helpers to Api rails
|
7
8
|
|
8
|
-
|
9
|
+
**This project provides the following helpers**
|
10
|
+
|
11
|
+
- Json Web Token helpers
|
12
|
+
- Json Api especifications
|
13
|
+
- Errors Serializers
|
14
|
+
- Pagination links Serializers
|
15
|
+
- Serializer Adapter (BETA)
|
16
|
+
|
17
|
+
## Compatibility
|
18
|
+
```
|
19
|
+
ruby >= 2.4
|
20
|
+
```
|
9
21
|
|
10
22
|
## Installation
|
11
23
|
|
12
24
|
Add this line to your application's Gemfile:
|
13
25
|
|
14
26
|
```ruby
|
15
|
-
gem 'api_pack'
|
27
|
+
gem 'api_pack', '~> 1.1.3'
|
16
28
|
```
|
17
29
|
|
18
30
|
And then execute:
|
@@ -25,13 +37,174 @@ Or install it yourself as:
|
|
25
37
|
|
26
38
|
## Usage
|
27
39
|
|
28
|
-
|
40
|
+
### JsonWebToken methods
|
41
|
+
- First create file api_pack.rb in initializers in put this code
|
42
|
+
```ruby
|
43
|
+
Rails.application.config.to_prepare do
|
44
|
+
ApiPack.hmac_secret = 'your_secret_key'
|
45
|
+
end
|
46
|
+
```
|
47
|
+
- ApiPack::JsonWebToken.encode({ user_id: user.id }) \
|
48
|
+
returns a valid token with an expiration time of one day by default
|
49
|
+
- To change o expiration call method with parameter exp:
|
50
|
+
```ruby
|
51
|
+
ApiPack::JsonWebToken.encode({ user_id: user.id, exp: 123 })
|
52
|
+
```
|
53
|
+
|
54
|
+
Usage in a service authenticate user
|
55
|
+
|
56
|
+
ApiPack use gem JWT
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
class AuthenticateUser
|
60
|
+
def initialize(email, password)
|
61
|
+
@email = email
|
62
|
+
@password = password
|
63
|
+
end
|
64
|
+
|
65
|
+
def call
|
66
|
+
# return token valid
|
67
|
+
ApiPack::JsonWebToken.encode({ user_id: user.id }) if user
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
attr_accessor :email, :password
|
73
|
+
|
74
|
+
def user
|
75
|
+
user = User.find_by(email: email)
|
76
|
+
return user if user&.authenticate(password)
|
77
|
+
|
78
|
+
# raise AuthenticationError if authenticate fail
|
79
|
+
raise(ApiPack::Errors::Auth::AuthenticationError, 'Invalid credentials')
|
80
|
+
end
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
- ApiPack::JsonWebToken.decode(http_auth_header)
|
85
|
+
|
86
|
+
Usage in a service authorize api request
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
class AuthorizeApiRequest
|
90
|
+
def initialize(headers: {})
|
91
|
+
@headers = headers
|
92
|
+
end
|
93
|
+
|
94
|
+
def call
|
95
|
+
{ user: user }
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
attr_accessor :headers
|
101
|
+
|
102
|
+
def user
|
103
|
+
@user ||= User.find(decoded_auth_token['user_id']) if decoded_auth_token
|
104
|
+
rescue ActiveRecord::RecordNotFound => e
|
105
|
+
# raise InvalidToken if user not found
|
106
|
+
raise ApiPack::Errors::Auth::InvalidToken, ("Invalid token #{e.message}")
|
107
|
+
end
|
108
|
+
|
109
|
+
def decoded_auth_token
|
110
|
+
# decode a token valid
|
111
|
+
@decoded_auth_token ||= ApiPack::JsonWebToken.decode(http_auth_header)
|
112
|
+
end
|
113
|
+
|
114
|
+
def http_auth_header
|
115
|
+
return headers['Authorization'].split(' ').last if headers['Authorization'].present?
|
116
|
+
|
117
|
+
raise(ApiPack::Errors::Auth::MissingToken, 'Missing token')
|
118
|
+
end
|
119
|
+
end
|
120
|
+
```
|
121
|
+
|
122
|
+
### Errors Serializers
|
123
|
+
|
124
|
+
- Errors handled \
|
125
|
+
ActiveRecord::RecordNotFound \
|
126
|
+
ActionController::ParameterMissing \
|
127
|
+
ActiveRecord::RecordInvalid
|
128
|
+
|
129
|
+
- ApiPack::Errors::HandleError.new(e).call
|
130
|
+
|
131
|
+
create an ExceptionHandler concern and deal with api errors in json api format
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
module ExceptionHandler
|
135
|
+
extend ActiveSupport::Concern
|
136
|
+
|
137
|
+
included do
|
138
|
+
rescue_from StandardError do |e|
|
139
|
+
result = ApiPack::Errors::HandleError.new(e).call
|
140
|
+
render json: result[:body], status: result[:status]
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
```
|
145
|
+
|
146
|
+
### Serializer Parser Adapter (BETA)
|
147
|
+
|
148
|
+
This Parser aims to provide adapters for using serializers like FastJsonApi
|
149
|
+
|
150
|
+
Default is FastJsonApi
|
151
|
+
|
152
|
+
By convection the serializers should be in
|
153
|
+
|
154
|
+
serializers/name_serializer/class_serializer
|
155
|
+
|
156
|
+
Ex: serializers/fast_jsonapi/user_serializer.rb
|
157
|
+
|
158
|
+
USAGE:
|
159
|
+
- Create initializer serilializer_parser.rb and put this code
|
160
|
+
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
ApiPack::Serializer::Parser.adapter = :fast_json_api
|
164
|
+
```
|
165
|
+
|
166
|
+
- include ApiPack::ApiHelper in aplication_controler.rb
|
167
|
+
|
168
|
+
```ruby
|
169
|
+
class ApplicationController < ActionController::API
|
170
|
+
include ApiPack::ApiHelper
|
171
|
+
end
|
172
|
+
```
|
173
|
+
- use method serializer_hash to return an hash
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
def index
|
177
|
+
users = User.all
|
178
|
+
|
179
|
+
render json: serializer_hash(users, :user)
|
180
|
+
end
|
181
|
+
```
|
182
|
+
## Pagination Links
|
183
|
+
- pagination_meta_generator \
|
184
|
+
Return an hash with pagination links
|
185
|
+
- Apipack has default per page like 10
|
186
|
+
- To change o defaut_per_page create an initializer api_pack and put this code
|
187
|
+
|
188
|
+
```ruby
|
189
|
+
ApiPack.defaut_per_page = 12
|
190
|
+
```
|
191
|
+
|
192
|
+
```ruby
|
193
|
+
def index
|
194
|
+
# current_page mtehod default is 1
|
195
|
+
# per_page method default is 10
|
196
|
+
users = User.page(current_page).per_page(per_page)
|
197
|
+
|
198
|
+
options = pagination_meta_generator(request, users.total_pages)
|
199
|
+
|
200
|
+
render json: serializer_hash(users, :user, opt: options)
|
201
|
+
end
|
202
|
+
```
|
29
203
|
|
30
|
-
##
|
204
|
+
## This example project uses gem api_apck
|
31
205
|
|
32
|
-
|
206
|
+
https://github.com/Jllima/api_pack_rails
|
33
207
|
|
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
208
|
|
36
209
|
## Contributing
|
37
210
|
|
data/lib/api_pack.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'jwt'
|
2
2
|
require 'fast_jsonapi'
|
3
3
|
require 'api_pack/version'
|
4
|
+
require 'api_pack/api_helper'
|
4
5
|
require 'api_pack/json_web_token'
|
5
6
|
require 'api_pack/pagination_meta_generator'
|
6
7
|
require 'api_pack/errors/auth'
|
@@ -9,25 +10,29 @@ require 'api_pack/errors/handle_error'
|
|
9
10
|
require 'api_pack/errors/api_errors_serializer'
|
10
11
|
require 'api_pack/errors/validation_error_serializer'
|
11
12
|
require 'api_pack/errors/validation_errors_serializer'
|
12
|
-
require 'api_pack/
|
13
|
-
require 'api_pack/support/api_helper'
|
13
|
+
require 'api_pack/support/helper'
|
14
14
|
require 'api_pack/serializer/parser'
|
15
15
|
|
16
16
|
module ApiPack
|
17
17
|
module_function
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
19
|
+
DEFAULT_PAGE = 1
|
20
|
+
DEFAULT_PER_PAGE = 10
|
21
|
+
|
22
|
+
def default_per_page=(value)
|
23
|
+
@default_per_page = value
|
24
|
+
end
|
24
25
|
|
25
26
|
def default_per_page
|
26
|
-
@default_per_page ||=
|
27
|
+
@default_per_page ||= DEFAULT_PER_PAGE
|
28
|
+
end
|
29
|
+
|
30
|
+
def hmac_secret=(value)
|
31
|
+
@hmac_secret = value
|
27
32
|
end
|
28
33
|
|
29
|
-
def
|
30
|
-
@
|
34
|
+
def hmac_secret
|
35
|
+
@hmac_secret
|
31
36
|
end
|
32
37
|
|
33
38
|
def serializer_adapter=(adapter)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ApiPack
|
2
|
+
module ApiHelper
|
3
|
+
def pagination_meta_generator(request, total_pages)
|
4
|
+
ApiPack::PaginationMetaGenerator.new(request: request, total_pages: total_pages).call
|
5
|
+
end
|
6
|
+
|
7
|
+
def current_page
|
8
|
+
return params[:page].to_i if defined?(params[:page]) && !params[:page].nil?
|
9
|
+
|
10
|
+
ApiPack::DEFAULT_PAGE
|
11
|
+
end
|
12
|
+
|
13
|
+
def per_page
|
14
|
+
return params[:per_page].to_i if defined?(params[:per_page]) && !params[:per_page].nil?
|
15
|
+
|
16
|
+
ApiPack::DEFAULT_PER_PAGE
|
17
|
+
end
|
18
|
+
|
19
|
+
def serializer_hash(resource, class_name, opt: {})
|
20
|
+
ApiPack::Serializer::Parser.serializer_hash(resource, class_name, opt: opt)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/api_pack/errors/auth.rb
CHANGED
@@ -24,8 +24,8 @@ module ApiPack
|
|
24
24
|
},
|
25
25
|
'ApiPack::Errors::Auth::InvalidToken' => {
|
26
26
|
method: :error_message_body,
|
27
|
-
title: 'Invalid Token',
|
28
|
-
status: :
|
27
|
+
title: 'Access Denied - Invalid Token',
|
28
|
+
status: :forbidden
|
29
29
|
},
|
30
30
|
'ApiPack::Errors::Auth::MissingToken' => {
|
31
31
|
method: :error_message_body,
|
@@ -1,13 +1,18 @@
|
|
1
1
|
module ApiPack
|
2
2
|
class JsonWebToken
|
3
|
-
|
3
|
+
# exp: 24 hours from now
|
4
|
+
def self.encode(payload, exp: (Time.now + 1 * 86_400).to_i)
|
5
|
+
raise ApiPack::Errors::Auth::MissingHmacSecret if ApiPack.hmac_secret.nil?
|
6
|
+
|
4
7
|
payload[:exp] = exp.to_i
|
5
8
|
|
6
|
-
JWT.encode(payload, ApiPack
|
9
|
+
JWT.encode(payload, ApiPack.hmac_secret)
|
10
|
+
rescue ApiPack::Errors::Auth::MissingHmacSecret
|
11
|
+
raise ApiPack::Errors::Auth::MissingHmacSecret, 'ApiPach.hmac_secret is missing'
|
7
12
|
end
|
8
13
|
|
9
14
|
def self.decode(token)
|
10
|
-
JWT.decode(token, ApiPack
|
15
|
+
JWT.decode(token, ApiPack.hmac_secret).first
|
11
16
|
rescue JWT::DecodeError => e
|
12
17
|
raise ApiPack::Errors::Auth::InvalidToken, e.message
|
13
18
|
end
|
@@ -1,8 +1,8 @@
|
|
1
|
-
require '
|
1
|
+
require 'api_pack/support/helper'
|
2
2
|
|
3
3
|
module ApiPack
|
4
4
|
class PaginationMetaGenerator
|
5
|
-
include Support::
|
5
|
+
include Support::Helper
|
6
6
|
|
7
7
|
def initialize(request:, total_pages:)
|
8
8
|
@url = "#{request.base_url}#{request.path}"
|
@@ -8,6 +8,9 @@ module ApiPack
|
|
8
8
|
name_klass = klass.to_s.split('_').collect(&:capitalize).join
|
9
9
|
|
10
10
|
serializer = "#{name_klass}Serializer"
|
11
|
+
|
12
|
+
require "./app/serializers/fast_jsonapi/#{klass}_serializer"
|
13
|
+
|
11
14
|
FastJsonapi.const_get(serializer).new(resource, opt).serializable_hash
|
12
15
|
end
|
13
16
|
end
|
data/lib/api_pack/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api_pack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jorge
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-03-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -60,7 +60,7 @@ files:
|
|
60
60
|
- bin/console
|
61
61
|
- bin/setup
|
62
62
|
- lib/api_pack.rb
|
63
|
-
- lib/api_pack/
|
63
|
+
- lib/api_pack/api_helper.rb
|
64
64
|
- lib/api_pack/errors/api_errors_serializer.rb
|
65
65
|
- lib/api_pack/errors/auth.rb
|
66
66
|
- lib/api_pack/errors/error_map.rb
|
@@ -71,13 +71,13 @@ files:
|
|
71
71
|
- lib/api_pack/pagination_meta_generator.rb
|
72
72
|
- lib/api_pack/serializer/adapter/fast_json_api.rb
|
73
73
|
- lib/api_pack/serializer/parser.rb
|
74
|
-
- lib/api_pack/support/
|
74
|
+
- lib/api_pack/support/helper.rb
|
75
75
|
- lib/api_pack/version.rb
|
76
76
|
homepage: https://github.com/Jllima/api_pack
|
77
77
|
licenses:
|
78
78
|
- MIT
|
79
79
|
metadata: {}
|
80
|
-
post_install_message:
|
80
|
+
post_install_message:
|
81
81
|
rdoc_options: []
|
82
82
|
require_paths:
|
83
83
|
- lib
|
@@ -92,8 +92,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
92
|
- !ruby/object:Gem::Version
|
93
93
|
version: '0'
|
94
94
|
requirements: []
|
95
|
-
rubygems_version: 3.0.
|
96
|
-
signing_key:
|
95
|
+
rubygems_version: 3.0.9
|
96
|
+
signing_key:
|
97
97
|
specification_version: 4
|
98
98
|
summary: Api requirements
|
99
99
|
test_files: []
|
data/lib/api_pack/constants.rb
DELETED