an_post_return 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/an_post_return/sftp/client.rb +17 -15
- data/lib/an_post_return/tracker.rb +44 -48
- data/lib/an_post_return/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6840e99cecb0feeedfe52e687f3cf8d72fa2db18361da0ba6a19da8847d9e4e3
|
4
|
+
data.tar.gz: 2c03b5069fabfb52bed2b91814fce2c2edc3be88f18d59f7889252e0870f7670
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f565a7a6619d26ad656442e72b8c251cc8ae00ff7ae686ad04cf06d58436509ecebf7b50e235c3e82614e422d1c69cb2b2a44ebdea973643f1e28dc94be1024
|
7
|
+
data.tar.gz: afe4adbc393b86837b1b6e48b469b05b14c304dac2b8babf5c99ebc39b81e4b9385f30bbcfdd9607781628ba98641cd5e136e043e8f390c7a280ae5b03244263
|
@@ -9,7 +9,9 @@ module AnPostReturn
|
|
9
9
|
module SFTP
|
10
10
|
class Client
|
11
11
|
# SFTP connection configuration
|
12
|
-
attr_reader :host, :username, :password, :proxy_config
|
12
|
+
attr_reader :host, :username, :password, :proxy_config, :connected
|
13
|
+
|
14
|
+
alias connected? connected
|
13
15
|
|
14
16
|
# Initialize a new SFTP client
|
15
17
|
#
|
@@ -34,7 +36,12 @@ module AnPostReturn
|
|
34
36
|
# @return [Boolean] true if connection successful, false otherwise
|
35
37
|
# @raise [AnPostReturn::SFTP::ConnectionError] if connection fails
|
36
38
|
def connect
|
37
|
-
|
39
|
+
return true if @connected
|
40
|
+
|
41
|
+
@ssh_session = start_ssh_session
|
42
|
+
@sftp_client = Net::SFTP::Session.new(@ssh_session)
|
43
|
+
@sftp_client.connect!
|
44
|
+
|
38
45
|
@connected = true
|
39
46
|
true
|
40
47
|
rescue Net::SSH::Exception => e
|
@@ -47,8 +54,8 @@ module AnPostReturn
|
|
47
54
|
def disconnect
|
48
55
|
return unless @connected
|
49
56
|
|
50
|
-
sftp_client.close_channel
|
51
|
-
ssh_session.close
|
57
|
+
@sftp_client.close_channel
|
58
|
+
@ssh_session.close
|
52
59
|
@connected = false
|
53
60
|
end
|
54
61
|
|
@@ -63,7 +70,7 @@ module AnPostReturn
|
|
63
70
|
temp_file = Tempfile.new(["sftp", File.extname(remote_path)])
|
64
71
|
|
65
72
|
begin
|
66
|
-
sftp_client.download!(remote_path, temp_file.path)
|
73
|
+
@sftp_client.download!(remote_path, temp_file.path)
|
67
74
|
block_given? ? yield(temp_file) : temp_file
|
68
75
|
rescue Net::SFTP::StatusException => e
|
69
76
|
if e.message.include?("no such file")
|
@@ -91,9 +98,9 @@ module AnPostReturn
|
|
91
98
|
|
92
99
|
begin
|
93
100
|
if glob_pattern
|
94
|
-
sftp_client.dir.glob(remote_path, glob_pattern) { |entry| entries << entry }
|
101
|
+
@sftp_client.dir.glob(remote_path, glob_pattern) { |entry| entries << entry }
|
95
102
|
else
|
96
|
-
sftp_client.dir.foreach(remote_path) { |entry| entries << entry }
|
103
|
+
@sftp_client.dir.foreach(remote_path) { |entry| entries << entry }
|
97
104
|
end
|
98
105
|
entries
|
99
106
|
rescue Net::SFTP::StatusException => e
|
@@ -103,25 +110,20 @@ module AnPostReturn
|
|
103
110
|
|
104
111
|
private
|
105
112
|
|
106
|
-
def sftp_client
|
107
|
-
@sftp_client ||= Net::SFTP::Session.new(ssh_session)
|
108
|
-
end
|
109
|
-
|
110
|
-
def ssh_session
|
111
|
-
return @ssh_session if @ssh_session
|
112
113
|
|
114
|
+
def start_ssh_session
|
113
115
|
ssh_options = { password: password, auth_methods: ["password"] }
|
114
116
|
|
115
117
|
if proxy_config
|
116
118
|
ssh_options[:proxy] = Net::SSH::Proxy::HTTP.new(
|
117
119
|
proxy_config[:host],
|
118
120
|
proxy_config[:port],
|
119
|
-
user: proxy_config[:
|
121
|
+
user: proxy_config[:user],
|
120
122
|
password: proxy_config[:password],
|
121
123
|
)
|
122
124
|
end
|
123
125
|
|
124
|
-
|
126
|
+
Net::SSH.start(host, username, ssh_options)
|
125
127
|
end
|
126
128
|
|
127
129
|
def ensure_connected
|
@@ -5,8 +5,16 @@ require_relative "configuration"
|
|
5
5
|
module AnPostReturn
|
6
6
|
class Tracker
|
7
7
|
# Initialize a new Tracking resource
|
8
|
+
attr_reader :sftp_client
|
8
9
|
def initialize
|
9
10
|
@config = AnPostReturn.configuration
|
11
|
+
@sftp_client =
|
12
|
+
SFTP::Client.new(
|
13
|
+
@config.sftp_config[:host],
|
14
|
+
@config.sftp_config[:username],
|
15
|
+
@config.sftp_config[:password],
|
16
|
+
@config.proxy_config,
|
17
|
+
)
|
10
18
|
raise ArgumentError, "SFTP configuration is not set" unless @config.sftp_configured?
|
11
19
|
end
|
12
20
|
|
@@ -19,17 +27,17 @@ module AnPostReturn
|
|
19
27
|
# @raise [AnPostReturn::SFTP::ConnectionError] if SFTP connection fails
|
20
28
|
# @raise [AnPostReturn::SFTP::FileError] if file operations fail
|
21
29
|
def track_with_account_number(account_number, last: 0, &block)
|
30
|
+
@sftp_client.connect unless @sftp_client.connected?
|
31
|
+
|
22
32
|
# pad the account number with leading zeros to 8 digits
|
23
33
|
account_number = account_number.to_s.rjust(8, "0")
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
track_from(file.name, sftp_client, &block) if file
|
32
|
-
end
|
34
|
+
file =
|
35
|
+
if last.zero?
|
36
|
+
@sftp_client.list_files(@config.sftp_config[:remote_path], "cdt#{account_number}*").first
|
37
|
+
else
|
38
|
+
@sftp_client.list_files(@config.sftp_config[:remote_path], "cdt#{account_number}*")[-(last + 1)]
|
39
|
+
end
|
40
|
+
track_from(file.name, &block) if file
|
33
41
|
end
|
34
42
|
|
35
43
|
# Get tracking data from a file, incrementing file number if needed
|
@@ -41,32 +49,32 @@ module AnPostReturn
|
|
41
49
|
# @raise [AnPostReturn::SFTP::ConnectionError] if SFTP connection fails
|
42
50
|
# @raise [AnPostReturn::SFTP::FileError] if file operations fail
|
43
51
|
# @raise [AnPostReturn::ParserError] if parsing fails
|
44
|
-
def track_from(last_filename,
|
52
|
+
def track_from(last_filename, &block)
|
45
53
|
return unless block_given?
|
46
54
|
|
47
|
-
|
48
|
-
# example file name: CDT99999999SSSSS.txt
|
49
|
-
# Where:
|
50
|
-
# • CDT is to prefix each file (Customer Data Tracking).
|
51
|
-
# • 99999999 is the An Post Customer Account Number.
|
52
|
-
# • SSSSS is a sequence number starting at 1 and incrementing by 1 for every file sent, with leading zeros.
|
53
|
-
# • .txt is the standard file extension.
|
54
|
-
#
|
55
|
-
# extract the customer account number, sequence number and extension
|
56
|
-
customer_account_number = last_filename.match(/^cdt(\d+)([0-9]{5})\.txt$/)[1]
|
57
|
-
sequence_number = last_filename.match(/^cdt(\d+)([0-9]{5})\.txt$/)[2]
|
58
|
-
extension = ".txt"
|
55
|
+
@sftp_client.connect unless @sftp_client.connected?
|
59
56
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
57
|
+
# example file name: CDT99999999SSSSS.txt
|
58
|
+
# Where:
|
59
|
+
# • CDT is to prefix each file (Customer Data Tracking).
|
60
|
+
# • 99999999 is the An Post Customer Account Number.
|
61
|
+
# • SSSSS is a sequence number starting at 1 and incrementing by 1 for every file sent, with leading zeros.
|
62
|
+
# • .txt is the standard file extension.
|
63
|
+
#
|
64
|
+
# extract the customer account number, sequence number and extension
|
65
|
+
customer_account_number = last_filename.match(/^cdt(\d+)([0-9]{5})\.txt$/)[1]
|
66
|
+
sequence_number = last_filename.match(/^cdt(\d+)([0-9]{5})\.txt$/)[2]
|
67
|
+
extension = ".txt"
|
65
68
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
69
|
+
while true
|
70
|
+
# increment the sequence number
|
71
|
+
sequence_number = sequence_number.to_i + 1
|
72
|
+
# format the new filename
|
73
|
+
next_filename = "cdt#{customer_account_number}#{sequence_number.to_s.rjust(5, "0")}#{extension}"
|
74
|
+
|
75
|
+
@sftp_client.read_file(next_filename) do |tracking_file|
|
76
|
+
data = SFTP::TrackingParser.parse(tracking_file)
|
77
|
+
yield next_filename, data
|
70
78
|
end
|
71
79
|
end
|
72
80
|
rescue SFTP::FileNotFoundError
|
@@ -74,24 +82,12 @@ module AnPostReturn
|
|
74
82
|
return
|
75
83
|
end
|
76
84
|
|
77
|
-
|
85
|
+
def disconnect
|
86
|
+
return false unless @sftp_client.connected?
|
78
87
|
|
79
|
-
|
80
|
-
|
81
|
-
client = existing_sftp_client
|
82
|
-
else
|
83
|
-
client =
|
84
|
-
SFTP::Client.new(
|
85
|
-
@config.sftp_config[:host],
|
86
|
-
@config.sftp_config[:username],
|
87
|
-
@config.sftp_config[:password],
|
88
|
-
@config.proxy_config,
|
89
|
-
)
|
90
|
-
client.connect
|
91
|
-
end
|
92
|
-
yield client
|
93
|
-
ensure
|
94
|
-
client&.disconnect unless existing_sftp_client
|
88
|
+
@sftp_client.disconnect
|
89
|
+
true
|
95
90
|
end
|
91
|
+
|
96
92
|
end
|
97
93
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: an_post_return
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Chong
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-05-
|
11
|
+
date: 2025-05-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: x25519
|