Sha256: f231ef90760ec10fa1fc423aca869c06969a0cf96d34ac370bacf0f682e1af48

Contents?: true

Size: 1.19 KB

Versions: 3

Compression:

Stored size: 1.19 KB

Contents

# typed: true
module Mangadex
  module Api
    class Context
      @@user = nil

      class << self
        def user
          @@user&.with_valid_session
        end

        def 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
          else
            raise ArgumentError, "Must be an instance of #{Mangadex::Api::User}, #{Mangadex::User} or Hash"
          end
        end

        def with_user(user)
          current_user = @@user
          @@user = user
          response = yield
          @@user = current_user
          response
        ensure
          @@user = current_user
        end

        def without_user
          with_user(nil) do
            yield
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mangadex-5.3.1.1 lib/mangadex/api/context.rb
mangadex-5.3.1 lib/mangadex/api/context.rb
mangadex-5.3.0 lib/mangadex/api/context.rb