Class: Mailosaur::Files

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

Overview

Operations for downloading the raw content associated with a message — file attachments, the full EML source of an email, and rendered email previews. Accessed via client.files.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn, handle_http_error) ⇒ Files

Creates and initializes a new instance of the Files class.

Parameters:

  • conn (Faraday::Connection)

    The client connection.

  • handle_http_error (Method)

    Callback used to convert HTTP error responses into errors.



10
11
12
13
# File 'lib/Mailosaur/files.rb', line 10

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.



16
17
18
# File 'lib/Mailosaur/files.rb', line 16

def conn
  @conn
end

Instance Method Details

#get_attachment(id) ⇒ String

Downloads a single attachment.

Parameters:

  • id (String)

    The identifier for the required attachment.

Returns:

  • (String)

    The attachment’s binary content.



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

def get_attachment(id)
  response = conn.get "api/files/attachments/#{id}"
  @handle_http_error.call(response) unless response.status == 200
  response.body
end

#get_email(id) ⇒ String

Downloads an EML file representing the specified email.

Parameters:

  • id (String)

    The identifier for the required message.

Returns:

  • (String)

    The raw EML content of the email.



38
39
40
41
42
# File 'lib/Mailosaur/files.rb', line 38

def get_email(id)
  response = conn.get "api/files/email/#{id}"
  @handle_http_error.call(response) unless response.status == 200
  response.body
end

#get_preview(id) ⇒ String

Downloads a screenshot of your email rendered in a real email client. Simply supply the unique identifier for the required preview.

Parameters:

  • id (String)

    The identifier of the email preview to be downloaded.

Returns:

  • (String)

    The preview screenshot image content.

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/Mailosaur/files.rb', line 55

def get_preview(id)
  timeout = 120_000
  poll_count = 0
  start_time = Time.now.to_f

  loop do
    response = conn.get "api/files/screenshots/#{id}"

    return response.body if response.status == 200

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

    delay_pattern = (response.headers['x-ms-delay'] || '1000').split(',').map(&:to_i)

    delay = poll_count >= delay_pattern.length ? delay_pattern[delay_pattern.length - 1] : delay_pattern[poll_count]

    poll_count += 1

    ## Stop if timeout will be exceeded
    if ((1000 * (Time.now.to_f - start_time).to_i) + delay) > timeout
      msg = format('An email preview was not generated in time. The email client may not be available, or the preview ID [%s] may be incorrect.', id)
      raise Mailosaur::MailosaurError.new(msg, 'preview_timeout')
    end

    sleep(delay / 1000.0)
  end
end