Skip to content

Commit

Permalink
Switched to Zeitwerk as autoloader. (#17)
Browse files Browse the repository at this point in the history
Signed-off-by: Hermann Mayer <[email protected]>
  • Loading branch information
Jack12816 authored Jan 11, 2025
1 parent e6e439a commit 2547e1b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 61 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### next

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

### 2.6.0 (3 January 2025)

Expand Down
3 changes: 2 additions & 1 deletion grape-jwt-authentication.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'grape', '>= 1.0', '< 3.0'
spec.add_dependency 'httparty', '>= 0.21'
spec.add_dependency 'jwt', '~> 2.6'
spec.add_dependency 'keyless', '~> 1.4'
spec.add_dependency 'keyless', '~> 1.6'
spec.add_dependency 'recursive-open-struct', '~> 2.0'
spec.add_dependency 'zeitwerk', '~> 2.6'
end
59 changes: 34 additions & 25 deletions lib/grape/jwt/authentication.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'zeitwerk'
require 'active_support'
require 'active_support/concern'
require 'active_support/configurable'
Expand All @@ -10,10 +11,6 @@
require 'jwt'
require 'keyless'
require 'grape'
require 'grape/jwt/authentication/version'
require 'grape/jwt/authentication/configuration'
require 'grape/jwt/authentication/dependencies'
require 'grape/jwt/authentication/jwt_handler'

module Grape
module Jwt
Expand All @@ -22,32 +19,44 @@ module Authentication
extend ActiveSupport::Concern
include Grape::DSL::API

# Setup a Zeitwerk autoloader instance and configure it
loader = Zeitwerk::Loader.for_gem_extension(Grape::Jwt)

# Finish the auto loader configuration
loader.setup

# Make sure to eager load all SDK constants
loader.eager_load

class << self
attr_writer :configuration
end

# Retrieve the current configuration object.
#
# @return [Configuration]
def self.configuration
@configuration ||= Configuration.new
end
# Include top-level features
include Extensions::Dependencies

# Configure the concern by providing a block which takes
# care of this task. Example:
#
# Grape::Jwt::Authentication.configure do |conf|
# # conf.xyz = [..]
# end
def self.configure
yield(configuration)
configure_dependencies
end
# Retrieve the current configuration object.
#
# @return [Configuration]
def configuration
@configuration ||= Configuration.new
end

# Reset the current configuration with the default one.
def self.reset_configuration!
self.configuration = Configuration.new
configure_dependencies
# Configure the concern by providing a block which takes
# care of this task. Example:
#
# Grape::Jwt::Authentication.configure do |conf|
# # conf.xyz = [..]
# end
def configure
yield(configuration)
configure_dependencies
end

# Reset the current configuration with the default one.
def reset_configuration!
self.configuration = Configuration.new
configure_dependencies
end
end

included do
Expand Down
34 changes: 0 additions & 34 deletions lib/grape/jwt/authentication/dependencies.rb

This file was deleted.

38 changes: 38 additions & 0 deletions lib/grape/jwt/authentication/extensions/dependencies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

module Grape
module Jwt
module Authentication
module Extensions
# Root-level handling of dependencies.
module Dependencies
# Specifies which configuration keys are shared between keyless
# and grape-jwt-authentication, so that we can easily pass through
# our configuration to keyless.
KEYLESS_CONFIGURATION = %i[
authenticator rsa_public_key_url rsa_public_key_caching
rsa_public_key_expiration jwt_issuer jwt_beholder jwt_options
jwt_verification_key
].freeze

# (Re)configure our gem dependencies. We take care of setting up
# +Keyless+, which has been extracted from this gem.
def configure_dependencies
configure_keyless
end

# Configure the +Keyless+ gem with our configuration.
def configure_keyless
configuration = Grape::Jwt::Authentication.configuration

Keyless.configure do |keyless|
KEYLESS_CONFIGURATION.each do |option|
keyless.send("#{option}=", configuration.send(option))
end
end
end
end
end
end
end
end

0 comments on commit 2547e1b

Please sign in to comment.