class SportsmansSupply::Invoice

Public Class Methods

all(options = {}) click to toggle source
# File lib/sportsmans-supply/invoice.rb, line 9
def self.all(options = {})
  requires!(options, :username, :password)
  new(options).all
end
new(options = {}) click to toggle source
# File lib/sportsmans-supply/invoice.rb, line 4
def initialize(options = {})
  requires!(options, :username, :password)
  @options = options
end

Public Instance Methods

all() click to toggle source
# File lib/sportsmans-supply/invoice.rb, line 14
def all
  invoice_filename = connect(@options) { |ftp| ftp.nlst('/invoices/invoices*.csv').last }&.split('/')&.last

  return [] if invoice_filename.nil?

  invoice_file = get_file(invoice_filename, 'invoices')
  invoice_data = []

  File.open(invoice_file).each_with_index do |row, i|
    row = row.split(",").map(&:strip)

    if i == 0
      @headers = row.map(&:downcase)
      next
    end

    invoice_data << {
      po_number:      row[@headers.index('order number')],
      invoice_number: row[@headers.index('invoice number')]
    }
  end

  invoice_file.close
  invoice_file.unlink

  invoice_data
end