Sha256: 1169ff62f16a94a59b4ab646f8af68aede6f3266b0ae27903f2b11700b567831

Contents?: true

Size: 1.94 KB

Versions: 31

Compression:

Stored size: 1.94 KB

Contents

# typed: strict
# frozen_string_literal: true

module ShopifyAPI
  module Auth
    class AuthScopes
      extend T::Sig

      SCOPE_DELIMITER = ","

      sig { params(scope_names: T.any(String, T::Array[String])).void }
      def initialize(scope_names = [])
        @compressed_scopes = T.let([].to_set, T::Set[String])
        @expanded_scopes = T.let([].to_set, T::Set[String])

        if scope_names.is_a?(String)
          scope_names = scope_names.to_s.split(SCOPE_DELIMITER)
        end

        store_scopes(scope_names)
      end

      sig { params(auth_scopes: AuthScopes).returns(T::Boolean) }
      def covers?(auth_scopes)
        auth_scopes.compressed_scopes <= expanded_scopes
      end

      sig { returns(String) }
      def to_s
        to_a.join(SCOPE_DELIMITER)
      end

      sig { returns(T::Array[String]) }
      def to_a
        compressed_scopes.to_a
      end

      sig { params(other: T.nilable(AuthScopes)).returns(T::Boolean) }
      def ==(other)
        !other.nil? &&
          other.class == self.class &&
          compressed_scopes == other.compressed_scopes
      end

      alias_method :eql?, :==

      sig { returns(Integer) }
      def hash
        compressed_scopes.hash
      end

      protected

      sig { returns(T::Set[String]) }
      attr_reader :compressed_scopes, :expanded_scopes

      private

      sig { params(scope_names: T::Array[String]).void }
      def store_scopes(scope_names)
        scopes = scope_names.map(&:strip).reject(&:empty?).to_set
        implied_scopes = scopes.map { |scope| implied_scope(scope) }.compact

        @compressed_scopes = scopes - implied_scopes
        @expanded_scopes = scopes + implied_scopes
      end

      sig { params(scope: String).returns(T.nilable(String)) }
      def implied_scope(scope)
        is_write_scope = scope =~ /\A(unauthenticated_)?write_(.*)\z/
        "#{Regexp.last_match(1)}read_#{Regexp.last_match(2)}" if is_write_scope
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
shopify_api-14.8.0 lib/shopify_api/auth/auth_scopes.rb
shopify_api-14.7.0 lib/shopify_api/auth/auth_scopes.rb
shopify_api-14.6.0 lib/shopify_api/auth/auth_scopes.rb
shopify_api-14.5.0 lib/shopify_api/auth/auth_scopes.rb
shopify_api-14.4.0 lib/shopify_api/auth/auth_scopes.rb
shopify_api-14.3.0 lib/shopify_api/auth/auth_scopes.rb
shopify_api-14.2.0 lib/shopify_api/auth/auth_scopes.rb
shopify_api-14.1.0 lib/shopify_api/auth/auth_scopes.rb
shopify_api-14.0.1 lib/shopify_api/auth/auth_scopes.rb
shopify_api-14.0.0 lib/shopify_api/auth/auth_scopes.rb
shopify_api-13.4.0 lib/shopify_api/auth/auth_scopes.rb
shopify_api-13.3.1 lib/shopify_api/auth/auth_scopes.rb
shopify_api-13.3.0 lib/shopify_api/auth/auth_scopes.rb
shopify_api-13.2.0 lib/shopify_api/auth/auth_scopes.rb
shopify_api-13.1.0 lib/shopify_api/auth/auth_scopes.rb
shopify_api-13.0.0 lib/shopify_api/auth/auth_scopes.rb
shopify_api-12.5.0 lib/shopify_api/auth/auth_scopes.rb
shopify_api-12.4.0 lib/shopify_api/auth/auth_scopes.rb
shopify_api-12.3.0 lib/shopify_api/auth/auth_scopes.rb
shopify_api-12.2.1 lib/shopify_api/auth/auth_scopes.rb