Class: AnPostReturn::Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/an_post_return/tracker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTracker

Returns a new instance of Tracker.

Raises:



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_clientObject (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

#disconnectObject



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

Parameters:

  • Base last filename processed (e.g. "cdt0370132115864.txt")

  • Existing SFTP client to use

Yield Parameters:

  • data (Hash)

    Parsed tracking data containing :header, :data, and :footer

Raises:

  • if SFTP connection fails

  • if file operations fail

  • if parsing fails



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
   = 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

Parameters:

  • The account number to track

  • (defaults to: 0)

    The number of files to track

Yield Parameters:

  • data (Hash)

    Parsed tracking data containing :header, :data, and :footer

Raises:

  • if SFTP connection fails

  • if file operations fail



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/an_post_return/tracker.rb', line 29

def (, last: 0, &block)
  @sftp_client.connect unless @sftp_client.connected?

  # pad the account number with leading zeros to 8 digits
   = .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