Skip to content

Commit 2547e1b

Browse files
authored
Switched to Zeitwerk as autoloader. (#17)
Signed-off-by: Hermann Mayer <[email protected]>
1 parent e6e439a commit 2547e1b

File tree

5 files changed

+75
-61
lines changed

5 files changed

+75
-61
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
### next
22

3-
* TODO: Replace this bullet point with an actual description of a change.
3+
* Switched to Zeitwerk as autoloader (#17)
44

55
### 2.6.0 (3 January 2025)
66

grape-jwt-authentication.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Gem::Specification.new do |spec|
3737
spec.add_dependency 'grape', '>= 1.0', '< 3.0'
3838
spec.add_dependency 'httparty', '>= 0.21'
3939
spec.add_dependency 'jwt', '~> 2.6'
40-
spec.add_dependency 'keyless', '~> 1.4'
40+
spec.add_dependency 'keyless', '~> 1.6'
4141
spec.add_dependency 'recursive-open-struct', '~> 2.0'
42+
spec.add_dependency 'zeitwerk', '~> 2.6'
4243
end

lib/grape/jwt/authentication.rb

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
require 'zeitwerk'
34
require 'active_support'
45
require 'active_support/concern'
56
require 'active_support/configurable'
@@ -10,10 +11,6 @@
1011
require 'jwt'
1112
require 'keyless'
1213
require 'grape'
13-
require 'grape/jwt/authentication/version'
14-
require 'grape/jwt/authentication/configuration'
15-
require 'grape/jwt/authentication/dependencies'
16-
require 'grape/jwt/authentication/jwt_handler'
1714

1815
module Grape
1916
module Jwt
@@ -22,32 +19,44 @@ module Authentication
2219
extend ActiveSupport::Concern
2320
include Grape::DSL::API
2421

22+
# Setup a Zeitwerk autoloader instance and configure it
23+
loader = Zeitwerk::Loader.for_gem_extension(Grape::Jwt)
24+
25+
# Finish the auto loader configuration
26+
loader.setup
27+
28+
# Make sure to eager load all SDK constants
29+
loader.eager_load
30+
2531
class << self
2632
attr_writer :configuration
27-
end
2833

29-
# Retrieve the current configuration object.
30-
#
31-
# @return [Configuration]
32-
def self.configuration
33-
@configuration ||= Configuration.new
34-
end
34+
# Include top-level features
35+
include Extensions::Dependencies
3536

36-
# Configure the concern by providing a block which takes
37-
# care of this task. Example:
38-
#
39-
# Grape::Jwt::Authentication.configure do |conf|
40-
# # conf.xyz = [..]
41-
# end
42-
def self.configure
43-
yield(configuration)
44-
configure_dependencies
45-
end
37+
# Retrieve the current configuration object.
38+
#
39+
# @return [Configuration]
40+
def configuration
41+
@configuration ||= Configuration.new
42+
end
4643

47-
# Reset the current configuration with the default one.
48-
def self.reset_configuration!
49-
self.configuration = Configuration.new
50-
configure_dependencies
44+
# Configure the concern by providing a block which takes
45+
# care of this task. Example:
46+
#
47+
# Grape::Jwt::Authentication.configure do |conf|
48+
# # conf.xyz = [..]
49+
# end
50+
def configure
51+
yield(configuration)
52+
configure_dependencies
53+
end
54+
55+
# Reset the current configuration with the default one.
56+
def reset_configuration!
57+
self.configuration = Configuration.new
58+
configure_dependencies
59+
end
5160
end
5261

5362
included do

lib/grape/jwt/authentication/dependencies.rb

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
module Grape
4+
module Jwt
5+
module Authentication
6+
module Extensions
7+
# Root-level handling of dependencies.
8+
module Dependencies
9+
# Specifies which configuration keys are shared between keyless
10+
# and grape-jwt-authentication, so that we can easily pass through
11+
# our configuration to keyless.
12+
KEYLESS_CONFIGURATION = %i[
13+
authenticator rsa_public_key_url rsa_public_key_caching
14+
rsa_public_key_expiration jwt_issuer jwt_beholder jwt_options
15+
jwt_verification_key
16+
].freeze
17+
18+
# (Re)configure our gem dependencies. We take care of setting up
19+
# +Keyless+, which has been extracted from this gem.
20+
def configure_dependencies
21+
configure_keyless
22+
end
23+
24+
# Configure the +Keyless+ gem with our configuration.
25+
def configure_keyless
26+
configuration = Grape::Jwt::Authentication.configuration
27+
28+
Keyless.configure do |keyless|
29+
KEYLESS_CONFIGURATION.each do |option|
30+
keyless.send("#{option}=", configuration.send(option))
31+
end
32+
end
33+
end
34+
end
35+
end
36+
end
37+
end
38+
end

0 commit comments

Comments
 (0)