api_pack 1.0.1 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +168 -6
- data/lib/api_pack.rb +12 -4
- data/lib/api_pack/api_helper.rb +23 -0
- data/lib/api_pack/pagination_meta_generator.rb +2 -2
- data/lib/api_pack/support/{api_helper.rb → helper.rb} +1 -1
- data/lib/api_pack/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1108b79f1495ff944ba4298731ec0c68e030d73d6ecbd031120d312c14620759
|
4
|
+
data.tar.gz: e56c3cbe722219a0c79307bc964f265aff47fcb6f2f63a46fc211752bb7363cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 77a5de40fb986ba63ee7b44be5d310ddda4660518843ee1ff9afa3ec2dda22f70e2b0916f2197496b4b1e7b4964860569882866042f6da57e28df6382d217048
|
7
|
+
data.tar.gz: 5ac7a7d1b8ca6ce1d6069ca1bc3bbcd390e02107678c526835d0da88bbecaad9831610f1caf0f610206a99987676ccbc21f3e65b60d4ebf0504ea442df548620
|
data/README.md
CHANGED
@@ -3,9 +3,20 @@
|
|
3
3
|
|
4
4
|
# ApiPack
|
5
5
|
|
6
|
-
|
6
|
+
ApiPack is a set of helpers to Api rails
|
7
7
|
|
8
|
-
|
8
|
+
**This project provides the following helpers**
|
9
|
+
|
10
|
+
- Json Web Token helpers
|
11
|
+
- Json Api especifications
|
12
|
+
- Errors Serializers
|
13
|
+
- Pagination links Serializers
|
14
|
+
- Serializer Adapter (BETA)
|
15
|
+
|
16
|
+
## Compatibility
|
17
|
+
```
|
18
|
+
ruby >= 2.4
|
19
|
+
```
|
9
20
|
|
10
21
|
## Installation
|
11
22
|
|
@@ -25,13 +36,164 @@ Or install it yourself as:
|
|
25
36
|
|
26
37
|
## Usage
|
27
38
|
|
28
|
-
|
39
|
+
### JsonWebToken methods
|
40
|
+
|
41
|
+
- ApiPack::JsonWebToken.encode({ user_id: user.id }) \
|
42
|
+
returns a valid token with an expiration time of one day
|
43
|
+
- To change o expiration create an initializer api_pack and put this code
|
44
|
+
```ruby
|
45
|
+
ApiPack.exp = 12345
|
46
|
+
```
|
47
|
+
|
48
|
+
Usage in a service authenticate user
|
49
|
+
|
50
|
+
ApiPack use gem JWT
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
class AuthenticateUser
|
54
|
+
def initialize(email, password)
|
55
|
+
@email = email
|
56
|
+
@password = password
|
57
|
+
end
|
58
|
+
|
59
|
+
def call
|
60
|
+
# return token valid
|
61
|
+
ApiPack::JsonWebToken.encode({ user_id: user.id }) if user
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
attr_accessor :email, :password
|
67
|
+
|
68
|
+
def user
|
69
|
+
user = User.find_by(email: email)
|
70
|
+
return user if user&.authenticate(password)
|
71
|
+
|
72
|
+
# raise AuthenticationError if authenticate fail
|
73
|
+
raise(ApiPack::Errors::Auth::AuthenticationError, 'Invalid credentials')
|
74
|
+
end
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
- ApiPack::JsonWebToken.decode(http_auth_header)
|
79
|
+
|
80
|
+
Usage in a service authorize api request
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
class AuthorizeApiRequest
|
84
|
+
def initialize(headers: {})
|
85
|
+
@headers = headers
|
86
|
+
end
|
87
|
+
|
88
|
+
def call
|
89
|
+
{ user: user }
|
90
|
+
end
|
91
|
+
|
92
|
+
private
|
93
|
+
|
94
|
+
attr_accessor :headers
|
95
|
+
|
96
|
+
def user
|
97
|
+
@user ||= User.find(decoded_auth_token['user_id']) if decoded_auth_token
|
98
|
+
rescue ActiveRecord::RecordNotFound => e
|
99
|
+
# raise InvalidToken if user not found
|
100
|
+
raise ApiPack::Errors::Auth::InvalidToken, ("Invalid token #{e.message}")
|
101
|
+
end
|
102
|
+
|
103
|
+
def decoded_auth_token
|
104
|
+
# decode a token valid
|
105
|
+
@decoded_auth_token ||= ApiPack::JsonWebToken.decode(http_auth_header)
|
106
|
+
end
|
107
|
+
|
108
|
+
def http_auth_header
|
109
|
+
return headers['Authorization'].split(' ').last if headers['Authorization'].present?
|
110
|
+
|
111
|
+
raise(ApiPack::Errors::Auth::MissingToken, 'Missing token')
|
112
|
+
end
|
113
|
+
end
|
114
|
+
```
|
115
|
+
|
116
|
+
### Errors Serializers
|
117
|
+
|
118
|
+
- Errors handled \
|
119
|
+
ActiveRecord::RecordNotFound \
|
120
|
+
ActionController::ParameterMissing \
|
121
|
+
ActiveRecord::RecordInvalid
|
122
|
+
|
123
|
+
- ApiPack::Errors::HandleError.new(e).call
|
29
124
|
|
30
|
-
|
125
|
+
create an ExceptionHandler concern and deal with api errors in json api format
|
31
126
|
|
32
|
-
|
127
|
+
```ruby
|
128
|
+
module ExceptionHandler
|
129
|
+
extend ActiveSupport::Concern
|
130
|
+
|
131
|
+
included do
|
132
|
+
rescue_from StandardError do |e|
|
133
|
+
result = ApiPack::Errors::HandleError.new(e).call
|
134
|
+
render json: result[:body], status: result[:status]
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
```
|
139
|
+
|
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
|
33
172
|
|
34
|
-
|
173
|
+
render json: serializer_hash(users, :user)
|
174
|
+
end
|
175
|
+
```
|
176
|
+
## Pagination Links
|
177
|
+
- pagination_meta_generator \
|
178
|
+
Return an hash with pagination links
|
179
|
+
- Apipack has default per page like 10
|
180
|
+
- To change o defaut_per_page create an initializer api_pack and put this code
|
181
|
+
|
182
|
+
```ruby
|
183
|
+
ApiPack.defaut_per_page = 12
|
184
|
+
```
|
185
|
+
|
186
|
+
```ruby
|
187
|
+
def index
|
188
|
+
# current_page mtehod default is 1
|
189
|
+
# per_page method default is 10
|
190
|
+
users = User.page(current_page).per_page(per_page)
|
191
|
+
|
192
|
+
options = pagination_meta_generator(request, users.total_pages)
|
193
|
+
|
194
|
+
render json: serializer_hash(users, :user, opt: options)
|
195
|
+
end
|
196
|
+
```
|
35
197
|
|
36
198
|
## Contributing
|
37
199
|
|
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'
|
@@ -10,20 +11,27 @@ 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
13
|
require 'api_pack/constants'
|
13
|
-
require 'api_pack/support/
|
14
|
+
require 'api_pack/support/helper'
|
14
15
|
require 'api_pack/serializer/parser'
|
15
16
|
|
16
17
|
module ApiPack
|
17
18
|
module_function
|
18
19
|
|
19
|
-
|
20
|
+
DEFAULT_PAGE = 1
|
21
|
+
DEFAULT_PER_PAGE = 10
|
20
22
|
# 24 hours from now
|
21
23
|
DEFAULT_EXP = (Time.now + 1 * 86_400).to_i
|
22
24
|
|
23
|
-
|
25
|
+
def default_per_page=(value)
|
26
|
+
@default_per_page = value
|
27
|
+
end
|
24
28
|
|
25
29
|
def default_per_page
|
26
|
-
@default_per_page ||=
|
30
|
+
@default_per_page ||= DEFAULT_PER_PAGE
|
31
|
+
end
|
32
|
+
|
33
|
+
def exp=(value)
|
34
|
+
@exp = value
|
27
35
|
end
|
28
36
|
|
29
37
|
def exp
|
@@ -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
|
@@ -1,8 +1,8 @@
|
|
1
|
-
require 'api_pack/support/
|
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}"
|
data/lib/api_pack/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jorge
|
@@ -60,6 +60,7 @@ files:
|
|
60
60
|
- bin/console
|
61
61
|
- bin/setup
|
62
62
|
- lib/api_pack.rb
|
63
|
+
- lib/api_pack/api_helper.rb
|
63
64
|
- lib/api_pack/constants.rb
|
64
65
|
- lib/api_pack/errors/api_errors_serializer.rb
|
65
66
|
- lib/api_pack/errors/auth.rb
|
@@ -71,7 +72,7 @@ files:
|
|
71
72
|
- lib/api_pack/pagination_meta_generator.rb
|
72
73
|
- lib/api_pack/serializer/adapter/fast_json_api.rb
|
73
74
|
- lib/api_pack/serializer/parser.rb
|
74
|
-
- lib/api_pack/support/
|
75
|
+
- lib/api_pack/support/helper.rb
|
75
76
|
- lib/api_pack/version.rb
|
76
77
|
homepage: https://github.com/Jllima/api_pack
|
77
78
|
licenses:
|