Class: Mangadex::Internal::Context

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/mangadex/internal/context.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



10
11
12
# File 'lib/mangadex/internal/context.rb', line 10

def initialize
  @allowed_content_ratings = Mangadex.configuration.default_content_ratings
end

Instance Attribute Details

#allowed_content_ratingsObject

Returns the value of attribute allowed_content_ratings.



8
9
10
# File 'lib/mangadex/internal/context.rb', line 8

def allowed_content_ratings
  @allowed_content_ratings
end

#ignore_userObject

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

Returns:

  • (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 allow_content_ratings(*content_ratings, &block)
  content_ratings = Mangadex::ContentRating.parse(Array(content_ratings))
  if block_given?
    content_ratings = Mangadex.context.allowed_content_ratings if content_ratings.empty?

    # set temporarily
    temp_set_value("allowed_content_ratings", content_ratings) do
      yield
    end
  elsif content_ratings.any?
    # set "permanently"
    @allowed_content_ratings = content_ratings
  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

#tagsObject



28
29
30
# File 'lib/mangadex/internal/context.rb', line 28

def tags
  @tags ||= Mangadex::Tag.list.data
end

#userObject



20
21
22
23
24
25
# File 'lib/mangadex/internal/context.rb', line 20

def user
  @ignore_user ? nil : @user
rescue Mangadex::Errors::UnauthorizedError
  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

#versionObject



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 with_allowed_content_ratings(*other_content_ratings, &block)
  T.unsafe(self).allow_content_ratings(*(allowed_content_ratings + other_content_ratings)) 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