class Mangadex::Api::User
Constants
- SERIALIZABLE_KEYS
Attributes
data[R]
mangadex_user_id[RW]
refresh[RW]
session[RW]
session_valid_until[RW]
Public Class Methods
from_storage(mangadex_user_id)
click to toggle source
# 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
new(mangadex_user_id:, session: nil, refresh: nil, data: nil, session_valid_until: nil)
click to toggle source
# 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
Public Instance Methods
persist()
click to toggle source
# 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)
click to toggle source
# File lib/mangadex/api/user.rb, line 38 def refresh_session(&block) return true unless session_expired? refresh_session!(&block) end
refresh_session!() { |self| ... }
click to toggle source
# 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?()
click to toggle source
# File lib/mangadex/api/user.rb, line 72 def session_expired? @session_valid_until.nil? || @session_valid_until <= Time.now end
to_h(except: [])
click to toggle source
# 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?()
click to toggle source
# File lib/mangadex/api/user.rb, line 90 def valid? !mangadex_user_id.nil? && !mangadex_user_id.strip.empty? end