nightcrawler_swift 0.4.0 → 0.5.0

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 (37) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +4 -1
  4. data/Changelog.md +12 -2
  5. data/Gemfile.lock +11 -1
  6. data/README.md +112 -7
  7. data/bin/nswift +1 -1
  8. data/lib/nightcrawler_swift/cli/commands/url_for.rb +9 -0
  9. data/lib/nightcrawler_swift/cli/formatters/basic.rb +40 -0
  10. data/lib/nightcrawler_swift/cli/opt_parser.rb +81 -0
  11. data/lib/nightcrawler_swift/cli/runner.rb +127 -0
  12. data/lib/nightcrawler_swift/cli.rb +16 -173
  13. data/lib/nightcrawler_swift/command.rb +5 -14
  14. data/lib/nightcrawler_swift/commands/delete.rb +4 -6
  15. data/lib/nightcrawler_swift/commands/download.rb +4 -6
  16. data/lib/nightcrawler_swift/commands/list.rb +0 -6
  17. data/lib/nightcrawler_swift/commands/upload.rb +1 -4
  18. data/lib/nightcrawler_swift/connection.rb +15 -15
  19. data/lib/nightcrawler_swift/exceptions.rb +2 -1
  20. data/lib/nightcrawler_swift/gateway.rb +68 -0
  21. data/lib/nightcrawler_swift/version.rb +1 -1
  22. data/lib/nightcrawler_swift.rb +7 -2
  23. data/nightcrawler_swift.gemspec +1 -0
  24. data/spec/lib/nightcrawler_swift/cli/commands/url_for_spec.rb +34 -0
  25. data/spec/lib/nightcrawler_swift/cli/formatters/basic_spec.rb +117 -0
  26. data/spec/lib/nightcrawler_swift/cli/opt_parser_spec.rb +135 -0
  27. data/spec/lib/nightcrawler_swift/{cli_spec.rb → cli/runner_spec.rb} +133 -136
  28. data/spec/lib/nightcrawler_swift/command_spec.rb +17 -32
  29. data/spec/lib/nightcrawler_swift/commands/delete_spec.rb +8 -29
  30. data/spec/lib/nightcrawler_swift/commands/download_spec.rb +8 -29
  31. data/spec/lib/nightcrawler_swift/commands/list_spec.rb +14 -44
  32. data/spec/lib/nightcrawler_swift/commands/upload_spec.rb +1 -8
  33. data/spec/lib/nightcrawler_swift/connection_spec.rb +26 -9
  34. data/spec/lib/nightcrawler_swift/gateway_spec.rb +139 -0
  35. data/spec/lib/nightcrawler_swift_spec.rb +15 -2
  36. data/spec/spec_helper.rb +3 -0
  37. metadata +31 -4
@@ -25,15 +25,15 @@ describe NightcrawlerSwift::Download do
25
25
  subject.execute "file_path"
26
26
  end
27
27
 
28
- before do
29
- allow(subject).to receive(:get).and_return(response)
30
- end
31
-
32
28
  context "success" do
33
29
  let :response do
34
30
  double(:response, code: 200, body: "content")
35
31
  end
36
32
 
33
+ before do
34
+ allow(subject).to receive(:get).and_return(response)
35
+ end
36
+
37
37
  it "gets using public url" do
38
38
  execute
39
39
  expect(subject).to have_received(:get).with("server-url/#{bucket}/file_path")
@@ -44,31 +44,10 @@ describe NightcrawlerSwift::Download do
44
44
  end
45
45
  end
46
46
 
47
- context "when the file does not exist" do
48
- let :response do
49
- double(:response, code: 404)
50
- end
51
-
52
- before do
53
- allow(subject).to receive(:get).and_raise(RestClient::ResourceNotFound.new(response))
54
- end
55
-
56
- it "raises NightcrawlerSwift::Exceptions::NotFoundError" do
57
- expect { execute }.to raise_error NightcrawlerSwift::Exceptions::NotFoundError
58
- end
59
- end
60
-
61
- context "when another error happens" do
62
- let :response do
63
- double(:response, code: 500)
64
- end
65
-
66
- before do
67
- allow(subject).to receive(:get).and_raise(RuntimeError.new(response))
68
- end
69
-
70
- it "raises NightcrawlerSwift::Exceptions::ConnectionError" do
71
- expect { execute }.to raise_error NightcrawlerSwift::Exceptions::ConnectionError
47
+ context "when the path was not informed" do
48
+ it "raises NightcrawlerSwift::Exceptions::ValidationError" do
49
+ expect { subject.execute nil }.to raise_error NightcrawlerSwift::Exceptions::ValidationError
50
+ expect { subject.execute "" }.to raise_error NightcrawlerSwift::Exceptions::ValidationError
72
51
  end
73
52
  end
74
53
  end
@@ -19,56 +19,26 @@ describe NightcrawlerSwift::List do
19
19
  end
20
20
 
21
21
  describe "#execute" do
22
- context "success" do
23
- let :json_response do
24
- [{"name" => "file"}]
25
- end
26
-
27
- let :response do
28
- double(:response, code: 200, body: json_response.to_json)
29
- end
30
-
31
- before do
32
- allow(subject).to receive(:get).and_return(response)
33
- end
34
-
35
- it "gets upload url" do
36
- subject.execute
37
- expect(subject).to have_received(:get).with(connection.upload_url, headers: {accept: :json})
38
- end
39
-
40
- it "returns the parsed json" do
41
- result = subject.execute
42
- expect(result).to eql json_response
43
- end
22
+ let :json_response do
23
+ [{"name" => "file"}]
44
24
  end
45
25
 
46
- context "when the bucket does not exist" do
47
- let :response do
48
- double(:response, code: 400)
49
- end
50
-
51
- before do
52
- allow(subject).to receive(:get).and_raise(RestClient::ResourceNotFound.new(response))
53
- end
54
-
55
- it "raises NightcrawlerSwift::Exceptions::NotFoundError" do
56
- expect { subject.execute }.to raise_error NightcrawlerSwift::Exceptions::NotFoundError
57
- end
26
+ let :response do
27
+ double(:response, code: 200, body: json_response.to_json)
58
28
  end
59
29
 
60
- context "when another error happens" do
61
- let :response do
62
- double(:response, code: 500)
63
- end
30
+ before do
31
+ allow(subject).to receive(:get).and_return(response)
32
+ end
64
33
 
65
- before do
66
- allow(subject).to receive(:get).and_raise(RuntimeError.new(response))
67
- end
34
+ it "gets upload url" do
35
+ subject.execute
36
+ expect(subject).to have_received(:get).with(connection.upload_url, headers: {accept: :json})
37
+ end
68
38
 
69
- it "raises NightcrawlerSwift::Exceptions::ConnectionError" do
70
- expect { subject.execute }.to raise_error NightcrawlerSwift::Exceptions::ConnectionError
71
- end
39
+ it "returns the parsed json" do
40
+ result = subject.execute
41
+ expect(result).to eql json_response
72
42
  end
73
43
  end
74
44
 
@@ -71,7 +71,7 @@ describe NightcrawlerSwift::Upload do
71
71
  it "sends max_age into headers" do
72
72
  NightcrawlerSwift.configure max_age: max_age
73
73
  execute
74
- expect(subject).to have_received(:put).with(anything, hash_including(headers: { content_type: "text/css", etag: etag, cache_control: "max-age=#{max_age}" }))
74
+ expect(subject).to have_received(:put).with(anything, hash_including(headers: { content_type: "text/css", etag: etag, cache_control: "public, max-age=#{max_age}" }))
75
75
  end
76
76
 
77
77
  context "when response code is 200" do
@@ -87,13 +87,6 @@ describe NightcrawlerSwift::Upload do
87
87
  let(:response) { double(:response, code: 500) }
88
88
  it { expect(execute).to be false }
89
89
  end
90
-
91
- context "when rescue RestClient::UnprocessableEntity" do
92
- it "wraps into NightcrawlerSwift::Exceptions::ValidationError" do
93
- expect(subject).to receive(:put).and_raise(RestClient::UnprocessableEntity.new)
94
- expect { execute }.to raise_error NightcrawlerSwift::Exceptions::ValidationError
95
- end
96
- end
97
90
  end
98
91
 
99
92
  end
@@ -2,7 +2,7 @@ require "spec_helper"
2
2
 
3
3
  describe NightcrawlerSwift::Connection do
4
4
 
5
- let :opts do
5
+ let :options do
6
6
  {
7
7
  bucket: "my-bucket-name",
8
8
  tenant_name: "tenant_username1",
@@ -13,9 +13,8 @@ describe NightcrawlerSwift::Connection do
13
13
  }
14
14
  end
15
15
 
16
- let :unauthorized_error do
17
- response = OpenStruct.new(body: "error", code: 401)
18
- RestClient::Unauthorized.new response, response.code
16
+ let :opts do
17
+ options
19
18
  end
20
19
 
21
20
  subject do
@@ -112,17 +111,35 @@ describe NightcrawlerSwift::Connection do
112
111
  expect { subject.connect! }.to raise_error(NightcrawlerSwift::Exceptions::ConfigurationError)
113
112
  end
114
113
  end
114
+
115
+ context "with a configured admin_url" do
116
+ let(:new_admin_url){ "http://some-new-admin-url" }
117
+ let(:opts) { options.merge(admin_url: new_admin_url) }
118
+
119
+ it "uses the given url" do
120
+ subject.connect!
121
+ expect(subject.admin_url).to eql new_admin_url
122
+ end
123
+ end
124
+
125
+ context "with a configured public_url" do
126
+ let(:new_public_url) { "http://some-new-public-url" }
127
+ let(:opts) { options.merge(public_url: new_public_url) }
128
+
129
+ it "uses the given url" do
130
+ subject.connect!
131
+ expect(subject.public_url).to eql new_public_url
132
+ end
133
+ end
115
134
  end
116
135
 
117
- describe "when some error happens with the connection" do
136
+ describe "when some error happens" do
118
137
  before do
119
- allow(RestClient).to receive(:post).
120
- with(opts[:auth_url], auth_json, content_type: :json, accept: :json).
121
- and_raise(unauthorized_error)
138
+ allow(RestClient::Resource).to receive(:new).and_raise(StandardError.new("error!"))
122
139
  end
123
140
 
124
141
  it "raises NightcrawlerSwift::Exceptions::ConnectionError" do
125
- expect { subject.connect! }.to raise_error(NightcrawlerSwift::Exceptions::ConnectionError)
142
+ expect { subject.connect! }.to raise_error NightcrawlerSwift::Exceptions::ConnectionError
126
143
  end
127
144
  end
128
145
  end
@@ -0,0 +1,139 @@
1
+ require 'spec_helper'
2
+
3
+ describe NightcrawlerSwift::Gateway do
4
+
5
+ let :url do
6
+ "http://some-url"
7
+ end
8
+
9
+ let :request_get do
10
+ subject.request {|r| r.get}
11
+ end
12
+
13
+ subject do
14
+ NightcrawlerSwift::Gateway.new url
15
+ end
16
+
17
+ before do
18
+ NightcrawlerSwift.configure
19
+ end
20
+
21
+ describe "initialization" do
22
+ it "creates the resource with the url and the options: verify_ssl and timeout" do
23
+ expect(RestClient::Resource).to receive(:new).with(
24
+ url,
25
+ verify_ssl: NightcrawlerSwift.options.verify_ssl,
26
+ timeout: NightcrawlerSwift.options.timeout
27
+ )
28
+ subject
29
+ end
30
+ end
31
+
32
+ describe "#request" do
33
+ context "with a successful request" do
34
+ it "calls the received block" do
35
+ expect(subject).to receive(:request).and_yield(subject.resource)
36
+ subject.request {|resource| }
37
+ end
38
+ end
39
+
40
+ context "when it is not authorized" do
41
+ let(:response) { double(:response, code: 401) }
42
+
43
+ before do
44
+ allow(subject.resource).to receive(:get).and_raise(RestClient::Unauthorized.new(response))
45
+ end
46
+
47
+ it "raises NightcrawlerSwift::Exceptions::UnauthorizedError" do
48
+ expect { request_get }.to raise_error NightcrawlerSwift::Exceptions::UnauthorizedError
49
+ end
50
+ end
51
+
52
+ context "when the resource does not exist" do
53
+ let(:response) { double(:response, code: 404) }
54
+
55
+ before do
56
+ allow(subject.resource).to receive(:get).and_raise(RestClient::ResourceNotFound.new(response))
57
+ end
58
+
59
+ it "raises NightcrawlerSwift::Exceptions::NotFoundError" do
60
+ expect { request_get }.to raise_error NightcrawlerSwift::Exceptions::NotFoundError
61
+ end
62
+ end
63
+
64
+ context "when rescue RestClient::UnprocessableEntity" do
65
+ let(:response) { double(:response, code: 422) }
66
+
67
+ before do
68
+ allow(subject.resource).to receive(:get).and_raise(RestClient::UnprocessableEntity.new(response))
69
+ end
70
+
71
+ it "wraps into NightcrawlerSwift::Exceptions::ValidationError" do
72
+ expect { request_get }.to raise_error NightcrawlerSwift::Exceptions::ValidationError
73
+ end
74
+ end
75
+
76
+ context "when another error happens" do
77
+ let :response do
78
+ double(:response, code: 500)
79
+ end
80
+
81
+ let :retries do
82
+ 3
83
+ end
84
+
85
+ context "with enough retries" do
86
+ it "recovers from the failure" do
87
+ expect(subject.resource).to receive(:get).once.and_raise(RuntimeError.new(response))
88
+ expect(subject.resource).to receive(:get).once.and_return(true)
89
+ expect(subject).to receive(:sleep).once.with(1)
90
+ expect(subject).to_not receive(:sleep).with(2)
91
+ expect { request_get }.to_not raise_error
92
+ expect(subject.attempts).to eql(2) # original + one retry
93
+ end
94
+ end
95
+
96
+ context "with all retries used" do
97
+ before do
98
+ NightcrawlerSwift.options.retries = retries
99
+ end
100
+
101
+ it "waits a time based on the attempt number and if no one results in a good request it raises the exception" do
102
+ expect(subject.instance_variable_get(:@retries)).to eql retries
103
+ expect(subject.resource).to receive(:get).exactly(4).times.and_raise(RuntimeError.new(response))
104
+ expect(subject).to receive(:sleep).once.with(1)
105
+ expect(subject).to receive(:sleep).once.with(2)
106
+ expect(subject).to receive(:sleep).once.with(4)
107
+ expect { request_get }.to raise_error NightcrawlerSwift::Exceptions::ConnectionError
108
+ expect(subject.attempts).to eql(retries + 1)
109
+ end
110
+
111
+ context "and the time achieves max_retry_time" do
112
+ it "waits the max_retry_time for every retry" do
113
+ current_retry_time = NightcrawlerSwift.options.max_retry_time - 1
114
+ subject.instance_variable_set(:@current_retry_time, current_retry_time)
115
+ expect(subject.resource).to receive(:get).exactly(4).times.and_raise(RuntimeError.new(response))
116
+ expect(subject).to receive(:sleep).once.with(current_retry_time)
117
+ expect(subject).to receive(:sleep).twice.with(NightcrawlerSwift.options.max_retry_time)
118
+ expect { request_get }.to raise_error NightcrawlerSwift::Exceptions::ConnectionError
119
+ expect(subject.attempts).to eql(retries + 1)
120
+ end
121
+ end
122
+ end
123
+
124
+ context "with retries set to false" do
125
+ before do
126
+ NightcrawlerSwift.options.retries = false
127
+ expect(subject).to_not receive(:sleep)
128
+ end
129
+
130
+ it "raises NightcrawlerSwift::Exceptions::ConnectionError" do
131
+ expect(subject.resource).to receive(:get).once.and_raise(RuntimeError.new(response))
132
+ expect { request_get }.to raise_error NightcrawlerSwift::Exceptions::ConnectionError
133
+ expect(subject.attempts).to eql(1)
134
+ end
135
+ end
136
+ end
137
+ end
138
+
139
+ end
@@ -3,7 +3,6 @@ require "spec_helper"
3
3
  describe NightcrawlerSwift do
4
4
 
5
5
  let(:opts) { {bucket: "rogue"} }
6
- let(:options) { OpenStruct.new(opts) }
7
6
 
8
7
  subject do
9
8
  NightcrawlerSwift
@@ -50,6 +49,18 @@ describe NightcrawlerSwift do
50
49
  end
51
50
  end
52
51
 
52
+ context "retries" do
53
+ it "defauts to 5" do
54
+ expect(subject.options.retries).to eql 5
55
+ end
56
+ end
57
+
58
+ context "max_retry_time" do
59
+ it "defauts to 30" do
60
+ expect(subject.options.max_retry_time).to eql 30
61
+ end
62
+ end
63
+
53
64
  context "and max_age isn't an integer" do
54
65
  let(:opts) { {max_age: "a string"} }
55
66
 
@@ -75,7 +86,9 @@ describe NightcrawlerSwift do
75
86
 
76
87
  it "returns the given options" do
77
88
  NightcrawlerSwift.configure(opts)
78
- expect(NightcrawlerSwift.options).to eql(OpenStruct.new(opts.merge(verify_ssl: false)))
89
+ opts.keys.each do |key|
90
+ expect(NightcrawlerSwift.options).to respond_to key
91
+ end
79
92
  end
80
93
  end
81
94
 
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
3
+
1
4
  require "rake"
2
5
  require "byebug"
3
6
  require "nightcrawler_swift"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nightcrawler_swift
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tulios
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-09-03 00:00:00.000000000 Z
12
+ date: 2014-09-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
@@ -81,6 +81,20 @@ dependencies:
81
81
  - - ">="
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: codeclimate-test-reporter
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
84
98
  - !ruby/object:Gem::Dependency
85
99
  name: byebug
86
100
  requirement: !ruby/object:Gem::Requirement
@@ -119,6 +133,10 @@ files:
119
133
  - bin/nswift
120
134
  - lib/nightcrawler_swift.rb
121
135
  - lib/nightcrawler_swift/cli.rb
136
+ - lib/nightcrawler_swift/cli/commands/url_for.rb
137
+ - lib/nightcrawler_swift/cli/formatters/basic.rb
138
+ - lib/nightcrawler_swift/cli/opt_parser.rb
139
+ - lib/nightcrawler_swift/cli/runner.rb
122
140
  - lib/nightcrawler_swift/command.rb
123
141
  - lib/nightcrawler_swift/commands/delete.rb
124
142
  - lib/nightcrawler_swift/commands/download.rb
@@ -127,6 +145,7 @@ files:
127
145
  - lib/nightcrawler_swift/commands/upload.rb
128
146
  - lib/nightcrawler_swift/connection.rb
129
147
  - lib/nightcrawler_swift/exceptions.rb
148
+ - lib/nightcrawler_swift/gateway.rb
130
149
  - lib/nightcrawler_swift/railtie.rb
131
150
  - lib/nightcrawler_swift/tasks/asset_sync.rake
132
151
  - lib/nightcrawler_swift/version.rb
@@ -137,7 +156,10 @@ files:
137
156
  - spec/fixtures/assets/ex3/ex4.txt
138
157
  - spec/fixtures/assets/js1.js
139
158
  - spec/fixtures/auth_success.json
140
- - spec/lib/nightcrawler_swift/cli_spec.rb
159
+ - spec/lib/nightcrawler_swift/cli/commands/url_for_spec.rb
160
+ - spec/lib/nightcrawler_swift/cli/formatters/basic_spec.rb
161
+ - spec/lib/nightcrawler_swift/cli/opt_parser_spec.rb
162
+ - spec/lib/nightcrawler_swift/cli/runner_spec.rb
141
163
  - spec/lib/nightcrawler_swift/command_spec.rb
142
164
  - spec/lib/nightcrawler_swift/commands/delete_spec.rb
143
165
  - spec/lib/nightcrawler_swift/commands/download_spec.rb
@@ -145,6 +167,7 @@ files:
145
167
  - spec/lib/nightcrawler_swift/commands/sync_spec.rb
146
168
  - spec/lib/nightcrawler_swift/commands/upload_spec.rb
147
169
  - spec/lib/nightcrawler_swift/connection_spec.rb
170
+ - spec/lib/nightcrawler_swift/gateway_spec.rb
148
171
  - spec/lib/nightcrawler_swift/tasks/asset_sync_spec.rb
149
172
  - spec/lib/nightcrawler_swift_spec.rb
150
173
  - spec/spec_helper.rb
@@ -180,7 +203,10 @@ test_files:
180
203
  - spec/fixtures/assets/ex3/ex4.txt
181
204
  - spec/fixtures/assets/js1.js
182
205
  - spec/fixtures/auth_success.json
183
- - spec/lib/nightcrawler_swift/cli_spec.rb
206
+ - spec/lib/nightcrawler_swift/cli/commands/url_for_spec.rb
207
+ - spec/lib/nightcrawler_swift/cli/formatters/basic_spec.rb
208
+ - spec/lib/nightcrawler_swift/cli/opt_parser_spec.rb
209
+ - spec/lib/nightcrawler_swift/cli/runner_spec.rb
184
210
  - spec/lib/nightcrawler_swift/command_spec.rb
185
211
  - spec/lib/nightcrawler_swift/commands/delete_spec.rb
186
212
  - spec/lib/nightcrawler_swift/commands/download_spec.rb
@@ -188,6 +214,7 @@ test_files:
188
214
  - spec/lib/nightcrawler_swift/commands/sync_spec.rb
189
215
  - spec/lib/nightcrawler_swift/commands/upload_spec.rb
190
216
  - spec/lib/nightcrawler_swift/connection_spec.rb
217
+ - spec/lib/nightcrawler_swift/gateway_spec.rb
191
218
  - spec/lib/nightcrawler_swift/tasks/asset_sync_spec.rb
192
219
  - spec/lib/nightcrawler_swift_spec.rb
193
220
  - spec/spec_helper.rb