Class: Mailtrap::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mailtrap/client.rb

Constant Summary collapse

SENDING_API_HOST =
'send.api.mailtrap.io'
BULK_SENDING_API_HOST =
'bulk.api.mailtrap.io'
SANDBOX_API_HOST =
'sandbox.api.mailtrap.io'
API_PORT =
443

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: ENV.fetch('MAILTRAP_API_KEY'), api_host: nil, api_port: API_PORT, bulk: false, sandbox: false, inbox_id: nil) ⇒ Client

Initializes a new Mailtrap::Client instance.

Parameters:

  • api_key (String) (defaults to: ENV.fetch('MAILTRAP_API_KEY'))

    The Mailtrap API key to use for sending. Required. If not set, is taken from the MAILTRAP_API_KEY environment variable.

  • api_host (String, nil) (defaults to: nil)

    The Mailtrap API hostname. If not set, is chosen internally.

  • api_port (Integer) (defaults to: API_PORT)

    The Mailtrap API port. Default: 443.

  • bulk (Boolean) (defaults to: false)

    Whether to use the Mailtrap bulk sending API. Default: false. If enabled, is incompatible with ‘sandbox: true`.

  • sandbox (Boolean) (defaults to: false)

    Whether to use the Mailtrap sandbox API. Default: false. If enabled, is incompatible with ‘bulk: true`.

  • inbox_id (Integer) (defaults to: nil)

    The sandbox inbox ID to send to. Required if sandbox API is used.

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mailtrap/client.rb', line 27

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)
  raise ArgumentError, 'inbox_id is required for sandbox API' if sandbox && inbox_id.nil?

  @api_key = api_key
  @api_host = api_host
  @api_port = api_port
  @bulk = bulk
  @sandbox = sandbox
  @inbox_id = inbox_id
end

Instance Attribute Details

#api_hostObject (readonly)

Returns the value of attribute api_host.



14
15
16
# File 'lib/mailtrap/client.rb', line 14

def api_host
  @api_host
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



14
15
16
# File 'lib/mailtrap/client.rb', line 14

def api_key
  @api_key
end

#api_portObject (readonly)

Returns the value of attribute api_port.



14
15
16
# File 'lib/mailtrap/client.rb', line 14

def api_port
  @api_port
end

#bulkObject (readonly)

Returns the value of attribute bulk.



14
15
16
# File 'lib/mailtrap/client.rb', line 14

def bulk
  @bulk
end

#inbox_idObject (readonly)

Returns the value of attribute inbox_id.



14
15
16
# File 'lib/mailtrap/client.rb', line 14

def inbox_id
  @inbox_id
end

#sandboxObject (readonly)

Returns the value of attribute sandbox.



14
15
16
# File 'lib/mailtrap/client.rb', line 14

def sandbox
  @sandbox
end

Instance Method Details

#send(mail) ⇒ Object

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
# File 'lib/mailtrap/client.rb', line 49

def send(mail)
  raise ArgumentError, 'should be Mailtrap::Mail::Base object' unless mail.is_a? Mail::Base

  request = post_request(request_url, mail.to_json)
  response = http_client.request(request)

  handle_response(response)
end