<commit_before># frozen_string_literal: true

module Devise
  module JWT
    # Inspect and extract information from a Devise mapping
    class MappingInspector
      attr_reader :scope, :mapping

      def initialize(scope)
        @scope = scope
        @mapping = Devise.mappings[scope]
      end

      def jwt?
        mapping.modules.member?(:jwt_authenticatable)
      end

      def session?
        routes?(:session)
      end

      def registration?
        routes?(:registration)
      end

      def model
        mapping.to
      end

      # :reek:FeatureEnvy
      def path(name)
        prefix, scope, request = path_parts(name)
        [prefix, scope, request].delete_if do |item|
          !item || item.empty?
        end.join('/').prepend('/')
      end

      # :reek:ControlParameter
      def method(name)
        case name
        when :sign_in
          'POST'
        when :sign_out
          sign_out_via.to_s.upcase
        when :registration
          'POST'
        end
      end

      def formats
        JWT.config.request_formats[scope] || default_formats
      end

      private

      def path_parts(name)
        prefix = mapping.instance_variable_get(:@path_prefix)
        path = mapping.path
        path_name = mapping.path_names[name]
        [
          prefix && prefix.gsub(%r{^/}, ''),
          path,
          path_name && !path_name.empty? ? path_name : nil
        ]
      end

      def routes?(name)
        mapping.routes.member?(name)
      end

      def sign_out_via
        mapping.sign_out_via.to_s.upcase
      end<commit_msg>Do path transformations in one place<commit_after># frozen_string_literal: true

module Devise
  module JWT
    # Inspect and extract information from a Devise mapping
    class MappingInspector
      attr_reader :scope, :mapping

      def initialize(scope)
        @scope = scope
        @mapping = Devise.mappings[scope]
      end

      def jwt?
        mapping.modules.member?(:jwt_authenticatable)
      end

      def session?
        routes?(:session)
      end

      def registration?
        routes?(:registration)
      end

      def model
        mapping.to
      end

      # :reek:FeatureEnvy
      def path(name)
        prefix, scope, request = path_parts(name)
        [prefix, scope, request].delete_if do |item|
          !item || item.empty?
        end.join('/').prepend('/').gsub('//', '/')
      end

      # :reek:ControlParameter
      def method(name)
        case name
        when :sign_in
          'POST'
        when :sign_out
          sign_out_via.to_s.upcase
        when :registration
          'POST'
        end
      end

      def formats
        JWT.config.request_formats[scope] || default_formats
      end

      private

      def path_parts(name)
        prefix = mapping.instance_variable_get(:@path_prefix)
        path = mapping.path
        path_name = mapping.path_names[name]
        [prefix, path, path_name]
      end

      def routes?(name)
        mapping.routes.member?(name)
      end

      def sign_out_via
        mapping.sign_out_via.to_s.upcase
      end
{'fraction_non_alphanumeric': 0.08663446054750402, 'fraction_numerical': 0.0, 'mean_word_length': 1.8159564823209429, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7131713', 'n_tokens_mistral': 928, 'n_tokens_neox': 905, 'n_words': 210}