Class: MailCapture::Inbox

Inherits:
Object
  • Object
show all
Defined in:
lib/mailcapture/inbox.rb

Overview

A scoped handle for a single capture inbox (tag). Create one with Client#inbox.

Keeps test code clean by binding the tag once:

inbox = mc.inbox('signup')
inbox.clear
MyApp.register(inbox.address)
email = inbox.wait_for(timeout: 15)
expect(email.otp).to match(/\A\d{6}\z/)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, tag) ⇒ Inbox

Returns a new instance of Inbox.



16
17
18
19
# File 'lib/mailcapture/inbox.rb', line 16

def initialize(client, tag)
  @client = client
  @tag    = tag
end

Instance Attribute Details

#tagString (readonly)

Returns the tag this inbox is scoped to.

Returns:

  • (String)

    the tag this inbox is scoped to



14
15
16
# File 'lib/mailcapture/inbox.rb', line 14

def tag
  @tag
end

Instance Method Details

#addressObject

The full capture email address for this inbox, e.g. “alice-signup@mailcapture.app”.

Requires ping to have been called first, or :username set in the constructor.

Raises:

  • (RuntimeError)

    if the username is not yet known



27
28
29
# File 'lib/mailcapture/inbox.rb', line 27

def address
  @client.address(tag)
end

#clearObject

Delete all captures. Call before each test for a clean starting state.

before(:each) { inbox.clear }


44
45
46
# File 'lib/mailcapture/inbox.rb', line 44

def clear
  @client.delete(tag)
end

#list(limit: nil, after: nil) ⇒ Object

List recent captures. See Client#list.



37
38
39
# File 'lib/mailcapture/inbox.rb', line 37

def list(limit: nil, after: nil)
  @client.list(tag: tag, limit: limit, after: after)
end

#to_sObject Also known as: inspect



48
49
50
# File 'lib/mailcapture/inbox.rb', line 48

def to_s
  "#<MailCapture::Inbox tag=#{tag.inspect}>"
end

#wait_for(timeout: 30, poll_timeout: 10, after: nil) ⇒ Object

Wait for an email to arrive. See Client#wait_for.



32
33
34
# File 'lib/mailcapture/inbox.rb', line 32

def wait_for(timeout: 30, poll_timeout: 10, after: nil)
  @client.wait_for(tag, timeout: timeout, poll_timeout: poll_timeout, after: after)
end