Class: MailHogwarts::Mailer

Inherits:
Object
  • Object
show all
Defined in:
lib/mailhogwarts/mailer.rb

Direct Known Subclasses

Messages, Search

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Mailer

Returns a new instance of Mailer.



5
6
7
# File 'lib/mailhogwarts/mailer.rb', line 5

def initialize(args)
  @url = "#{@domain = args[:url]}#{@path = args[:path]}"
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



3
4
5
# File 'lib/mailhogwarts/mailer.rb', line 3

def body
  @body
end

#domainObject (readonly)

Returns the value of attribute domain.



3
4
5
# File 'lib/mailhogwarts/mailer.rb', line 3

def domain
  @domain
end

#messagesObject (readonly)

Returns the value of attribute messages.



3
4
5
# File 'lib/mailhogwarts/mailer.rb', line 3

def messages
  @messages
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/mailhogwarts/mailer.rb', line 3

def path
  @path
end

#urlObject (readonly)

Returns the value of attribute url.



3
4
5
# File 'lib/mailhogwarts/mailer.rb', line 3

def url
  @url
end

Instance Method Details

#[](index) ⇒ Object



25
26
27
# File 'lib/mailhogwarts/mailer.rb', line 25

def [](index)
  @messages[index]
end

#callObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mailhogwarts/mailer.rb', line 9

def call
  response = RestClient::Request.execute method: :get, url: @url, verify_ssl: false
  @body = JSON.parse(response.body)

  @messages = body['items'].map do |message|
    Message.new id: message['ID'],
                created_at: Time.parse(message['Created']).to_i,
                from: message['Raw']['From'],
                to: message['Raw']['To'],
                subject: readable_subject(message['Content']['Headers']['Subject'].join(' ')),
                body: readable_body(message['Content']['Body'])
  end

  @messages.size
end

#countObject



33
34
35
# File 'lib/mailhogwarts/mailer.rb', line 33

def count
  body['count']
end

#idsObject



29
30
31
# File 'lib/mailhogwarts/mailer.rb', line 29

def ids
  @ids = @messages.map(&:id)
end

#select(filters) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mailhogwarts/mailer.rb', line 41

def select(filters)
  messages = @messages
  filtered_messages = []

  (%i[from to subject body] & filters.keys).each do |filter|
    messages.each do |message|
      filtered_messages << message if message.send(filter).include?(filters[filter])
    end

    messages = filtered_messages
    filtered_messages = []
  end

  messages
end

#totalObject



37
38
39
# File 'lib/mailhogwarts/mailer.rb', line 37

def total
  body['total']
end