Class: Mangadex::Api::User

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/mangadex/api/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mangadex_user_id:, session: nil, refresh: nil, data: nil, session_valid_until: nil) ⇒ User

Returns a new instance of User.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
# File 'lib/mangadex/api/user.rb', line 11

def initialize(mangadex_user_id:, session: nil, refresh: nil, data: nil, session_valid_until: nil)
  raise ArgumentError, 'Missing mangadex_user_id' if mangadex_user_id.to_s.empty?

  @mangadex_user_id = mangadex_user_id
  @session = session
  @session_valid_until = session_valid_until ? session_valid_until : (session ? Time.now + (14 * 60) : nil)
  @refresh = refresh
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#mangadex_user_idObject

Returns the value of attribute mangadex_user_id.



7
8
9
# File 'lib/mangadex/api/user.rb', line 7

def mangadex_user_id
  @mangadex_user_id
end

#refreshObject

Returns the value of attribute refresh.



7
8
9
# File 'lib/mangadex/api/user.rb', line 7

def refresh
  @refresh
end

#sessionObject

Returns the value of attribute session.



7
8
9
# File 'lib/mangadex/api/user.rb', line 7

def session
  @session
end

#session_valid_untilObject

Returns the value of attribute session_valid_until.



7
8
9
# File 'lib/mangadex/api/user.rb', line 7

def session_valid_until
  @session_valid_until
end

Class Method Details

.from_storage(mangadex_user_id) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mangadex/api/user.rb', line 69

def self.from_storage(mangadex_user_id)
  return if mangadex_user_id.nil?

  session = Mangadex.storage.get(mangadex_user_id, 'session')
  refresh = Mangadex.storage.get(mangadex_user_id, 'refresh')
  session_valid_until = Mangadex.storage.get(mangadex_user_id, 'session_valid_until')

  user = if session || refresh || session_valid_until
    session_valid_until = session_valid_until ? Time.parse(session_valid_until) : nil

    new(
      mangadex_user_id: mangadex_user_id,
      session: session,
      refresh: refresh,
      session_valid_until: session_valid_until,
    ).with_valid_session
  else
    nil
  end

  if user
    Mangadex.context.user = user
  end

  user
end

Instance Method Details

#persistObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mangadex/api/user.rb', line 51

def persist
  return false unless valid?

  Mangadex.storage.set(mangadex_user_id, 'session', session) if session
  Mangadex.storage.set(mangadex_user_id, 'refresh', refresh) if refresh
  if session_valid_until
    Mangadex.storage.set(mangadex_user_id, 'session_valid_until', session_valid_until.to_s)
  end

  true
end

#refresh!Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mangadex/api/user.rb', line 24

def refresh!
  return false if refresh.nil?

  response = Mangadex::Internal::Request.post('/auth/refresh', payload: { token: refresh })
  return false unless response['token']

  @session_valid_until = Time.now + (14 * 60)
  @refresh = response.dig('token', 'refresh')
  @session = response.dig('token', 'session')

  true
end

#session_expired?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/mangadex/api/user.rb', line 46

def session_expired?
  @session_valid_until.nil? || @session_valid_until <= Time.now
end

#valid?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/mangadex/api/user.rb', line 64

def valid?
  !mangadex_user_id.nil? && !mangadex_user_id.strip.empty?
end

#with_valid_sessionObject



38
39
40
41
42
43
# File 'lib/mangadex/api/user.rb', line 38

def with_valid_session
  session_expired? && refresh!
  self
ensure
  self
end