Sha256: 2cd4bbd9f9991c65bf452a43730e653cc0d7c0bc3024e523c7cb6759854f2363

Contents?: true

Size: 818 Bytes

Versions: 2

Compression:

Stored size: 818 Bytes

Contents

module EmailBills
  module Vendors
    class Base
      attr_reader :from, :checker

      def initialize(from:, checker: EmailBills::GmailChecker.new)
        @from = from
        @checker = checker
      end

      def receipts
        @messages ||= messages.map { |i| receipt_klass.new(i) }
      end

      def total
        receipts.reduce(0.0) do |memo, receipt|
          memo += receipt.value if receipt.value?
          memo
        end
      end

      def title
        mailbox
      end
      
      private

      def messages
        @messages ||= checker.get_since(from, mailbox)
      end

      def mailbox
        fail NotImplementedError
      end

      def receipt_klass
        name = self.class.name.match(/::([^:]+)$/)[1]
        EmailBills::Receipts.const_get name
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
email_bills-0.1.1 lib/email_bills/vendors/base.rb
email_bills-0.1.0 lib/email_bills/vendors/base.rb