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.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/dcas-ruby.gemspec +1 -1
  3. data/lib/dcas.rb +40 -25
  4. metadata +1 -1
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.3.2
data/dcas-ruby.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{dcas-ruby}
8
- s.version = "0.3.1"
8
+ s.version = "0.3.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["BehindLogic"]
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.gsub(/.*[\\\/][^\\\/]+$/,'')
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
- ftp.mkdir(DCAS::STAGING_BUCKET) unless ftp.nlst.include?(DCAS::STAGING_BUCKET)
61
- ftp.chdir(DCAS::STAGING_BUCKET)
62
- # 2) Delete the same filename from the STAGING folder if one exists.
63
- ftp.delete(shortname) if ftp.nlst.include?(shortname)
64
- # 3) Upload the file into the STAGING folder.
65
- ftp.put(filename, shortname)
66
- # 4) If we're still connected, check the file size of the file, then move it out of STAGING and mark file as completed.
67
- if ftp.nlst.include?(shortname) && ftp.size(shortname) == File.size(filename)
68
- ftp.rename(shortname, "../#{outgoing_bucket}/#{shortname}") unless testing || outgoing_bucket == DCAS::STAGING_BUCKET
69
- lock_object.submit_finished!(shortname) if lock_object
70
- else
71
- if lock_object
72
- lock_object.submit_failed!(shortname)
73
- return false
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
- raise RuntimeError, "FAILED uploading `#{filename}' - incomplete or unsuccessful upload. Please try again."
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
- if block.arity == 1
158
- yield ftp_connection
159
- else
160
- yield
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
- @inside_with_ftp -= 1
163
- ftp_done
178
+ result
164
179
  end
165
180
  def ftp_done
166
181
  close_ftp if @inside_with_ftp.to_i == 0
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dcas-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - BehindLogic