Class: DCAS::PaymentBatch

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, batch_id) ⇒ PaymentBatch

Returns a new instance of PaymentBatch.



5
6
7
8
# File 'lib/dcas/payment.rb', line 5

def initialize(client, batch_id)
  @client = client
  @batch_id = batch_id
end

Instance Attribute Details

#batch_idObject (readonly)

Returns the value of attribute batch_id.



10
11
12
# File 'lib/dcas/payment.rb', line 10

def batch_id
  @batch_id
end

Instance Method Details

#<<(payment) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
# File 'lib/dcas/payment.rb', line 16

def <<(payment)
  raise ArgumentError, "payment should be instance of Payment" unless payment.is_a?(Payment)
  type = payments.first.class
  raise ArgumentError, "payment added to a #{type} batch should be a #{type} but was #{payment.class.name}!" if !payments.empty? && !payment.is_a?(type)
  payment.batch = self
  payments << payment
end

#filenameObject



28
29
30
# File 'lib/dcas/payment.rb', line 28

def filename
  "#{@client.company_username}_#{type}.csv"
end

#paymentsObject



12
13
14
# File 'lib/dcas/payment.rb', line 12

def payments
  @payments ||= []
end

#to_csvObject

Generates a payment batch file and returns its contents.



33
34
35
36
37
38
39
40
# File 'lib/dcas/payment.rb', line 33

def to_csv
  FasterCSV.generate do |csv|
    csv << [ 'HD', @client.company_alias, @client.company_username, @client.company_password, 'Check' ]
    payments.each do |payment|
      csv << payment.to_csv_data if payment.batch == self # Safety net in case the same payment was applied to more than one batch. It will only go through in the last batch it was added to.
    end
  end
end

#typeObject



24
25
26
# File 'lib/dcas/payment.rb', line 24

def type
  payments.first.class.name.gsub(/.*::/,'').downcase
end