dcas-ruby 0.3.1 → 0.3.2
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.
- data/VERSION +1 -1
- data/dcas-ruby.gemspec +1 -1
- data/lib/dcas.rb +40 -25
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.2
|
data/dcas-ruby.gemspec
CHANGED
data/lib/dcas.rb
CHANGED
@@ -50,34 +50,43 @@ module DCAS
|
|
50
50
|
# #submit_failed!(filename)
|
51
51
|
# If a lock_object is supplied, the method will mark it as failed and return false instead of raising an error, in case of failure.
|
52
52
|
def submit_payments_file!(filename, lock_object=nil)
|
53
|
-
shortname = filename.
|
53
|
+
shortname = filename.match(/[\\\/]([^\\\/]+)$/)[1]
|
54
54
|
if lock_object && lock_object.submit_locked?(shortname)
|
55
|
-
raise RuntimeError, "Submit for #{shortname} is locked!"
|
55
|
+
# raise RuntimeError, "Submit for #{shortname} is locked!"
|
56
|
+
return nil
|
56
57
|
else
|
57
58
|
lock_object.submit_lock!(shortname) if lock_object
|
58
59
|
with_ftp do |ftp|
|
59
60
|
# 1) Create the STAGING folder if it's not already there.
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
61
|
+
begin
|
62
|
+
ftp.mkdir(DCAS::STAGING_BUCKET) unless ftp.nlst.include?(DCAS::STAGING_BUCKET)
|
63
|
+
ftp.chdir(DCAS::STAGING_BUCKET)
|
64
|
+
# 2) Delete the same filename from the STAGING folder if one exists.
|
65
|
+
ftp.delete(shortname) if ftp.nlst.include?(shortname)
|
66
|
+
# 3) Upload the file into the STAGING folder.
|
67
|
+
puts "Uploading #{filename} as #{shortname}..."
|
68
|
+
ftp.put(filename, shortname)
|
69
|
+
rescue Object
|
70
|
+
false
|
71
|
+
end && begin
|
72
|
+
# 4) If we're still connected, check the file size of the file, then move it out of STAGING and mark file as completed.
|
73
|
+
if ftp.nlst.include?(shortname) && ftp.size(shortname) == File.size(filename)
|
74
|
+
ftp.rename(shortname, "../#{outgoing_bucket}/#{shortname}") unless testing || outgoing_bucket == DCAS::STAGING_BUCKET
|
75
|
+
lock_object.submit_finished!(shortname) if lock_object
|
76
|
+
true
|
74
77
|
else
|
75
|
-
|
78
|
+
if lock_object
|
79
|
+
lock_object.submit_failed!(shortname)
|
80
|
+
false
|
81
|
+
else
|
82
|
+
raise RuntimeError, "FAILED uploading `#{filename}' - incomplete or unsuccessful upload. Please try again."
|
83
|
+
end
|
76
84
|
end
|
85
|
+
rescue Object
|
86
|
+
false
|
77
87
|
end
|
78
88
|
end
|
79
89
|
end
|
80
|
-
true
|
81
90
|
end
|
82
91
|
|
83
92
|
# Writes one batch to file and submits it to the DCAS outgoing payments bucket.
|
@@ -91,7 +100,7 @@ module DCAS
|
|
91
100
|
File.makedirs(cache_location)
|
92
101
|
filename = cache_location + "/" + batch.filename
|
93
102
|
# 1) Create the file locally.
|
94
|
-
File.open(filename) {|f| f << batch.to_csv }
|
103
|
+
File.open(filename, 'w') {|f| f << batch.to_csv }
|
95
104
|
# 2) Upload it to the DCAS outgoing payments bucket.
|
96
105
|
submit_payments_file!(filename, lock_object)
|
97
106
|
end
|
@@ -154,13 +163,19 @@ module DCAS
|
|
154
163
|
# This allows all functionality to share the same connection, then log out after all work is finished.
|
155
164
|
def with_ftp(&block)
|
156
165
|
@inside_with_ftp = @inside_with_ftp.to_i + 1
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
166
|
+
result = false
|
167
|
+
begin
|
168
|
+
result = if block.arity == 1
|
169
|
+
yield ftp_connection
|
170
|
+
else
|
171
|
+
yield
|
172
|
+
end
|
173
|
+
rescue Object
|
174
|
+
ensure
|
175
|
+
@inside_with_ftp -= 1
|
176
|
+
ftp_done
|
161
177
|
end
|
162
|
-
|
163
|
-
ftp_done
|
178
|
+
result
|
164
179
|
end
|
165
180
|
def ftp_done
|
166
181
|
close_ftp if @inside_with_ftp.to_i == 0
|