Class: AnPostReturn::SFTP::TrackingParser
- Inherits:
-
Object
- Object
- AnPostReturn::SFTP::TrackingParser
- Defined in:
- lib/an_post_return/sftp/tracking_parser.rb
Constant Summary collapse
- REQUIRED_FIELDS =
%w[tracking_number status timestamp].freeze
- TIMESTAMP_FORMAT =
"%Y-%m-%d %H:%M:%S".freeze
Class Method Summary collapse
-
.parse(file) ⇒ Array<Hash>
Parse tracking data from a CSV file.
Instance Method Summary collapse
-
#parse(file_path) ⇒ Hash
Parse tracking data from a text file.
Class Method Details
.parse(file) ⇒ Array<Hash>
Parse tracking data from a CSV file
15 16 17 |
# File 'lib/an_post_return/sftp/tracking_parser.rb', line 15 def self.parse(file) new.parse(file) end |
Instance Method Details
#parse(file_path) ⇒ Hash
Parse tracking data from a text file
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/an_post_return/sftp/tracking_parser.rb', line 24 def parse(file_path) validate_file!(file_path) result = { header: nil, data: [], footer: nil } File.foreach(file_path) do |line| line = line.strip next if line.empty? # Determine delimiter and parse fields delimiter = line.include?("+") ? "+" : "," fields = CSV.parse_line(line, col_sep: delimiter, quote_char: '"') record_type = fields[0] case record_type when "00" result[:header] = parse_header(fields) when "01" result[:data] << parse_data_record(fields) when "99" result[:footer] = (fields) else next # Skip unknown record types end end result rescue CSV::MalformedCSVError => e raise ParserError, "Invalid file format: #{e.}" rescue => e raise ParserError, "Error parsing tracking data: #{e.}" end |