Class: FakeSMTPd::MessageStore

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message_dir) ⇒ MessageStore

Returns a new instance of MessageStore.



366
367
368
# File 'lib/fakesmtpd/server.rb', line 366

def initialize(message_dir)
  @message_dir = message_dir
end

Instance Attribute Details

#message_dirObject (readonly)

Returns the value of attribute message_dir.



364
365
366
# File 'lib/fakesmtpd/server.rb', line 364

def message_dir
  @message_dir
end

Instance Method Details

#[](message_id) ⇒ Object



390
391
392
393
394
395
396
# File 'lib/fakesmtpd/server.rb', line 390

def [](message_id)
  message_file = "#{message_dir}/fakesmtpd-client-#{message_id}.json"
  if File.exists?(message_file)
    return message_file
  end
  nil
end

#clearObject



398
399
400
# File 'lib/fakesmtpd/server.rb', line 398

def clear
  FileUtils.rm_f(message_files)
end

#store(message_id, from, recipients, body) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/fakesmtpd/server.rb', line 370

def store(message_id, from, recipients, body)
  outfile = File.join(message_dir, "fakesmtpd-client-#{message_id}.json")
  File.open(outfile, 'w:UTF-8') do |f|
    f.write JSON.pretty_generate(
      message_id: message_id,
      from: from,
      recipients: recipients,
      body: body,
    )
  end
  outfile
end

#to_hashObject



383
384
385
386
387
388
# File 'lib/fakesmtpd/server.rb', line 383

def to_hash
  message_files.each_with_object({}) do |filename, h|
    message_id = File.basename(filename, '.json').gsub(/[^0-9]+/, '')
    h[message_id] = File.expand_path(filename)
  end
end