stockboy 1.3.0 → 1.3.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b36c08f870f1306e9c36cb125438bf8e01b3e6dc
4
- data.tar.gz: e997f70d91635b9456ed6c0758e981ea7f2e16ed
3
+ metadata.gz: 05fab717904ef1e81b3395c5e9bd55868304794b
4
+ data.tar.gz: 65fcee4af9cb07edac6349a5e0c6c11bf22b6013
5
5
  SHA512:
6
- metadata.gz: 0a129da6b911fa8812706140192419782ce1e09e5ea26bc8a161dcf1006a918ff7ab6cbd94a94e1b6f4c279d9d092146e87386b5dd64a837980a03ba2682e48a
7
- data.tar.gz: e5fd24fb0698cfb2d8488f35587ea49b0ac2d7bc0d39422a17fd5218a0974429fb1baf214e5f00f5295ec4ba309dade51e5a9a1d2b9048e5a9b0db6a51e19773
6
+ metadata.gz: a430d46d3e5e422d723f8b1dd236257e4d61043cdb417e418706d24484260cbd0725e16d72ac20435d9feb1e140fbf9513cf26cc84af57d85080a369a50701df
7
+ data.tar.gz: caed313e0d31f77265ad1f6823f67309b6e8388cded3473289aa2eee51ef245a6cdecb25b44ce8b63bf98ebf9545ac6f0758fe0b81c9880bd249ae3c39883b3c
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.3.1 / 2017-11-07
4
+
5
+ * [ENHANCEMENT] Expose both raw FTP commands or generic adapter commands
6
+
3
7
  ## 1.3.0 / 2017-10-26
4
8
 
5
9
  * [FEATURE] Add `secure` option for FTP to use SFTP
@@ -110,21 +110,21 @@ module Stockboy::Providers
110
110
  @file_larger = opts[:file_larger]
111
111
  @pick = opts[:pick] || :last
112
112
  DSL.new(self).instance_eval(&block) if block_given?
113
- @open_client = nil
113
+ @open_adapter = nil
114
114
  end
115
115
 
116
116
  def adapter_class
117
117
  secure ? SFTPAdapter : FTPAdapter
118
118
  end
119
119
 
120
- def client
121
- return yield @open_client if @open_client
120
+ def adapter
121
+ return yield @open_adapter if @open_adapter
122
122
 
123
123
  adapter_class.new(self).open do |ftp|
124
- @open_client = ftp
124
+ @open_adapter = ftp
125
125
  ftp.chdir file_dir if file_dir
126
126
  response = yield ftp
127
- @open_client = nil
127
+ @open_adapter = nil
128
128
  response
129
129
  end
130
130
 
@@ -134,9 +134,13 @@ module Stockboy::Providers
134
134
  nil
135
135
  end
136
136
 
137
+ def client
138
+ adapter { |ftp| yield ftp.client }
139
+ end
140
+
137
141
  def matching_file
138
142
  return @matching_file if @matching_file
139
- client do |ftp|
143
+ adapter do |ftp|
140
144
  file_listing = ftp.list_files
141
145
  @matching_file = pick_from file_listing.select(&file_name_matcher)
142
146
  end
@@ -144,7 +148,7 @@ module Stockboy::Providers
144
148
 
145
149
  def delete_data
146
150
  raise Stockboy::OutOfSequence, "must confirm #matching_file or calling #data" unless picked_matching_file?
147
- client do |ftp|
151
+ adapter do |ftp|
148
152
  logger.info "FTP deleting file #{host} #{file_dir}/#{matching_file}"
149
153
  ftp.delete matching_file
150
154
  matching_file
@@ -161,7 +165,7 @@ module Stockboy::Providers
161
165
  private
162
166
 
163
167
  def fetch_data
164
- client do |ftp|
168
+ adapter do |ftp|
165
169
  validate_file(matching_file)
166
170
  if valid?
167
171
  logger.debug "FTP getting file #{inspect_matching_file}"
@@ -199,21 +203,21 @@ module Stockboy::Providers
199
203
  end
200
204
 
201
205
  def validate_file_newer(data_file)
202
- @data_time ||= client { |ftp| ftp.modification_time(data_file) }
206
+ @data_time ||= adapter { |ftp| ftp.modification_time(data_file) }
203
207
  if file_newer and @data_time < file_newer
204
208
  errors << "No new files since #{file_newer}"
205
209
  end
206
210
  end
207
211
 
208
212
  def validate_file_smaller(data_file)
209
- @data_size ||= client { |ftp| ftp.size(data_file) }
213
+ @data_size ||= adapter { |ftp| ftp.size(data_file) }
210
214
  if file_smaller and @data_size > file_smaller
211
215
  errors << "File size larger than #{file_smaller}"
212
216
  end
213
217
  end
214
218
 
215
219
  def validate_file_larger(data_file)
216
- @data_size ||= client { |ftp| ftp.size(data_file) }
220
+ @data_size ||= adapter { |ftp| ftp.size(data_file) }
217
221
  if file_larger and @data_size < file_larger
218
222
  errors << "File size smaller than #{file_larger}"
219
223
  end
@@ -34,10 +34,6 @@ module Stockboy::Providers
34
34
  client.download!(full_path(file_name))
35
35
  end
36
36
 
37
- def full_path(file_name)
38
- ::File.join(@file_dir, file_name)
39
- end
40
-
41
37
  def modification_time(file_name)
42
38
  stat(file_name).mtime
43
39
  end
@@ -46,6 +42,12 @@ module Stockboy::Providers
46
42
  stat(file_name).size
47
43
  end
48
44
 
45
+ private
46
+
47
+ def full_path(file_name)
48
+ ::File.join(@file_dir, file_name)
49
+ end
50
+
49
51
  def stat(file_name)
50
52
  client.file.open(full_path(file_name)).stat
51
53
  end
@@ -1,3 +1,3 @@
1
1
  module Stockboy
2
- VERSION = "1.3.0"
2
+ VERSION = "1.3.1"
3
3
  end
@@ -41,9 +41,63 @@ module Stockboy
41
41
  end
42
42
  end
43
43
 
44
+ describe "#adapter" do
45
+ it "should return yielded result" do
46
+ stub_adapter
47
+
48
+ result = provider.adapter { |_| "a_file_name.csv" }
49
+
50
+ expect(result).to eq "a_file_name.csv"
51
+ end
52
+
53
+ context "with default secure (false)" do
54
+ it "yields an FTP adapter" do
55
+ net_ftp = expect_ftp_connection
56
+
57
+ provider.adapter do |ftp|
58
+ expect(ftp.client).to be net_ftp
59
+ end
60
+ end
61
+
62
+ it "catches FTP errors" do
63
+ expect_ftp_connection
64
+
65
+ provider.adapter do |ftp|
66
+ raise Net::FTPError, "not really gopher"
67
+ end
68
+
69
+ expect(provider.errors).to include "not really gopher"
70
+ end
71
+ end
72
+
73
+ context "with secure" do
74
+ before do
75
+ provider.secure = true
76
+ end
77
+
78
+ it "yields an SFTP adapter" do
79
+ net_sftp = expect_sftp_connection
80
+
81
+ provider.adapter do |ftp|
82
+ expect(ftp.client).to be net_sftp
83
+ end
84
+ end
85
+
86
+ it "catches SFTP errors" do
87
+ expect_sftp_connection
88
+
89
+ provider.adapter do |ftp|
90
+ raise Net::SFTP::Exception, "still not gopher"
91
+ end
92
+
93
+ expect(provider.errors).to include "still not gopher"
94
+ end
95
+ end
96
+ end
97
+
44
98
  describe "#client" do
45
99
  it "should return yielded result" do
46
- expect_connection
100
+ expect_ftp_connection
47
101
 
48
102
  result = provider.client { |_| "a_file_name.csv" }
49
103
 
@@ -67,20 +121,20 @@ module Stockboy
67
121
  end
68
122
 
69
123
  it "downloads the last matching file" do
70
- net_ftp = expect_connection
71
- expect(net_ftp).to receive(:list_files).and_return ['20120101.csv', '20120102.csv']
72
- expect(net_ftp).to receive(:download).with('20120102.csv').and_return "DATA"
124
+ adapter = stub_adapter
125
+ expect(adapter).to receive(:list_files).and_return ['20120101.csv', '20120102.csv']
126
+ expect(adapter).to receive(:download).with('20120102.csv').and_return "DATA"
73
127
  expect(provider).to receive(:validate_file).and_return true
74
128
 
75
129
  expect(provider.data).to eq "DATA"
76
130
  end
77
131
 
78
132
  it "skips old files" do
79
- net_ftp = expect_connection
80
- expect(net_ftp).to receive(:list_files).and_return ['20120101.csv', '20120102.csv']
81
- expect(net_ftp).to receive(:modification_time).with('20120102.csv').and_return(Time.new(2009,01,01))
82
- expect(net_ftp).to receive(:size).with('20120102.csv').and_return(Time.new(2009,01,01))
83
- expect(net_ftp).to_not receive(:download)
133
+ adapter = stub_adapter
134
+ expect(adapter).to receive(:list_files).and_return ['20120101.csv', '20120102.csv']
135
+ expect(adapter).to receive(:modification_time).with('20120102.csv').and_return(Time.new(2009,01,01))
136
+ expect(adapter).to receive(:size).with('20120102.csv').and_return(Time.new(2009,01,01))
137
+ expect(adapter).to_not receive(:download)
84
138
 
85
139
  provider.file_newer = Time.new(2010,1,1)
86
140
  expect(provider.data).to be nil
@@ -89,13 +143,13 @@ module Stockboy
89
143
 
90
144
  describe "#matching_file" do
91
145
  it "does not change until cleared" do
92
- net_ftp = expect_connection
93
- expect(net_ftp).to receive(:list_files).and_return ["1.csv", "2.csv"]
146
+ adapter = stub_adapter
147
+ expect(adapter).to receive(:list_files).and_return ["1.csv", "2.csv"]
94
148
 
95
149
  expect(provider.matching_file).to eq "2.csv"
96
150
 
97
- net_ftp = expect_connection
98
- expect(net_ftp).to receive(:list_files).and_return ["1.csv", "2.csv", "3.csv"]
151
+ adapter = stub_adapter
152
+ expect(adapter).to receive(:list_files).and_return ["1.csv", "2.csv", "3.csv"]
99
153
 
100
154
  expect(provider.matching_file).to eq "2.csv"
101
155
  provider.clear
@@ -110,25 +164,37 @@ module Stockboy
110
164
  end
111
165
 
112
166
  it "should delete the matching file" do
113
- net_ftp = expect_connection
114
- expect(net_ftp).to receive(:list_files).and_return ["1.csv", "2.csv"]
167
+ adapter = stub_adapter
168
+ expect(adapter).to receive(:list_files).and_return ["1.csv", "2.csv"]
115
169
 
116
170
  expect(provider.matching_file).to eq "2.csv"
117
171
 
118
- net_ftp = expect_connection
119
- expect(net_ftp).to receive(:delete).with("2.csv")
172
+ adapter = stub_adapter
173
+ expect(adapter).to receive(:delete).with("2.csv")
120
174
 
121
175
  expect(provider.delete_data).to eq "2.csv"
122
176
  end
123
177
  end
124
178
 
125
- def expect_connection
126
- adapter = instance_double(provider.adapter_class)
179
+ def stub_adapter
180
+ adapter = instance_double(provider.adapter_class, client: nil)
127
181
  expect(adapter).to receive(:open).and_yield(adapter)
128
182
 
129
183
  expect(provider.adapter_class).to receive(:new).with(provider).and_return adapter
130
184
  adapter
131
185
  end
132
186
 
187
+ def expect_ftp_connection
188
+ connection = instance_double(Net::FTP, "binary=" => nil, "passive=" => nil)
189
+ expect(Net::FTP).to receive(:open).and_yield(connection)
190
+ connection
191
+ end
192
+
193
+ def expect_sftp_connection
194
+ connection = instance_double(Net::SFTP)
195
+ expect(Net::SFTP).to receive(:start).and_yield(connection)
196
+ connection
197
+ end
198
+
133
199
  end
134
200
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stockboy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Vit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-27 00:00:00.000000000 Z
11
+ date: 2017-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake