Class: Mangadex::Api::User
- Inherits:
-
Object
- Object
- Mangadex::Api::User
- Extended by:
- T::Sig
- Defined in:
- lib/mangadex/api/user.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#mangadex_user_id ⇒ Object
Returns the value of attribute mangadex_user_id.
-
#refresh ⇒ Object
Returns the value of attribute refresh.
-
#session ⇒ Object
Returns the value of attribute session.
-
#session_valid_until ⇒ Object
Returns the value of attribute session_valid_until.
Instance Method Summary collapse
-
#initialize(mangadex_user_id, session: nil, refresh: nil, data: nil) ⇒ User
constructor
A new instance of User.
- #refresh! ⇒ Object
- #session_expired? ⇒ Boolean
- #with_valid_session ⇒ Object
Constructor Details
#initialize(mangadex_user_id, session: nil, refresh: nil, data: nil) ⇒ User
Returns a new instance of User.
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) 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 ? Time.now + (14 * 60) : nil @refresh = refresh @data = data end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
8 9 10 |
# File 'lib/mangadex/api/user.rb', line 8 def data @data end |
#mangadex_user_id ⇒ Object
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 |
#refresh ⇒ Object
Returns the value of attribute refresh.
7 8 9 |
# File 'lib/mangadex/api/user.rb', line 7 def refresh @refresh end |
#session ⇒ Object
Returns the value of attribute session.
7 8 9 |
# File 'lib/mangadex/api/user.rb', line 7 def session @session end |
#session_valid_until ⇒ Object
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 |
Instance Method Details
#refresh! ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/mangadex/api/user.rb', line 24 def refresh! return false if refresh.nil? response = Mangadex::Api::Context.without_user do Mangadex::Internal::Request.post('/auth/refresh', payload: { token: refresh }) end 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
48 49 50 |
# File 'lib/mangadex/api/user.rb', line 48 def session_expired? @session_valid_until.nil? || @session_valid_until <= Time.now end |
#with_valid_session ⇒ Object
40 41 42 43 44 45 |
# File 'lib/mangadex/api/user.rb', line 40 def with_valid_session session_expired? && refresh! self ensure self end |