Class: Mailosaur::Servers

Inherits:
Object
  • Object
show all
Defined in:
lib/Mailosaur/servers.rb

Overview

Operations for creating and managing your Mailosaur inboxes (servers) — they group your tests together, each with its own domain and SMTP/POP3/IMAP credentials. Accessed via client.servers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn, handle_http_error) ⇒ Servers

Creates and initializes a new instance of the Servers class.

Parameters:

  • conn (Faraday::Connection)

    The client connection.

  • handle_http_error (Method)

    Callback used to convert HTTP error responses into errors.



11
12
13
14
# File 'lib/Mailosaur/servers.rb', line 11

def initialize(conn, handle_http_error)
  @conn = conn
  @handle_http_error = handle_http_error
end

Instance Attribute Details

#connConnection (readonly)

Returns the client connection.

Returns:

  • (Connection)

    the client connection.



17
18
19
# File 'lib/Mailosaur/servers.rb', line 17

def conn
  @conn
end

Instance Method Details

#create(server_create_options) ⇒ Mailosaur::Models::Server

Creates a new inbox (server).

Parameters:

Returns:



40
41
42
43
44
45
# File 'lib/Mailosaur/servers.rb', line 40

def create(server_create_options)
  response = conn.post 'api/servers', server_create_options.to_json
  @handle_http_error.call(response) unless response.status == 200
  model = JSON.parse(response.body)
  Mailosaur::Models::Server.new(model)
end

#delete(id) ⇒ nil

Permanently delete an inbox (server). This will also delete all messages, associated attachments, etc. within the inbox (server). This operation cannot be undone.

Parameters:

  • id (String)

    The unique identifier of the inbox (server).

Returns:

  • (nil)

    Once the inbox (server) has been deleted.



99
100
101
102
103
# File 'lib/Mailosaur/servers.rb', line 99

def delete(id)
  response = conn.delete "api/servers/#{id}"
  @handle_http_error.call(response) unless response.status == 204
  nil
end

#generate_email_address(server) ⇒ String

Generates a random email address by appending a random string in front of the domain name of the inbox (server).

Parameters:

  • server (String)

    The identifier of the inbox (server).

Returns:

  • (String)

    A random email address ending in the domain of the inbox (server).



113
114
115
116
# File 'lib/Mailosaur/servers.rb', line 113

def generate_email_address(server)
  host = ENV['MAILOSAUR_SMTP_HOST'] || 'mailosaur.net'
  format('%s@%s.%s', SecureRandom.hex(3), server, host)
end

#get(id) ⇒ Mailosaur::Models::Server

Retrieves the detail for a single inbox (server).

Parameters:

  • id (String)

    The unique identifier of the inbox (server).

Returns:



54
55
56
57
58
59
# File 'lib/Mailosaur/servers.rb', line 54

def get(id)
  response = conn.get "api/servers/#{id}"
  @handle_http_error.call(response) unless response.status == 200
  model = JSON.parse(response.body)
  Mailosaur::Models::Server.new(model)
end

#get_password(id) ⇒ String

Retrieves the password for an inbox (server). This password can be used for SMTP, POP3, and IMAP connectivity.

Parameters:

  • id (String)

    The unique identifier of the inbox (server).

Returns:

  • (String)

    The password for the inbox (server).



69
70
71
72
73
74
# File 'lib/Mailosaur/servers.rb', line 69

def get_password(id)
  response = conn.get "api/servers/#{id}/password"
  @handle_http_error.call(response) unless response.status == 200
  model = JSON.parse(response.body)
  model['value']
end

#listMailosaur::Models::ServerListResult

Returns a list of your inboxes (servers). Inboxes (servers) are returned sorted in alphabetical order.

Returns:



25
26
27
28
29
30
# File 'lib/Mailosaur/servers.rb', line 25

def list
  response = conn.get 'api/servers'
  @handle_http_error.call(response) unless response.status == 200
  model = JSON.parse(response.body)
  Mailosaur::Models::ServerListResult.new(model)
end

#update(id, server) ⇒ Mailosaur::Models::Server

Updates the attributes of an inbox (server).

Parameters:

  • id (String)

    The unique identifier of the inbox (server).

  • server (Mailosaur::Models::Server)

    The updated inbox (server).

Returns:



84
85
86
87
88
89
# File 'lib/Mailosaur/servers.rb', line 84

def update(id, server)
  response = conn.put "api/servers/#{id}", server.to_json
  @handle_http_error.call(response) unless response.status == 200
  model = JSON.parse(response.body)
  Mailosaur::Models::Server.new(model)
end