Class: Mangadex::Api::User
- Extended by:
- T::Sig
- Defined in:
- lib/mangadex/api/user.rb
Constant Summary collapse
- SERIALIZABLE_KEYS =
%i( mangadex_user_id session refresh session_valid_until )
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.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(mangadex_user_id:, session: nil, refresh: nil, data: nil, session_valid_until: nil) ⇒ User
constructor
A new instance of User.
- #persist ⇒ Object
- #refresh_session(&block) ⇒ Object
- #refresh_session!(&block) ⇒ Object
- #session_expired? ⇒ Boolean
- #to_h(except: []) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(mangadex_user_id:, session: nil, refresh: nil, data: nil, session_valid_until: nil) ⇒ User
Returns a new instance of User.
18 19 20 21 22 23 24 25 26 |
# File 'lib/mangadex/api/user.rb', line 18 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
#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 |
Class Method Details
.from_storage(mangadex_user_id) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/mangadex/api/user.rb', line 107 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, ) else nil end if user Mangadex.context.user = user end user end |
Instance Method Details
#persist ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mangadex/api/user.rb', line 77 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_session(&block) ⇒ Object
38 39 40 41 42 |
# File 'lib/mangadex/api/user.rb', line 38 def refresh_session(&block) return true unless session_expired? refresh_session!(&block) end |
#refresh_session!(&block) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/mangadex/api/user.rb', line 54 def refresh_session!(&block) 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') if block_given? yield(self) end true end |
#session_expired? ⇒ Boolean
72 73 74 |
# File 'lib/mangadex/api/user.rb', line 72 def session_expired? @session_valid_until.nil? || @session_valid_until <= Time.now end |
#to_h(except: []) ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/mangadex/api/user.rb', line 95 def to_h(except: []) except = Array(except).map(&:to_sym) keys = SERIALIZABLE_KEYS.reject do |key| except.include?(key) end keys.map do |key| [key, send(key)] end.to_h end |
#valid? ⇒ Boolean
90 91 92 |
# File 'lib/mangadex/api/user.rb', line 90 def valid? !mangadex_user_id.nil? && !mangadex_user_id.strip.empty? end |