Class: Mangadex::Internal::Request
- Inherits:
-
Object
- Object
- Mangadex::Internal::Request
- Defined in:
- lib/mangadex/internal/request.rb
Constant Summary collapse
- BASE_URI =
'https://api.mangadex.org'
- 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) ⇒ 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.
37 38 39 40 41 42 |
# File 'lib/mangadex/internal/request.rb', line 37 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.
13 14 15 |
# File 'lib/mangadex/internal/request.rb', line 13 def headers @headers end |
#method ⇒ Object
Returns the value of attribute method.
13 14 15 |
# File 'lib/mangadex/internal/request.rb', line 13 def method @method end |
#path ⇒ Object
Returns the value of attribute path.
13 14 15 |
# File 'lib/mangadex/internal/request.rb', line 13 def path @path end |
#payload ⇒ Object
Returns the value of attribute payload.
13 14 15 |
# File 'lib/mangadex/internal/request.rb', line 13 def payload @payload end |
#raw ⇒ Object
Returns the value of attribute raw.
13 14 15 |
# File 'lib/mangadex/internal/request.rb', line 13 def raw @raw end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
14 15 16 |
# File 'lib/mangadex/internal/request.rb', line 14 def response @response end |
Class Method Details
.delete(path, headers: nil, auth: false, payload: nil, raw: false) ⇒ Object
33 34 35 |
# File 'lib/mangadex/internal/request.rb', line 33 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) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/mangadex/internal/request.rb', line 16 def self.get(path, params={}, auth: false, headers: nil, raw: 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
25 26 27 |
# File 'lib/mangadex/internal/request.rb', line 25 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
29 30 31 |
# File 'lib/mangadex/internal/request.rb', line 29 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
44 45 46 47 48 49 50 51 |
# File 'lib/mangadex/internal/request.rb', line 44 def request RestClient::Request.new( method: method, url: request_url, headers: request_headers, payload: request_payload, ) end |
#run!(raw: false, auth: false) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/mangadex/internal/request.rb', line 53 def run!(raw: false, auth: false) payload_details = request_payload ? "Payload: #{request_payload}" : "{no-payload}" puts("[#{self.class.name}] #{method.to_s.upcase} #{request_url} #{payload_details}") raise Mangadex::UserNotLoggedIn.new if auth && Mangadex::Api::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") if @response.body raw ? @response.body : Mangadex::Api::Response.coerce(JSON.parse(@response.body)) end rescue RestClient::Exception => error if error.response.body raw ? error.response.body : Mangadex::Api::Response.coerce(JSON.parse(error.response.body)) else raise error end end |