Commit cfcc872

mokha <mokha@cisco.com>
2019-04-16 16:20:55
return mail objects instead of filenames and allow search by subject
1 parent a8e3360
Changed files (2)
lib
minbox
spec
lib/minbox/inbox.rb
@@ -23,26 +23,30 @@ module Minbox
 
     def emails(count: 0)
       wait_until { |x| x.count >= count } if count > 0
-
-      @emails.keys
+      @emails.values
     end
 
     def wait_until(seconds: 5, wait: 0.1)
       iterations = (seconds / wait).to_i
       iterations.times do
-        return true if yield(self)
+        result = yield(self)
+        return result if result
+
         sleep wait
       end
-      false
+      nil
     end
 
     def wait_until!(*args, &block)
       raise "timeout: expired. #{args}" unless wait_until(*args, &block)
     end
 
-    def open(id)
-      wait_until { @emails[id] }
-      @emails[id]
+    def open(subject:)
+      wait_until do
+        emails.find do |email|
+          email.subject == subject
+        end
+      end
     end
 
     def empty!
spec/minbox/inbox_spec.rb
@@ -34,14 +34,14 @@ RSpec.describe Minbox::Inbox do
   end
 
   describe "#emails" do
-    specify { expect(subject.emails(count: 2)).to match_array(['1.eml', '2.eml']) }
+    specify { expect(subject.emails(count: 2).map(&:subject)).to match_array(['goodbye world', 'hello world']) }
   end
 
   describe "#wait_until!" do
     context "when the condition is satisfied" do
       before { subject.wait_until! { |x| x.count == 2 } }
 
-      specify { expect(subject.emails).to match_array(['1.eml', '2.eml']) }
+      specify { expect(subject.emails(count: 2).map(&:subject)).to match_array(['goodbye world', 'hello world']) }
     end
 
     context "when the condition is not satisfied" do
@@ -54,14 +54,12 @@ RSpec.describe Minbox::Inbox do
   end
 
   describe "#open" do
-    context "when opening an email in the inbox" do
-      let(:result) { subject.open('2.eml') }
-
-      specify { expect(result.subject).to eql('goodbye world') }
+    context "when opening an email by subject" do
+      specify { expect(subject.open(subject: 'goodbye world').subject).to eql('goodbye world') }
     end
 
     context "when opening an email not in the inbox" do
-      let(:result) { subject.open("#{SecureRandom.uuid}.eml") }
+      let(:result) { subject.open(subject: SecureRandom.uuid) }
 
       specify { expect(result).to be_nil }
     end