api_pack 1.1.1 → 1.3.1
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/Gemfile +0 -1
- data/README.md +18 -43
- data/lib/api_pack.rb +7 -10
- data/lib/api_pack/api_helper.rb +1 -1
- 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/support/undescore.rb +5 -0
- data/lib/api_pack/version.rb +1 -1
- metadata +7 -7
- 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: b271de6e69e7310b83a0e0a4272291207ab1b2b21356e77ced5d78d38c1884af
|
4
|
+
data.tar.gz: 5a7e2ce5a172e446acea51b9c8f046b778942d61a9947307e378122d30873264
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 565ebc2d98a7f6b3aa32e1c9a8735cbbfeaf7b482f4fff4a51078dcc27454942765fc05a90f1eb7183c63245144046e422c4a6216ce942d3a068a4d2fb97c29a
|
7
|
+
data.tar.gz: 35ed85d6f2f209754acba27f472864eb1cf5fb3a16652612ee0ad7886cfc4129d3da7713d0bef7cc94840c2ba296626fa77710c38dbfced631ae478b4f01e59b
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
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
|
@@ -23,7 +24,7 @@ ruby >= 2.4
|
|
23
24
|
Add this line to your application's Gemfile:
|
24
25
|
|
25
26
|
```ruby
|
26
|
-
gem 'api_pack'
|
27
|
+
gem 'api_pack', '~> 1.3.1'
|
27
28
|
```
|
28
29
|
|
29
30
|
And then execute:
|
@@ -37,12 +38,17 @@ Or install it yourself as:
|
|
37
38
|
## Usage
|
38
39
|
|
39
40
|
### JsonWebToken methods
|
40
|
-
|
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
|
+
```
|
41
47
|
- ApiPack::JsonWebToken.encode({ user_id: user.id }) \
|
42
|
-
returns a valid token with an expiration time of one day
|
43
|
-
- To change o expiration
|
48
|
+
returns a valid token with an expiration time of one day by default
|
49
|
+
- To change o expiration call method with parameter exp:
|
44
50
|
```ruby
|
45
|
-
ApiPack.exp
|
51
|
+
ApiPack::JsonWebToken.encode({ user_id: user.id, exp: 123 })
|
46
52
|
```
|
47
53
|
|
48
54
|
Usage in a service authenticate user
|
@@ -137,46 +143,10 @@ module ExceptionHandler
|
|
137
143
|
end
|
138
144
|
```
|
139
145
|
|
140
|
-
### Serializer Parser Adapter (BETA)
|
141
|
-
|
142
|
-
This Parser aims to provide adapters for using serializers like FastJsonApi
|
143
|
-
|
144
|
-
Default is FastJsonApi
|
145
|
-
|
146
|
-
By convection the serializers should be in
|
147
|
-
|
148
|
-
serializers/name_serializer/class_serializer
|
149
|
-
|
150
|
-
Ex: serializers/fast_jsonapi/user_serializer.rb
|
151
|
-
|
152
|
-
USAGE:
|
153
|
-
- Create initializer serilializer_parser.rb and put this code
|
154
|
-
|
155
|
-
|
156
|
-
```ruby
|
157
|
-
ApiPack::Serializer::Parser.adapter = :fast_json_api
|
158
|
-
```
|
159
|
-
|
160
|
-
- include ApiPack::ApiHelper in aplication_controler.rb
|
161
|
-
|
162
|
-
```ruby
|
163
|
-
class ApplicationController < ActionController::API
|
164
|
-
include ApiPack::ApiHelper
|
165
|
-
end
|
166
|
-
```
|
167
|
-
- use method serializer_hash to return an hash
|
168
|
-
|
169
|
-
```ruby
|
170
|
-
def index
|
171
|
-
users = User.all
|
172
|
-
|
173
|
-
render json: serializer_hash(users, :user)
|
174
|
-
end
|
175
|
-
```
|
176
146
|
## Pagination Links
|
177
147
|
- pagination_meta_generator \
|
178
148
|
Return an hash with pagination links
|
179
|
-
- Apipack has default per page like
|
149
|
+
- Apipack has default per page like 5
|
180
150
|
- To change o defaut_per_page create an initializer api_pack and put this code
|
181
151
|
|
182
152
|
```ruby
|
@@ -195,6 +165,11 @@ def index
|
|
195
165
|
end
|
196
166
|
```
|
197
167
|
|
168
|
+
## This example project uses gem api_apck
|
169
|
+
|
170
|
+
https://github.com/Jllima/api_pack_rails
|
171
|
+
|
172
|
+
|
198
173
|
## Contributing
|
199
174
|
|
200
175
|
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/api_pack.
|
data/lib/api_pack.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'jwt'
|
2
|
-
require 'fast_jsonapi'
|
3
2
|
require 'api_pack/version'
|
4
3
|
require 'api_pack/api_helper'
|
5
4
|
require 'api_pack/json_web_token'
|
@@ -10,18 +9,16 @@ require 'api_pack/errors/handle_error'
|
|
10
9
|
require 'api_pack/errors/api_errors_serializer'
|
11
10
|
require 'api_pack/errors/validation_error_serializer'
|
12
11
|
require 'api_pack/errors/validation_errors_serializer'
|
13
|
-
require 'api_pack/constants'
|
14
12
|
require 'api_pack/support/helper'
|
13
|
+
require 'api_pack/support/undescore'
|
15
14
|
require 'api_pack/serializer/parser'
|
16
15
|
|
17
16
|
module ApiPack
|
18
17
|
module_function
|
19
18
|
|
20
19
|
DEFAULT_PAGE = 1
|
21
|
-
DEFAULT_PER_PAGE =
|
22
|
-
|
23
|
-
DEFAULT_EXP = (Time.now + 1 * 86_400).to_i
|
24
|
-
|
20
|
+
DEFAULT_PER_PAGE = 5
|
21
|
+
|
25
22
|
def default_per_page=(value)
|
26
23
|
@default_per_page = value
|
27
24
|
end
|
@@ -30,12 +27,12 @@ module ApiPack
|
|
30
27
|
@default_per_page ||= DEFAULT_PER_PAGE
|
31
28
|
end
|
32
29
|
|
33
|
-
def
|
34
|
-
@
|
30
|
+
def hmac_secret=(value)
|
31
|
+
@hmac_secret = value
|
35
32
|
end
|
36
33
|
|
37
|
-
def
|
38
|
-
@
|
34
|
+
def hmac_secret
|
35
|
+
@hmac_secret
|
39
36
|
end
|
40
37
|
|
41
38
|
def serializer_adapter=(adapter)
|
data/lib/api_pack/api_helper.rb
CHANGED
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
|
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.3.1
|
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-06-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -61,7 +61,6 @@ files:
|
|
61
61
|
- bin/setup
|
62
62
|
- lib/api_pack.rb
|
63
63
|
- lib/api_pack/api_helper.rb
|
64
|
-
- lib/api_pack/constants.rb
|
65
64
|
- lib/api_pack/errors/api_errors_serializer.rb
|
66
65
|
- lib/api_pack/errors/auth.rb
|
67
66
|
- lib/api_pack/errors/error_map.rb
|
@@ -73,12 +72,13 @@ files:
|
|
73
72
|
- lib/api_pack/serializer/adapter/fast_json_api.rb
|
74
73
|
- lib/api_pack/serializer/parser.rb
|
75
74
|
- lib/api_pack/support/helper.rb
|
75
|
+
- lib/api_pack/support/undescore.rb
|
76
76
|
- lib/api_pack/version.rb
|
77
77
|
homepage: https://github.com/Jllima/api_pack
|
78
78
|
licenses:
|
79
79
|
- MIT
|
80
80
|
metadata: {}
|
81
|
-
post_install_message:
|
81
|
+
post_install_message:
|
82
82
|
rdoc_options: []
|
83
83
|
require_paths:
|
84
84
|
- lib
|
@@ -93,8 +93,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
93
|
- !ruby/object:Gem::Version
|
94
94
|
version: '0'
|
95
95
|
requirements: []
|
96
|
-
rubygems_version: 3.
|
97
|
-
signing_key:
|
96
|
+
rubygems_version: 3.2.15
|
97
|
+
signing_key:
|
98
98
|
specification_version: 4
|
99
99
|
summary: Api requirements
|
100
100
|
test_files: []
|
data/lib/api_pack/constants.rb
DELETED