Class: Mangadex::Api::User
- Inherits:
-
Object
- Object
- Mangadex::Api::User
- 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
nil: Nothing happened, no need to refresh the token true: The tokens were successfully refreshed false: Error: refresh token empty or could not refresh the token on the server.
- #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.
8 9 10 11 12 13 14 15 16 |
# File 'lib/mangadex/api/user.rb', line 8 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.
6 7 8 |
# File 'lib/mangadex/api/user.rb', line 6 def data @data end |
#mangadex_user_id ⇒ Object
Returns the value of attribute mangadex_user_id.
5 6 7 |
# File 'lib/mangadex/api/user.rb', line 5 def mangadex_user_id @mangadex_user_id end |
#refresh ⇒ Object
Returns the value of attribute refresh.
5 6 7 |
# File 'lib/mangadex/api/user.rb', line 5 def refresh @refresh end |
#session ⇒ Object
Returns the value of attribute session.
5 6 7 |
# File 'lib/mangadex/api/user.rb', line 5 def session @session end |
#session_valid_until ⇒ Object
Returns the value of attribute session_valid_until.
5 6 7 |
# File 'lib/mangadex/api/user.rb', line 5 def session_valid_until @session_valid_until end |
Instance Method Details
#refresh! ⇒ Object
nil: Nothing happened, no need to refresh the token true: The tokens were successfully refreshed false: Error: refresh token empty or could not refresh the token on the server
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/mangadex/api/user.rb', line 21 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
43 44 45 |
# File 'lib/mangadex/api/user.rb', line 43 def session_expired? @session_valid_until.nil? || @session_valid_until <= Time.now end |
#with_valid_session ⇒ Object
36 37 38 39 40 41 |
# File 'lib/mangadex/api/user.rb', line 36 def with_valid_session session_expired? && refresh! self ensure self end |