Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
vittorius committed Mar 7, 2024
1 parent 1fd0e2a commit 7b7c1e8
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions lib/mailtrap/sending/client.rb
Expand Up @@ -7,15 +7,40 @@
module Mailtrap
module Sending
class Client
API_HOST = 'send.api.mailtrap.io'
SENDING_API_HOST = 'send.api.mailtrap.io'
BULK_SENDING_API_HOST = 'send.api.mailtrap.io'
SANDBOX_API_HOST = 'sandbox.api.mailtrap.io'
API_PORT = 443

attr_reader :api_key, :api_host, :api_port
attr_reader :api_key, :api_host, :api_port, :bulk, :sandbox, :inbox_id

# Initializes a new Mailtrap::Sending::Client instance.
#
# @param [String] api_key The Mailtrap API key to use for sending. Required.
# @param [String, nil] api_host The Mailtrap API hostname. Default is based on sandbox and bulk params.
# @param [Integer] api_port The Mailtrap API port. Required.
# @param [Boolean] bulk Whether to use the Mailtrap bulk API. Default false.
# @param [boolean] sandbox Whether to use the Mailtrap sandbox API. Default false.
# @param [Integer] inbox_id The inbox ID to send to. Optional.
def initialize( # rubocop:disable Metrics/ParameterLists
api_key: ENV.fetch('MAILTRAP_API_KEY'),
api_host: nil,
api_port: API_PORT,
bulk: false,
sandbox: false,
inbox_id: nil
)
raise ArgumentError, 'api_key is required' if api_key.nil?
raise ArgumentError, 'api_port is required' if api_port.nil?

api_host ||= select_api_host(bulk: bulk, sandbox: sandbox)

def initialize(api_key: ENV.fetch('MAILTRAP_API_KEY'), api_host: API_HOST, api_port: API_PORT)
@api_key = api_key
@api_host = api_host
@api_port = api_port
@bulk = bulk
@sandbox = sandbox
@inbox_id = inbox_id
end

def send(mail)
Expand All @@ -29,6 +54,18 @@ def send(mail)

private

def select_api_host(bulk:, sandbox:)
raise ArgumentError, 'bulk mode is not applicable for sandbox API' if bulk && sandbox

if sandbox
SANDBOX_API_HOST
elsif bulk
BULK_SENDING_API_HOST
else
SENDING_API_HOST
end
end

def http_client
@http_client ||= Net::HTTP.new(api_host, api_port).tap { |client| client.use_ssl = true }
end
Expand Down

0 comments on commit 7b7c1e8

Please sign in to comment.