Class: Resend::Request
- Inherits:
-
Object
- Object
- Resend::Request
- Defined in:
- lib/resend/request.rb
Overview
This class is responsible for making the appropriate HTTP calls and raising the specific errors based on the response.
Direct Known Subclasses
Constant Summary collapse
- BASE_URL =
ENV["RESEND_BASE_URL"] || "https://api.resend.com/"
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#options ⇒ Object
Returns the value of attribute options.
-
#verb ⇒ Object
Returns the value of attribute verb.
Instance Method Summary collapse
- #handle_error!(data, resp = nil) ⇒ Object
-
#initialize(path = "", body = {}, verb = "POST", options: {}) ⇒ Request
constructor
A new instance of Request.
-
#perform ⇒ Object
Performs the HTTP call.
Constructor Details
#initialize(path = "", body = {}, verb = "POST", options: {}) ⇒ Request
Returns a new instance of Request.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/resend/request.rb', line 11 def initialize(path = "", body = {}, verb = "POST", options: {}) raise if (api_key = Resend.api_key).nil? api_key = api_key.call if api_key.is_a?(Proc) @path = path @body = body @verb = verb @options = @headers = { "Content-Type" => "application/json", "Accept" => "application/json", "User-Agent" => "resend-ruby:#{Resend::VERSION}", "Authorization" => "Bearer #{api_key}" } set_idempotency_key set_batch_validation end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
9 10 11 |
# File 'lib/resend/request.rb', line 9 def body @body end |
#options ⇒ Object
Returns the value of attribute options.
9 10 11 |
# File 'lib/resend/request.rb', line 9 def @options end |
#verb ⇒ Object
Returns the value of attribute verb.
9 10 11 |
# File 'lib/resend/request.rb', line 9 def verb @verb end |
Instance Method Details
#handle_error!(data, resp = nil) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/resend/request.rb', line 43 def handle_error!(data, resp = nil) code = data[:statusCode] body = data[:message] headers = resp.respond_to?(:headers) ? resp.headers : (data[:headers] || {}) # get error from the known list of errors error_class = Resend::Error::ERRORS[code] || Resend::Error raise error_class.new(body, code, headers) end |
#perform ⇒ Object
Performs the HTTP call
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/resend/request.rb', line 32 def perform = resp = HTTParty.send(@verb.to_sym, "#{BASE_URL}#{@path}", ) check_json!(resp) data = process_response(resp) headers = extract_headers(resp) Resend::Response.new(data, headers) end |