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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1108b79f1495ff944ba4298731ec0c68e030d73d6ecbd031120d312c14620759
4
- data.tar.gz: e56c3cbe722219a0c79307bc964f265aff47fcb6f2f63a46fc211752bb7363cb
3
+ metadata.gz: b271de6e69e7310b83a0e0a4272291207ab1b2b21356e77ced5d78d38c1884af
4
+ data.tar.gz: 5a7e2ce5a172e446acea51b9c8f046b778942d61a9947307e378122d30873264
5
5
  SHA512:
6
- metadata.gz: 77a5de40fb986ba63ee7b44be5d310ddda4660518843ee1ff9afa3ec2dda22f70e2b0916f2197496b4b1e7b4964860569882866042f6da57e28df6382d217048
7
- data.tar.gz: 5ac7a7d1b8ca6ce1d6069ca1bc3bbcd390e02107678c526835d0da88bbecaad9831610f1caf0f610206a99987676ccbc21f3e65b60d4ebf0504ea442df548620
6
+ metadata.gz: 565ebc2d98a7f6b3aa32e1c9a8735cbbfeaf7b482f4fff4a51078dcc27454942765fc05a90f1eb7183c63245144046e422c4a6216ce942d3a068a4d2fb97c29a
7
+ data.tar.gz: 35ed85d6f2f209754acba27f472864eb1cf5fb3a16652612ee0ad7886cfc4129d3da7713d0bef7cc94840c2ba296626fa77710c38dbfced631ae478b4f01e59b
data/.gitignore CHANGED
@@ -8,6 +8,7 @@
8
8
  /tmp/
9
9
  Gemfile.lock
10
10
  .DS_store
11
+ api_pack-*
11
12
 
12
13
  # rspec failure tracking
13
14
  .rspec_status
data/Gemfile CHANGED
@@ -4,7 +4,6 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  gemspec
6
6
 
7
- gem 'fast_jsonapi'
8
7
  gem 'jwt'
9
8
 
10
9
  group :test do
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
- [![Build Status](https://travis-ci.org/Jllima/api_pack.svg?branch=master)](https://travis-ci.org/Jllima/api_pack)
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 create an initializer api_pack and put this code
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 = 12345
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 10
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 = 10
22
- # 24 hours from now
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 exp=(value)
34
- @exp = value
30
+ def hmac_secret=(value)
31
+ @hmac_secret = value
35
32
  end
36
33
 
37
- def exp
38
- @exp ||= DEFAULT_EXP
34
+ def hmac_secret
35
+ @hmac_secret
39
36
  end
40
37
 
41
38
  def serializer_adapter=(adapter)
@@ -13,7 +13,7 @@ module ApiPack
13
13
  def per_page
14
14
  return params[:per_page].to_i if defined?(params[:per_page]) && !params[:per_page].nil?
15
15
 
16
- ApiPack::DEFAULT_PER_PAGE
16
+ ApiPack.default_per_page
17
17
  end
18
18
 
19
19
  def serializer_hash(resource, class_name, opt: {})
@@ -4,6 +4,7 @@ module ApiPack
4
4
  class AuthenticationError < StandardError; end
5
5
  class InvalidToken < StandardError; end
6
6
  class MissingToken < StandardError; end
7
+ class MissingHmacSecret < StandardError; end
7
8
  end
8
9
  end
9
10
  end
@@ -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: :unprocessable_entity
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
- def self.encode(payload, exp: ApiPack.exp)
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::HMAC_SECRET)
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::HMAC_SECRET).first
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
@@ -0,0 +1,5 @@
1
+ class String
2
+ def underscore
3
+ gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').tr("-", "_").downcase
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module ApiPack
2
- VERSION = '1.1.1'.freeze
2
+ VERSION = '1.3.1'.freeze
3
3
  end
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.1.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: 2020-10-20 00:00:00.000000000 Z
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.0.8
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: []
@@ -1,3 +0,0 @@
1
- module ApiPack
2
- HMAC_SECRET = '2e484b352b985259601a5f2f2c1712b31d63136bf22d9213303a5b501942723827508038484b9da1ead77a7788799c55e05785f2f148e53d943e6dedf635a291'.freeze
3
- end