class BillHicks::ResponseFile

Attributes

credentials[R]
filename[R]

Public Class Methods

all(ftp) click to toggle source

Return list of '855 Purchase Order Acknowledgement' files @option options [String] :username required @option options [String] :password required

# File lib/bill_hicks/response_file.rb, line 21
def self.all(ftp)
  ftp.nlst("*.txt")
end
new(ftp, options = {}) click to toggle source

@option options [String] :username required @option options [String] :password required @option options [String] :filename required

# File lib/bill_hicks/response_file.rb, line 10
def initialize(ftp, options = {})
  requires!(options, :username, :password, :filename)

  @credentials  = options.select { |k, v| [:username, :password].include?(k) }
  @filename     = options[:filename]
  @ftp          = ftp
end

Public Instance Methods

ack?() click to toggle source

Is the file a '855 Purchase Order Acknowledgement'?

# File lib/bill_hicks/response_file.rb, line 26
def ack?
  filename.downcase.start_with?("ack")
end
asn?() click to toggle source

Is the file a '856 Advance Shipping Notice'?

# File lib/bill_hicks/response_file.rb, line 31
def asn?
  filename.downcase.start_with?("asn")
end
content() click to toggle source

Use '#gettextfile' to read file contents as a string

# File lib/bill_hicks/response_file.rb, line 36
def content
  return @content if @content

  @content = @ftp.gettextfile(@filename, nil)

  @content
end
to_json() click to toggle source

Convert to easily readable key-value pairs

# File lib/bill_hicks/response_file.rb, line 45
def to_json
  if corrupt_asn?
    CSV.parse(content.gsub("Price|", ""), headers: true, col_sep: "|").
      map { |x| x.to_h }.
      group_by { |x| x["PO Number"] }
  else
    CSV.parse(content, headers: true, col_sep: "|").
      map { |x| x.to_h }.
      group_by { |x| x["PO Number"] }
  end
end

Private Instance Methods

corrupt_asn?() click to toggle source
# File lib/bill_hicks/response_file.rb, line 59
def corrupt_asn?
  return false if ack?
  lines = content.lines.map(&:chomp)
  if lines[0].split("|").length != lines[1].split("|").length
    puts "Notice: ASN file is malformed! (#{filename})"
    true
  else
    false
  end
end