Class: Mangadex::Internal::Context
- Extended by:
- T::Sig
- Defined in:
- lib/mangadex/internal/context.rb
Instance Attribute Summary collapse
-
#allowed_content_ratings ⇒ Object
Returns the value of attribute allowed_content_ratings.
-
#ignore_user ⇒ Object
Returns the value of attribute ignore_user.
Class Method Summary collapse
Instance Method Summary collapse
- #allow_content_ratings(*content_ratings, &block) ⇒ Object
-
#force_raw_requests(&block) ⇒ Object
Only recommended for development and debugging only.
-
#initialize ⇒ Context
constructor
A new instance of Context.
- #tags ⇒ Object
- #user ⇒ Object
- #user=(user) ⇒ Object
- #version ⇒ Object
- #with_allowed_content_ratings(*other_content_ratings, &block) ⇒ Object
- #with_user(user, &block) ⇒ Object
- #without_user(&block) ⇒ Object
Constructor Details
#initialize ⇒ Context
Returns a new instance of Context.
10 11 12 |
# File 'lib/mangadex/internal/context.rb', line 10 def initialize = Mangadex.configuration. end |
Instance Attribute Details
#allowed_content_ratings ⇒ Object
Returns the value of attribute allowed_content_ratings.
8 9 10 |
# File 'lib/mangadex/internal/context.rb', line 8 def end |
#ignore_user ⇒ Object
Returns the value of attribute ignore_user.
8 9 10 |
# File 'lib/mangadex/internal/context.rb', line 8 def ignore_user @ignore_user end |
Class Method Details
.user_object?(user) ⇒ Boolean
117 118 119 120 121 122 123 124 125 |
# File 'lib/mangadex/internal/context.rb', line 117 def self.user_object?(user) return false if user.nil? missing_methods = [:session, :refresh, :mangadex_user_id, :save] - user.methods return true if missing_methods.empty? warn("Potential user object #{user} is missing #{missing_methods}") false end |
Instance Method Details
#allow_content_ratings(*content_ratings, &block) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/mangadex/internal/context.rb', line 82 def (*, &block) = Mangadex::ContentRating.parse(Array()) if block_given? = Mangadex.context. if .empty? # set temporarily temp_set_value("allowed_content_ratings", ) do yield end elsif .any? # set "permanently" = else # This is to throw an exception prompting to pass a block if there no params. yield end end |
#force_raw_requests(&block) ⇒ Object
Only recommended for development and debugging only
107 108 109 110 111 112 113 114 115 |
# File 'lib/mangadex/internal/context.rb', line 107 def force_raw_requests(&block) if block_given? temp_set_value("force_raw_requests", true) do yield end else !!@force_raw_requests end end |
#tags ⇒ Object
28 29 30 |
# File 'lib/mangadex/internal/context.rb', line 28 def ||= Mangadex::Tag.list.data end |
#user ⇒ Object
20 21 22 23 24 25 |
# File 'lib/mangadex/internal/context.rb', line 20 def user @ignore_user ? nil : @user rescue Mangadex::Errors:: warn("A user is present but not authenticated!") nil end |
#user=(user) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/mangadex/internal/context.rb', line 47 def user=(user) if user.is_a?(Mangadex::Api::User) @user = user elsif user.is_a?(Mangadex::User) @user = Mangadex::Api::User.new( mangadex_user_id: user.id, data: user, ) elsif user.is_a?(Hash) user = Mangadex::Internal::Definition.validate(user, { mangadex_user_id: { accepts: String, required: true }, session: { accepts: String }, refresh: { accepts: String }, }) @user = Mangadex::Api::User.new( mangadex_user_id: user[:mangadex_user_id], session: user[:session], refresh: user[:refresh], ) elsif Mangadex::Internal::Context.user_object?(user) @user = Mangadex::Api::User.new( mangadex_user_id: user.mangadex_user_id.to_s, session: user.session, refresh: user.refresh, session_valid_until: user.session_valid_until, data: user, ) elsif user.nil? @user = nil else raise TypeError, "Invalid user type." end end |
#version ⇒ Object
15 16 17 |
# File 'lib/mangadex/internal/context.rb', line 15 def version @version ||= Mangadex::Api::VersionChecker.check_mangadex_version end |
#with_allowed_content_ratings(*other_content_ratings, &block) ⇒ Object
100 101 102 103 104 |
# File 'lib/mangadex/internal/context.rb', line 100 def (*, &block) T.unsafe(self).(*( + )) do yield end end |
#with_user(user, &block) ⇒ Object
33 34 35 36 37 |
# File 'lib/mangadex/internal/context.rb', line 33 def with_user(user, &block) temp_set_value("user", user) do yield end end |
#without_user(&block) ⇒ Object
40 41 42 43 44 |
# File 'lib/mangadex/internal/context.rb', line 40 def without_user(&block) temp_set_value("ignore_user", true) do yield end end |