Sha256: 3b850620f016448c49ca8631f9487560b776779b37f2b7e961b9c728b01b4471

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

# typed: true
module Mangadex
  module Api
    class Context
      extend T::Sig

      @@user = nil
      @@version = nil

      sig { returns(T.nilable(String)) }
      def self.version
        return @@version unless @@version.nil?

        @@version = Mangadex::Api::Version.check_mangadex_version
      end

      sig { returns(T.nilable(Mangadex::Api::User)) }
      def self.user
        @@user&.with_valid_session
      end

      sig { params(user: T.nilable(T.any(Hash, Mangadex::Api::User, Mangadex::User))).void }
      def self.user=(user)
        if user.is_a?(Mangadex::Api::User)
          @@user = user
        elsif user.is_a?(Mangadex::User)
          @@user = Mangadex::Api::User.new(
            user.id,
            data: user,
          )
        elsif user.is_a?(Hash)
          user = user.with_indifferent_access

          @@user = Mangadex::Api::User.new(
            user[:mangadex_user_id],
            session: user[:session],
            refresh: user[:refresh],
          )
        elsif user.nil?
          @@user = nil
        end
      end

      sig { params(user: T.nilable(T.any(Hash, Mangadex::Api::User, Mangadex::User)), block: T.proc.returns(T.untyped)).returns(T.untyped) }
      def self.with_user(user, &block)
        current_user = @@user
        @@user = user
        response = yield
        @@user = current_user
        response
      ensure
        @@user = current_user
      end

      sig { params(block: T.proc.returns(T.untyped)).returns(T.untyped) }
      def self.without_user(&block)
        with_user(nil) do
          yield
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mangadex-5.3.2 lib/mangadex/api/context.rb
mangadex-5.3.1.3 lib/mangadex/api/context.rb
mangadex-5.3.1.2 lib/mangadex/api/context.rb