Class: BillHicks::ResponseFile

Inherits:
Base
  • Object
show all
Defined in:
lib/bill_hicks/response_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connect

Constructor Details

#initialize(ftp, options = {}) ⇒ ResponseFile

Returns a new instance of ResponseFile.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :username (String)

    required

  • :password (String)

    required

  • :filename (String)

    required



10
11
12
13
14
15
16
# 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

Instance Attribute Details

#credentialsObject (readonly)

Returns the value of attribute credentials.



4
5
6
# File 'lib/bill_hicks/response_file.rb', line 4

def credentials
  @credentials
end

#filenameObject (readonly)

Returns the value of attribute filename.



5
6
7
# File 'lib/bill_hicks/response_file.rb', line 5

def filename
  @filename
end

Class Method Details

.all(ftp) ⇒ Object

Return list of ‘855 Purchase Order Acknowledgement’ files

Parameters:

  • options (Hash)

    a customizable set of options



21
22
23
# File 'lib/bill_hicks/response_file.rb', line 21

def self.all(ftp)
  ftp.nlst("*.txt")
end

Instance Method Details

#ack?Boolean

Is the file a ‘855 Purchase Order Acknowledgement’?

Returns:

  • (Boolean)


26
27
28
# File 'lib/bill_hicks/response_file.rb', line 26

def ack?
  filename.downcase.start_with?("ack")
end

#asn?Boolean

Is the file a ‘856 Advance Shipping Notice’?

Returns:

  • (Boolean)


31
32
33
# File 'lib/bill_hicks/response_file.rb', line 31

def asn?
  filename.downcase.start_with?("asn")
end

#contentObject

Use ‘#gettextfile’ to read file contents as a string



36
37
38
39
40
41
42
# File 'lib/bill_hicks/response_file.rb', line 36

def content
  return @content if @content

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

  @content
end

#to_jsonObject

Convert to easily readable key-value pairs



45
46
47
48
49
50
51
52
53
54
55
# 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