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(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
# File 'lib/bill_hicks/response_file.rb', line 10

def initialize(options = {})
  requires!(options, :username, :password, :filename)

  @credentials = options.select { |k, v| [:username, :password].include?(k) }
  @filename    = options[:filename]
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(options = {}) ⇒ Object

Return list of ‘855 Purchase Order Acknowledgement’ files

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :username (String)

    required

  • :password (String)

    required



20
21
22
23
24
25
26
27
# File 'lib/bill_hicks/response_file.rb', line 20

def self.all(options = {})
  requires!(options, :username, :password)

  Base.connect(options) do |ftp|
    ftp.chdir(BillHicks.config.full_response_dir)
    ftp.nlst("*.txt")
  end
end

Instance Method Details

#ack?Boolean

Is the file a ‘855 Purchase Order Acknowledgement’?

Returns:

  • (Boolean)


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

def ack?
  content.start_with?("ACK")
end

#asn?Boolean

Is the file a ‘856 Advance Shipping Notice’?

Returns:

  • (Boolean)


35
36
37
# File 'lib/bill_hicks/response_file.rb', line 35

def asn?
  content.start_with?("ASN")
end

#contentObject

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



40
41
42
43
44
45
46
# File 'lib/bill_hicks/response_file.rb', line 40

def content
  return @content if @content
  connect(@credentials) do |ftp|
    ftp.chdir(BillHicks.config.full_response_dir)
    @content = ftp.gettextfile(@filename, nil)
  end
end

#to_jsonObject

Convert to easily readable key-value pairs



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bill_hicks/response_file.rb', line 49

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