Class: Mangadex::Internal::Request
- Defined in:
- lib/mangadex/internal/request.rb
Constant Summary collapse
- ALLOWED_METHODS =
i(get post put delete).freeze
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#method ⇒ Object
Returns the value of attribute method.
-
#path ⇒ Object
Returns the value of attribute path.
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#raw ⇒ Object
Returns the value of attribute raw.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Class Method Summary collapse
- .delete(path, headers: nil, auth: false, payload: nil, raw: false) ⇒ Object
- .get(path, params = {}, auth: false, headers: nil, raw: false, content_rating: false) ⇒ Object
- .post(path, headers: nil, auth: false, payload: nil, raw: false) ⇒ Object
- .put(path, headers: nil, auth: false, payload: nil, raw: false) ⇒ Object
Instance Method Summary collapse
-
#initialize(path, method:, headers: nil, payload: nil) ⇒ Request
constructor
A new instance of Request.
- #request ⇒ Object
- #run!(raw: false, auth: false) ⇒ Object
Constructor Details
#initialize(path, method:, headers: nil, payload: nil) ⇒ Request
Returns a new instance of Request.
34 35 36 37 38 39 |
# File 'lib/mangadex/internal/request.rb', line 34 def initialize(path, method:, headers: nil, payload: nil) @path = path @headers = Hash(headers) @payload = payload @method = ensure_method!(method) end |
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
10 11 12 |
# File 'lib/mangadex/internal/request.rb', line 10 def headers @headers end |
#method ⇒ Object
Returns the value of attribute method.
10 11 12 |
# File 'lib/mangadex/internal/request.rb', line 10 def method @method end |
#path ⇒ Object
Returns the value of attribute path.
10 11 12 |
# File 'lib/mangadex/internal/request.rb', line 10 def path @path end |
#payload ⇒ Object
Returns the value of attribute payload.
10 11 12 |
# File 'lib/mangadex/internal/request.rb', line 10 def payload @payload end |
#raw ⇒ Object
Returns the value of attribute raw.
10 11 12 |
# File 'lib/mangadex/internal/request.rb', line 10 def raw @raw end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
11 12 13 |
# File 'lib/mangadex/internal/request.rb', line 11 def response @response end |
Class Method Details
.delete(path, headers: nil, auth: false, payload: nil, raw: false) ⇒ Object
30 31 32 |
# File 'lib/mangadex/internal/request.rb', line 30 def self.delete(path, headers: nil, auth: false, payload: nil, raw: false) new(path, method: :delete, headers: headers, payload: payload).run!(raw: raw, auth: auth) end |
.get(path, params = {}, auth: false, headers: nil, raw: false, content_rating: false) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/mangadex/internal/request.rb', line 13 def self.get(path, params={}, auth: false, headers: nil, raw: false, content_rating: false) new( path_with_params(path, params, ), method: :get, headers: headers, payload: nil, ).run!(raw: raw, auth: auth) end |
.post(path, headers: nil, auth: false, payload: nil, raw: false) ⇒ Object
22 23 24 |
# File 'lib/mangadex/internal/request.rb', line 22 def self.post(path, headers: nil, auth: false, payload: nil, raw: false) new(path, method: :post, headers: headers, payload: payload).run!(raw: raw, auth: auth) end |
.put(path, headers: nil, auth: false, payload: nil, raw: false) ⇒ Object
26 27 28 |
# File 'lib/mangadex/internal/request.rb', line 26 def self.put(path, headers: nil, auth: false, payload: nil, raw: false) new(path, method: :put, headers: headers, payload: payload).run!(raw: raw, auth: auth) end |
Instance Method Details
#request ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/mangadex/internal/request.rb', line 41 def request RestClient::Request.new( method: method, url: request_url, headers: request_headers, payload: request_payload, ) end |
#run!(raw: false, auth: false) ⇒ Object
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 |
# File 'lib/mangadex/internal/request.rb', line 50 def run!(raw: false, auth: false) payload_details = request_payload ? "Payload: #{sensitive_request_payload}" : "{no-payload}" puts("[#{self.class.name}] #{method.to_s.upcase} #{request_url} #{payload_details}") raise Mangadex::Errors::UserNotLoggedIn.new if auth && Mangadex.context.user.nil? start_time = Time.now @response = request.execute end_time = Time.now elapsed_time = ((end_time - start_time) * 1000).to_i puts("[#{self.class.name}] took #{elapsed_time} ms") 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:: => error raise Errors::.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 |