Class: Mailcrate

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port) ⇒ Mailcrate

Returns a new instance of Mailcrate.



11
12
13
14
# File 'lib/mailcrate.rb', line 11

def initialize(port)
  @port = port
  @mails = []
end

Instance Attribute Details

#mailsObject (readonly)

Returns the value of attribute mails.



9
10
11
# File 'lib/mailcrate.rb', line 9

def mails
  @mails
end

#portObject (readonly)

Returns the value of attribute port.



9
10
11
# File 'lib/mailcrate.rb', line 9

def port
  @port
end

Class Method Details

.used_portsObject



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

def self.used_ports
  @used_ports ||= []
end

Instance Method Details

#start(opts = {}) ⇒ Object

Raises:

  • (Errno::EADDRINUSE)


16
17
18
19
20
21
# File 'lib/mailcrate.rb', line 16

def start(opts = {})
  raise Errno::EADDRINUSE if self.class.used_ports.include?(@port)
  self.class.used_ports << @port
  @service = opts[:service] || TCPServer.new('localhost', @port)
  @thread = Thread.new { accept(@service) }
end

#stopObject



23
24
25
26
27
28
# File 'lib/mailcrate.rb', line 23

def stop
  self.class.used_ports.delete(@port)
  @thread.kill unless @thread.nil?
  @thread.join unless @thread.nil?
  @service.close unless @service.nil? || @service.closed?
end