Class: BillHicks::ResponseFile
- Defined in:
- lib/bill_hicks/response_file.rb
Instance Attribute Summary collapse
-
#credentials ⇒ Object
readonly
Returns the value of attribute credentials.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
Class Method Summary collapse
-
.all(options = {}) ⇒ Object
Return list of ‘855 Purchase Order Acknowledgement’ files.
Instance Method Summary collapse
-
#ack? ⇒ Boolean
Is the file a ‘855 Purchase Order Acknowledgement’?.
-
#asn? ⇒ Boolean
Is the file a ‘856 Advance Shipping Notice’?.
-
#content ⇒ Object
Use ‘#gettextfile’ to read file contents as a string.
-
#initialize(options = {}) ⇒ ResponseFile
constructor
A new instance of ResponseFile.
-
#to_json ⇒ Object
Convert to easily readable key-value pairs.
Methods inherited from Base
Constructor Details
#initialize(options = {}) ⇒ ResponseFile
Returns a new instance of ResponseFile.
10 11 12 13 14 15 |
# File 'lib/bill_hicks/response_file.rb', line 10 def initialize( = {}) requires!(, :username, :password, :filename) @credentials = .select { |k, v| [:username, :password].include?(k) } @filename = [:filename] end |
Instance Attribute Details
#credentials ⇒ Object (readonly)
Returns the value of attribute credentials.
4 5 6 |
# File 'lib/bill_hicks/response_file.rb', line 4 def credentials @credentials end |
#filename ⇒ Object (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
20 21 22 23 24 25 26 27 |
# File 'lib/bill_hicks/response_file.rb', line 20 def self.all( = {}) requires!(, :username, :password) Base.connect() 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’?
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’?
35 36 37 |
# File 'lib/bill_hicks/response_file.rb', line 35 def asn? content.start_with?("ASN") end |
#content ⇒ Object
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_json ⇒ Object
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 |