Class: Howitzer::MailtrapApi::Client

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

Overview

A Mailtrap::Client object is used to communicate with the Mailtrap API.

Constant Summary collapse

BASE_URL =

:nodoc:

"https://mailtrap.io/api/v1/inboxes/#{Howitzer.mailtrap_inbox_id}".freeze

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



10
11
12
# File 'lib/howitzer/mailtrap_api/client.rb', line 10

def initialize
  @api_token = Howitzer.mailtrap_api_token
end

Instance Method Details

#find_attachments(message) ⇒ Array

Finds attachments for message

Parameters:

  • message (Hash)

    which attachments should be extracted

Returns:

  • (Array)

    returns array of attachments



31
32
33
# File 'lib/howitzer/mailtrap_api/client.rb', line 31

def find_attachments(message)
  JSON.parse(RestClient.get("#{BASE_URL}/messages/#{message['id']}/attachments", 'Api-Token' => @api_token))
end

#find_message(recipient, subject) ⇒ Hash

Finds message according to given parameters

Parameters:

  • recipient (String)

    this is recipient mail address for message filtering

  • subject (String)

    this is subject of the message to filter particular message

Returns:

  • (Hash)

    json message parsed to ruby hash



20
21
22
23
24
# File 'lib/howitzer/mailtrap_api/client.rb', line 20

def find_message(recipient, subject)
  recipient = recipient.gsub('+', '%2B')
  messages = filter_by_subject(messages(recipient), subject)
  latest_message(messages)
end

#get_html_body(message) ⇒ Object



35
36
37
38
39
# File 'lib/howitzer/mailtrap_api/client.rb', line 35

def get_html_body(message)
  RestClient.get("#{BASE_URL}/messages/#{message['id']}/body.html", 'Api-Token' => @api_token).body
rescue => e
  raise Howitzer::CommunicationError, e.message
end

#get_raw_body(message) ⇒ Object



47
48
49
50
51
# File 'lib/howitzer/mailtrap_api/client.rb', line 47

def get_raw_body(message)
  RestClient.get("#{BASE_URL}/messages/#{message['id']}/body.raw", 'Api-Token' => @api_token).body
rescue => e
  raise Howitzer::CommunicationError, e.message
end

#get_txt_body(message) ⇒ Object



41
42
43
44
45
# File 'lib/howitzer/mailtrap_api/client.rb', line 41

def get_txt_body(message)
  RestClient.get("#{BASE_URL}/messages/#{message['id']}/body.txt", 'Api-Token' => @api_token).body
rescue => e
  raise Howitzer::CommunicationError, e.message
end