class Mailosaur::Messages

Attributes

conn[R]

@return [Connection] the client connection.

Public Class Methods

new(conn, handle_http_error) click to toggle source

Creates and initializes a new instance of the Messages class. @param client connection.

# File lib/Mailosaur/messages.rb, line 9
def initialize(conn, handle_http_error)
  @conn = conn
  @handle_http_error = handle_http_error
end

Public Instance Methods

delete(id) click to toggle source

Delete a message

Permanently deletes a message. This operation cannot be undone. Also deletes any attachments related to the message.

@param id The identifier of the message to be deleted.

# File lib/Mailosaur/messages.rb, line 66
def delete(id)
  response = conn.delete 'api/messages/' + id
  @handle_http_error.call(response) unless response.status == 204
  nil
end
delete_all(server) click to toggle source

Delete all messages

Permanently deletes all messages held by the specified server. This operation cannot be undone. Also deletes any attachments related to each message.

@param server [String] The identifier of the server to be emptied.

# File lib/Mailosaur/messages.rb, line 111
def delete_all(server)
  response = conn.delete 'api/messages?server=' + server
  @handle_http_error.call(response) unless response.status == 204
  nil
end
get(server, criteria, timeout: 10_000, received_after: DateTime.now - (1.0 / 24)) click to toggle source

Retrieve a message using search criteria

Returns as soon as a message matching the specified search criteria is found. This is the most efficient method of looking up a message.

@param server [String] The identifier of the server hosting the message. @param criteria [SearchCriteria] The search criteria to use in order to find a match. @param timeout [Integer] Specify how long to wait for a matching result (in milliseconds). @param received_after [DateTime] Limits results to only messages received after this date/time.

@return [Message] operation results.

# File lib/Mailosaur/messages.rb, line 33
def get(server, criteria, timeout: 10_000, received_after: DateTime.now - (1.0 / 24))
  # Defaults timeout to 10s, receivedAfter to 1h
  raise Mailosaur::MailosaurError.new('Must provide a valid Server ID.', 'invalid_request') if server.length != 8

  result = search(server, criteria, page: 0, items_per_page: 1, timeout: timeout, received_after: received_after)
  get_by_id(result.items[0].id)
end
get_by_id(id) click to toggle source

Retrieve a message

Retrieves the detail for a single email message. Simply supply the unique identifier for the required message.

@param id The identifier of the email message to be retrieved.

@return [Message] operation results.

# File lib/Mailosaur/messages.rb, line 51
def get_by_id(id)
  response = conn.get 'api/messages/' + id
  @handle_http_error.call(response) unless response.status == 200
  model = JSON.load(response.body)
  Mailosaur::Models::Message.new(model)
end
list(server, page: nil, items_per_page: nil, received_after: nil) click to toggle source

List all messages

Returns a list of your messages in summary form. The summaries are returned sorted by received date, with the most recently-received messages appearing first.

@param server [String] The identifier of the server hosting the messages. @param page [Integer] Used in conjunction with `itemsPerPage` to support pagination. @param items_per_page [Integer] A limit on the number of results to be returned per page. Can be set between 1 and 1000 items, the default is 50. @param received_after [DateTime] Limits results to only messages received after this date/time.

@return [MessageListResult] operation results.

# File lib/Mailosaur/messages.rb, line 89
def list(server, page: nil, items_per_page: nil, received_after: nil)
  url = 'api/messages?server=' + server
  url += page ? '&page=' + page.to_s : ''
  url += items_per_page ? '&itemsPerPage=' + items_per_page.to_s : ''
  url += received_after ? '&receivedAfter=' + CGI.escape(received_after.iso8601) : ''

  response = conn.get url

  @handle_http_error.call(response) unless response.status == 200

  model = JSON.load(response.body)
  Mailosaur::Models::MessageListResult.new(model)
end