Class: AnPostReturn::Tracker
- Inherits:
-
Object
- Object
- AnPostReturn::Tracker
- Defined in:
- lib/an_post_return/tracker.rb
Instance Attribute Summary collapse
-
#sftp_client ⇒ Object
readonly
Initialize a new Tracking resource.
Instance Method Summary collapse
- #disconnect ⇒ Object
-
#initialize ⇒ Tracker
constructor
A new instance of Tracker.
-
#track_from(last_filename) {|data| ... } ⇒ void
Get tracking data from a file, incrementing file number if needed.
-
#track_with_account_number(account_number, last: 0) {|data| ... } ⇒ void
Track files with a specific account number.
Constructor Details
#initialize ⇒ Tracker
Returns a new instance of Tracker.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/an_post_return/tracker.rb', line 9 def initialize @config = AnPostReturn.configuration @sftp_client = SFTP::Client.new( @config.sftp_config[:host], @config.sftp_config[:username], @config.sftp_config[:password], @config.proxy_config, ) raise ArgumentError, "SFTP configuration is not set" unless @config.sftp_configured? end |
Instance Attribute Details
#sftp_client ⇒ Object (readonly)
Initialize a new Tracking resource
8 9 10 |
# File 'lib/an_post_return/tracker.rb', line 8 def sftp_client @sftp_client end |
Instance Method Details
#disconnect ⇒ Object
85 86 87 88 89 90 |
# File 'lib/an_post_return/tracker.rb', line 85 def disconnect return false unless @sftp_client.connected? @sftp_client.disconnect true end |
#track_from(last_filename) {|data| ... } ⇒ void
This method returns an undefined value.
Get tracking data from a file, incrementing file number if needed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/an_post_return/tracker.rb', line 52 def track_from(last_filename, &block) return unless block_given? @sftp_client.connect unless @sftp_client.connected? # example file name: CDT99999999SSSSS.txt # Where: # • CDT is to prefix each file (Customer Data Tracking). # • 99999999 is the An Post Customer Account Number. # • SSSSS is a sequence number starting at 1 and incrementing by 1 for every file sent, with leading zeros. # • .txt is the standard file extension. # # extract the customer account number, sequence number and extension customer_account_number = last_filename.match(/^cdt(\d+)([0-9]{5})\.txt$/)[1] sequence_number = last_filename.match(/^cdt(\d+)([0-9]{5})\.txt$/)[2] extension = ".txt" while true # increment the sequence number sequence_number = sequence_number.to_i + 1 # format the new filename next_filename = "cdt#{customer_account_number}#{sequence_number.to_s.rjust(5, "0")}#{extension}" @sftp_client.read_file(next_filename) do |tracking_file| data = SFTP::TrackingParser.parse(tracking_file) yield next_filename, data end end rescue SFTP::FileNotFoundError # If file not found, we're done return end |
#track_with_account_number(account_number, last: 0) {|data| ... } ⇒ void
This method returns an undefined value.
Track files with a specific account number
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/an_post_return/tracker.rb', line 29 def track_with_account_number(account_number, last: 0, &block) @sftp_client.connect unless @sftp_client.connected? # pad the account number with leading zeros to 8 digits account_number = account_number.to_s.rjust(8, "0") file = if last.zero? @sftp_client.list_files(@config.sftp_config[:remote_path], "cdt#{account_number}*").first else @sftp_client.list_files(@config.sftp_config[:remote_path], "cdt#{account_number}*")[-(last + 1)] end track_from(file.name, &block) if file end |