class Howitzer::MailtrapApi::Client

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

Public Class Methods

new() click to toggle source
# File lib/howitzer/mailtrap_api/client.rb, line 10
def initialize
  @api_token = Howitzer.mailtrap_api_token
end

Public Instance Methods

find_attachments(message) click to toggle source

Finds attachments for message

@param message [Hash] which attachments should be extracted @return [Array] returns array of attachments

# 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) click to toggle source

Finds message according to given parameters

@param recipient [String] this is recipient mail address for message filtering @param subject [String] this is subject of the message to filter particular message @return [Hash] json message parsed to ruby hash

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

filter_by_subject(messages, subject) click to toggle source
# File lib/howitzer/mailtrap_api/client.rb, line 63
def filter_by_subject(messages, subject)
  result_messages = []
  messages.each { |msg| result_messages << msg if msg['subject'] == subject }
  result_messages
end
latest_message(messages) click to toggle source
# File lib/howitzer/mailtrap_api/client.rb, line 59
def latest_message(messages)
  messages[0]
end
messages(recipient) click to toggle source
# File lib/howitzer/mailtrap_api/client.rb, line 55
def messages(recipient)
  JSON.parse(RestClient.get("#{BASE_URL}/messages?search=#{recipient}", 'Api-Token' => @api_token))
end