Class: Mangadex::Internal::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/mangadex/internal/request.rb

Constant Summary collapse

ALLOWED_METHODS =
i(get post put delete).freeze
SENSITIVE_FIELDS =
%w(password token oldPassword newPassword)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, method:, headers: nil, payload: nil) ⇒ Request



35
36
37
38
39
40
# File 'lib/mangadex/internal/request.rb', line 35

def initialize(path, method:, headers: nil, payload: nil)
  @path = path
  @headers = Hash(headers)
  @payload = payload
  @method = ensure_method!(method)
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



11
12
13
# File 'lib/mangadex/internal/request.rb', line 11

def headers
  @headers
end

#methodObject

Returns the value of attribute method.



11
12
13
# File 'lib/mangadex/internal/request.rb', line 11

def method
  @method
end

#pathObject

Returns the value of attribute path.



11
12
13
# File 'lib/mangadex/internal/request.rb', line 11

def path
  @path
end

#payloadObject

Returns the value of attribute payload.



11
12
13
# File 'lib/mangadex/internal/request.rb', line 11

def payload
  @payload
end

#rawObject

Returns the value of attribute raw.



11
12
13
# File 'lib/mangadex/internal/request.rb', line 11

def raw
  @raw
end

#responseObject (readonly)

Returns the value of attribute response.



12
13
14
# File 'lib/mangadex/internal/request.rb', line 12

def response
  @response
end

Class Method Details

.delete(path, headers: nil, auth: false, payload: nil, raw: false) ⇒ Object



31
32
33
# File 'lib/mangadex/internal/request.rb', line 31

def self.delete(path, headers: nil, auth: false, payload: nil, raw: false)
  new(path, method: :delete, headers: headers, payload: payload).run_with_info!(raw: raw, auth: auth)
end

.get(path, params = {}, auth: false, headers: nil, raw: false, content_rating: false) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/mangadex/internal/request.rb', line 14

def self.get(path, params={}, auth: false, headers: nil, raw: false, content_rating: false)
  new(
    path_with_params(path, params, content_rating),
    method: :get,
    headers: headers,
    payload: nil,
  ).run_with_info!(raw: raw, auth: auth)
end

.post(path, headers: nil, auth: false, payload: nil, raw: false) ⇒ Object



23
24
25
# File 'lib/mangadex/internal/request.rb', line 23

def self.post(path, headers: nil, auth: false, payload: nil, raw: false)
  new(path, method: :post, headers: headers, payload: payload).run_with_info!(raw: raw, auth: auth)
end

.put(path, headers: nil, auth: false, payload: nil, raw: false) ⇒ Object



27
28
29
# File 'lib/mangadex/internal/request.rb', line 27

def self.put(path, headers: nil, auth: false, payload: nil, raw: false)
  new(path, method: :put, headers: headers, payload: payload).run_with_info!(raw: raw, auth: auth)
end

Instance Method Details

#requestObject



42
43
44
45
46
47
48
49
# File 'lib/mangadex/internal/request.rb', line 42

def request
  RestClient::Request.new(
    method: method,
    url: request_url,
    headers: request_headers,
    payload: request_payload,
  )
end

#run!(raw: false, auth: false) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mangadex/internal/request.rb', line 57

def run!(raw: false, auth: false)
  raise Mangadex::Errors::UserNotLoggedIn.new if auth && Mangadex.context.user.nil?

  @response = request.execute
  raw_request = raw || Mangadex.context.force_raw_requests

  if (body = @response.body)
    raw_request ? try_json(body) : Mangadex::Api::Response.coerce(try_json(body))
  end
rescue RestClient::Unauthorized => error
  raise Errors::UnauthorizedError.new(Mangadex::Api::Response.coerce(try_json(error.response.body)))
rescue RestClient::Exception => error
  if (body = error.response.body)
    raw_request ? try_json(body) : Mangadex::Api::Response.coerce(JSON.parse(body)) rescue raise error
  else
    raise error
  end
end

#run_with_info!(*args, **kwargs) ⇒ Object



51
52
53
54
55
# File 'lib/mangadex/internal/request.rb', line 51

def run_with_info!(*args, **kwargs)
  measure_time_taken do
    run!(*args, **kwargs)
  end
end