Sha256: a9e4b94f4f84a07988904c180025597d1fdaa38c93d327f8ed4238557488d9d8

Contents?: true

Size: 1.29 KB

Versions: 77

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

require "resend/version"
require "resend/errors"
require "httparty"

module Resend
  # This class is responsible for making the appropriate HTTP calls
  # and raising the specific errors based on the response.
  class Request
    BASE_URL = "https://api.resend.com/"

    attr_accessor :body, :verb

    def initialize(path = "", body = {}, verb = "POST")
      raise if Resend.api_key.nil?

      @path = path
      @body = body
      @verb = verb
      @headers = {
        "Content-Type" => "application/json",
        "Accept" => "application/json",
        "User-Agent" => "resend-ruby:#{Resend::VERSION}",
        "Authorization" => "Bearer #{Resend.api_key}"
      }
    end

    # Performs the HTTP call
    def perform
      options = {
        headers: @headers
      }

      options[:body] = @body.to_json unless @body.empty?
      resp = HTTParty.send(@verb.to_sym, "#{BASE_URL}#{@path}", options)
      resp.transform_keys!(&:to_sym) unless resp.body.empty?
      handle_error!(resp) if resp[:statusCode] && (resp[:statusCode] != 200 || resp[:statusCode] != 201)
      resp
    end

    def handle_error!(resp)
      code = resp[:statusCode]
      body = resp[:message]
      error = Resend::Error::ERRORS[code]
      raise(error.new(body, code)) if error
    end
  end
end

Version data entries

77 entries across 77 versions & 2 rubygems

Version Path
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/resend-0.8.1/lib/resend/request.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/resend-0.8.1/lib/resend/request.rb
resend-0.9.0 lib/resend/request.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/resend-0.8.1/lib/resend/request.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/resend-0.8.1/lib/resend/request.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/resend-0.8.1/lib/resend/request.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/resend-0.8.1/lib/resend/request.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/resend-0.8.1/lib/resend/request.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/resend-0.8.1/lib/resend/request.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/resend-0.8.1/lib/resend/request.rb
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/resend-0.8.1/lib/resend/request.rb
harbr-0.2.1 vendor/bundle/ruby/3.2.0/gems/resend-0.8.1/lib/resend/request.rb
harbr-0.2.0 vendor/bundle/ruby/3.2.0/gems/resend-0.8.1/lib/resend/request.rb
harbr-0.1.99 vendor/bundle/ruby/3.2.0/gems/resend-0.8.1/lib/resend/request.rb
harbr-0.1.98 vendor/bundle/ruby/3.2.0/gems/resend-0.8.1/lib/resend/request.rb
harbr-0.1.97 vendor/bundle/ruby/3.2.0/gems/resend-0.8.1/lib/resend/request.rb
harbr-0.1.96 vendor/bundle/ruby/3.2.0/gems/resend-0.8.1/lib/resend/request.rb
harbr-0.1.95 vendor/bundle/ruby/3.2.0/gems/resend-0.8.1/lib/resend/request.rb
harbr-0.1.94 vendor/bundle/ruby/3.2.0/gems/resend-0.8.1/lib/resend/request.rb
harbr-0.1.93 vendor/bundle/ruby/3.2.0/gems/resend-0.8.1/lib/resend/request.rb