lib/embratel/phone_bill.rb in embratel-1.0.0 vs lib/embratel/phone_bill.rb in embratel-1.1.0
- old
+ new
@@ -1,40 +1,21 @@
module Embratel
class PhoneBill
+ attr_reader :payables
+
def initialize(path)
- begin
- @csv = CSV.read(path, { :skip_blanks => true })
- rescue Errno::ENOENT
- raise
- rescue Errno::EISDIR
- raise
- rescue CSV::MalformedCSVError
- raise
- else
- raise InvalidPhoneBillFileError if (invalid_rows? || non_csv?(path))
- end
+ @payables = CSVParser.parse(path)
end
def calls
- @calls ||= @csv.inject([]) do |calls, row|
- call = Call.new(row)
- call.valid? ? (calls << call) : calls
- end
+ @calls ||= @payables.select(&:call?)
end
- def total
- @total ||= calls.inject(0) { |sum, call| sum += call.cost.to_f }
+ def fees
+ @calls ||= @payables.select(&:fee?)
end
- private
-
- def invalid_rows?
- csv = @csv.dup
- 3.times { csv.shift }
- csv.any? { |row| !Call.new(row).valid? }
- end
-
- def non_csv?(path)
- File.extname(path) != '.csv'
+ def total
+ @total ||= payables.inject(0) { |sum, payable| sum += payable.cost.to_f }
end
end
end