Class: Mangadex::Api::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/mangadex/api/context.rb

Constant Summary collapse

@@user =
nil

Class Method Summary collapse

Class Method Details

.userObject



8
9
10
# File 'lib/mangadex/api/context.rb', line 8

def user
  @@user&.with_valid_session
end

.user=(user) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mangadex/api/context.rb', line 12

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

.with_user(user) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/mangadex/api/context.rb', line 35

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

.without_userObject



45
46
47
48
49
# File 'lib/mangadex/api/context.rb', line 45

def without_user
  with_user(nil) do
    yield
  end
end