nightcrawler_swift 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6210245e5388c7b1c73fa1c6ff58b2f7a2770320
4
- data.tar.gz: 6071b7124b88f4293ea186b2755c99a7e6f3b407
3
+ metadata.gz: e8ef6b380623f8ce83cfea1491c10c44bfc8ed04
4
+ data.tar.gz: 9df2abbd80c6f1fceedf2dddc58d59be45e33520
5
5
  SHA512:
6
- metadata.gz: 65753c810f8691b5e06afe0db034d692ac9187d7d73e5b3be899a141fd1ffe3eb86114ebb2bcd4c6ebdffadc8760905a9a04d6da93a677f6c3db7a116824ee48
7
- data.tar.gz: bd9c1a5bbc7dffd4e24c0d8efab7e50f0e8917af973b4b9798e2d9df14b9f61793d1414d196df03b292b6758e431a50c6b3913d22c5fbb198617d2492c587674
6
+ metadata.gz: 8fdf97c07f42e76d5b83941b2f95d8cb32270bba360369c0b28547a25151ad56290aad0ea5ecfb076d1c012fb5abf4a331c579f216ed815dd342f9f5addf97d5
7
+ data.tar.gz: 9f2d5e258abc3f309cc57759a81938041ddf8ec49111276e3b4ee3bf6120edf527c875a01715b28a030aa970c508d5dbbfb185d702dd2d354e068cba11d6eff3
data/Changelog.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.7.0
4
+
5
+ - CLI now supports ```--no-cache``` which ignores the cached connection and removes the cache file if it exists (issue #12)
6
+ - Bugfix: invalidate nswift_cache when the connection fail (issue #27)
7
+ - Bugfix: OpenStruct in Ruby 1.9.x doesn't have the method [], using 'send' instead
8
+ - Bugfix: OpenStruct and nil doesn't have the method to_h in Ruby 1.9.x
9
+
3
10
  ## 0.6.0
4
11
 
5
12
  - Upload command allows a custom max-age (issue #23)
data/Gemfile.lock CHANGED
@@ -1,26 +1,27 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nightcrawler_swift (0.6.0)
4
+ nightcrawler_swift (0.7.0)
5
5
  multi_mime (>= 1.0.1)
6
6
  rest-client
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- byebug (3.2.0)
11
+ byebug (3.5.1)
12
12
  columnize (~> 0.8)
13
13
  debugger-linecache (~> 1.2)
14
+ slop (~> 3.6)
14
15
  codeclimate-test-reporter (0.4.0)
15
16
  simplecov (>= 0.7.1, < 1.0.0)
16
- columnize (0.8.9)
17
+ columnize (0.9.0)
17
18
  debugger-linecache (1.2.0)
18
19
  diff-lcs (1.2.5)
19
20
  docile (1.1.5)
20
- mime-types (2.3)
21
+ mime-types (2.4.3)
21
22
  multi_json (1.10.1)
22
23
  multi_mime (1.0.1)
23
- netrc (0.7.7)
24
+ netrc (0.10.2)
24
25
  rake (10.3.2)
25
26
  rest-client (1.7.2)
26
27
  mime-types (>= 1.16, < 3.0)
@@ -42,6 +43,7 @@ GEM
42
43
  multi_json
43
44
  simplecov-html (~> 0.8.0)
44
45
  simplecov-html (0.8.0)
46
+ slop (3.6.0)
45
47
 
46
48
  PLATFORMS
47
49
  ruby
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Nightcrawler Swift [![Code Climate](https://codeclimate.com/github/tulios/nightcrawler_swift/badges/gpa.svg)](https://codeclimate.com/github/tulios/nightcrawler_swift) [![Travis](https://api.travis-ci.org/tulios/nightcrawler_swift.svg?branch=master)](https://travis-ci.org/tulios/nightcrawler_swift) [![Gem Version](https://badge.fury.io/rb/nightcrawler_swift.svg)](http://badge.fury.io/rb/nightcrawler_swift)
1
+ # Nightcrawler Swift [![Gem Version](https://badge.fury.io/rb/nightcrawler_swift.svg)](http://badge.fury.io/rb/nightcrawler_swift) [![Code Climate](https://codeclimate.com/github/tulios/nightcrawler_swift/badges/gpa.svg)](https://codeclimate.com/github/tulios/nightcrawler_swift) [![Travis](https://api.travis-ci.org/tulios/nightcrawler_swift.svg?branch=master)](https://travis-ci.org/tulios/nightcrawler_swift)
2
2
 
3
3
  Like the X-Men nightcrawler this gem teleports your assets to a OpenStack Swift bucket/container. It was designed to sync your assets with OpenStack Swift and allow some operations with your buckets/containers.
4
4
 
@@ -36,6 +36,7 @@ module NightcrawlerSwift::CLI
36
36
  configure_option_bucket
37
37
  configure_option_max_age
38
38
  configure_option_config
39
+ configure_option_no_cache
39
40
  configure_option_help
40
41
  configure_option_version
41
42
  end
@@ -73,6 +74,12 @@ module NightcrawlerSwift::CLI
73
74
  end
74
75
  end
75
76
 
77
+ def configure_option_no_cache
78
+ @parser.on_tail("--no-cache", "Avoids the connection cache. It also clears the cache if it exists") do
79
+ @runner.options.use_cache = false
80
+ end
81
+ end
82
+
76
83
  def configure_option_help
77
84
  @parser.on_tail("-h", "--help", "Show this message") do
78
85
  @runner.log help
@@ -33,6 +33,7 @@ module NightcrawlerSwift::CLI
33
33
  @options.configured = true
34
34
  @options.default_config_file = true
35
35
  @options.config_file = File.expand_path(File.join(user_home_dir, CONFIG_FILE))
36
+ @options.use_cache = true
36
37
  @options.cache_file = File.expand_path(File.join(user_home_dir, CACHE_FILE))
37
38
  @options.config_hash = {}
38
39
  end
@@ -79,23 +80,34 @@ module NightcrawlerSwift::CLI
79
80
  def connect_and_execute &block
80
81
  path = options.cache_file
81
82
  if File.exist?(path)
82
- hash = JSON.parse File.read(path)
83
- NightcrawlerSwift.connection.auth_response = OpenStruct.new(hash)
84
- NightcrawlerSwift.connection.configure
85
-
86
- token_id = NightcrawlerSwift.connection.token_id
87
- NightcrawlerSwift.logger.debug "Cache found, restablishing connection with token_id: #{token_id}"
83
+ @options.use_cache ? restore_connection(path) : File.delete(path)
88
84
  end
89
85
 
90
86
  begin
91
87
  block.call
92
88
  ensure
89
+ return unless @options.use_cache
93
90
  File.open(path, "w") do |f|
94
91
  f.write(NightcrawlerSwift.connection.auth_response.to_h.to_json)
95
92
  end
96
93
  end
97
94
  end
98
95
 
96
+ def restore_connection cache_path
97
+ begin
98
+ hash = JSON.parse File.read(cache_path)
99
+ NightcrawlerSwift.connection.auth_response = OpenStruct.new(hash)
100
+ NightcrawlerSwift.connection.configure
101
+
102
+ token_id = NightcrawlerSwift.connection.token_id
103
+ NightcrawlerSwift.logger.debug "Cache found, restablishing connection with token_id: #{token_id}"
104
+
105
+ rescue
106
+ log "Failed to restore connection, removing cache"
107
+ File.delete cache_path
108
+ end
109
+ end
110
+
99
111
  def parse_parameters
100
112
  @opt_parser.parse!
101
113
  check_rcfile if options.default_config_file
@@ -1,38 +1,40 @@
1
1
  module NightcrawlerSwift
2
- module Hash
2
+ module Ext
3
+ module Hash
3
4
 
4
- unless {}.respond_to?(:symbolize_keys)
5
- def symbolize_keys
6
- {}.tap do |result|
7
- keys.each {|k| result[k.to_sym] = self[k]}
5
+ unless {}.respond_to?(:symbolize_keys)
6
+ def symbolize_keys
7
+ {}.tap do |result|
8
+ keys.each {|k| result[k.to_sym] = self[k]}
9
+ end
8
10
  end
9
- end
10
11
 
11
- def symbolize_keys!
12
- self.tap do
13
- keys.each {|k| self[k.to_sym] = delete(k)}
12
+ def symbolize_keys!
13
+ self.tap do
14
+ keys.each {|k| self[k.to_sym] = delete(k)}
15
+ end
14
16
  end
15
17
  end
16
- end
17
18
 
18
- unless {}.respond_to?(:compact)
19
- def compact
20
- {}.tap do |result|
21
- keys.each {|k| result[k] = self[k] unless self[k].nil?}
19
+ unless {}.respond_to?(:compact)
20
+ def compact
21
+ {}.tap do |result|
22
+ keys.each {|k| result[k] = self[k] unless self[k].nil?}
23
+ end
22
24
  end
23
- end
24
25
 
25
- def compact!
26
- self.tap do
27
- keys.each do |k|
28
- value = delete(k)
29
- self[k] = value unless value.nil?
26
+ def compact!
27
+ self.tap do
28
+ keys.each do |k|
29
+ value = delete(k)
30
+ self[k] = value unless value.nil?
31
+ end
30
32
  end
31
33
  end
32
34
  end
33
- end
34
35
 
36
+ end
35
37
  end
36
38
  end
37
39
 
38
- Hash.send :include, NightcrawlerSwift::Hash
40
+ Hash.send :include, NightcrawlerSwift::Ext::Hash
@@ -0,0 +1,15 @@
1
+ module NightcrawlerSwift
2
+ module Ext
3
+ module NilClass
4
+
5
+ unless nil.respond_to?(:to_h)
6
+ def to_h
7
+ {}
8
+ end
9
+ end
10
+
11
+ end
12
+ end
13
+ end
14
+
15
+ NilClass.send :include, NightcrawlerSwift::Ext::NilClass
@@ -0,0 +1,15 @@
1
+ module NightcrawlerSwift
2
+ module Ext
3
+ module OpenStruct
4
+
5
+ unless ::OpenStruct.new.respond_to?(:to_h)
6
+ def to_h
7
+ @table.dup || {}
8
+ end
9
+ end
10
+
11
+ end
12
+ end
13
+ end
14
+
15
+ OpenStruct.send :include, NightcrawlerSwift::Ext::OpenStruct
@@ -62,7 +62,10 @@ module NightcrawlerSwift
62
62
  :ssl_ca_file
63
63
 
64
64
  ].inject({}) {|hash, key|
65
- hash.tap {hash[key] = options[key]}
65
+ hash.tap {
66
+ # OpenStruct in Ruby 1.9.x doesn't have the method []
67
+ hash[key] = options.send(key)
68
+ }
66
69
  }.compact
67
70
  end
68
71
 
@@ -1,3 +1,3 @@
1
1
  module NightcrawlerSwift
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -8,6 +8,8 @@ require "rest_client"
8
8
  require "nightcrawler_swift/version"
9
9
  require "nightcrawler_swift/exceptions"
10
10
  require "nightcrawler_swift/ext/hash"
11
+ require "nightcrawler_swift/ext/open_struct"
12
+ require "nightcrawler_swift/ext/nil_class"
11
13
  require "nightcrawler_swift/options"
12
14
  require "nightcrawler_swift/gateway"
13
15
  require "nightcrawler_swift/connection"
@@ -25,5 +25,10 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "rake"
26
26
  spec.add_development_dependency "rspec"
27
27
  spec.add_development_dependency "codeclimate-test-reporter"
28
- spec.add_development_dependency "byebug"
28
+
29
+ if RUBY_VERSION =~ /1\.9/
30
+ spec.add_development_dependency "debugger"
31
+ else
32
+ spec.add_development_dependency "byebug"
33
+ end
29
34
  end
@@ -131,6 +131,16 @@ describe NightcrawlerSwift::CLI::OptParser do
131
131
  end
132
132
  end
133
133
  end
134
+
135
+ ["--no-cache"].each do |switch|
136
+ context switch do
137
+ it "configures use_cache with false" do
138
+ allow(runner).to receive(:argv).and_return([switch])
139
+ subject.parse!
140
+ expect(runner.options.use_cache).to eql false
141
+ end
142
+ end
143
+ end
134
144
  end
135
145
 
136
146
  describe "#parse!" do
@@ -63,7 +63,12 @@ describe NightcrawlerSwift::CLI::Runner do
63
63
  expect(subject.options.config_file).to eql File.expand_path(path)
64
64
  end
65
65
 
66
- it "points to a cache file in the uder home dir" do
66
+ it "enables the use of cache" do
67
+ subject.run
68
+ expect(subject.options.use_cache).to eql true
69
+ end
70
+
71
+ it "points to a cache file in the user home dir" do
67
72
  subject.run
68
73
  path = File.join(config_dir, NightcrawlerSwift::CLI::CACHE_FILE)
69
74
  expect(subject.options.cache_file).to eql File.expand_path(path)
@@ -112,7 +117,9 @@ describe NightcrawlerSwift::CLI::Runner do
112
117
 
113
118
  expect(connection).to receive(:configure).and_call_original
114
119
  subject.run
115
- expect(connection.auth_response).to eql(OpenStruct.new(connection_success_json))
120
+ # This test uses 'eq' instead of 'eql' because in Ruby 1.9.x the method
121
+ # 'equal?' is different than '==' making this test fail
122
+ expect(connection.auth_response).to eq(OpenStruct.new(connection_success_json))
116
123
  end
117
124
  end
118
125
 
@@ -146,6 +153,30 @@ describe NightcrawlerSwift::CLI::Runner do
146
153
  end
147
154
  end
148
155
 
156
+ context "with no-cache flag" do
157
+ before do
158
+ subject.send :configure_default_options
159
+ expect(subject.options.use_cache).to eql true
160
+ allow(subject).to receive(:parse_parameters) { subject.options.use_cache = false }
161
+ end
162
+
163
+ it "doesn't write the auth_response into cache file" do
164
+ expect(File.exist?(cache_file)).to eql false
165
+ allow(connection).to receive(:auth_response).and_return(OpenStruct.new(connection_success_json))
166
+ subject.run
167
+ expect(File.exist?(cache_file)).to eql false
168
+ end
169
+
170
+ it "removes the cache file if it exists" do
171
+ expect(File.exist?(cache_file)).to eql false
172
+ allow(connection).to receive(:auth_response).and_return(OpenStruct.new(connection_success_json))
173
+ File.open(cache_file, "w") {|f| f.write(connection_success_json.to_json)}
174
+ expect(File.exist?(cache_file)).to eql true
175
+ subject.run
176
+ expect(File.exist?(cache_file)).to eql false
177
+ end
178
+ end
179
+
149
180
  it "writes the auth_response into cache file" do
150
181
  expect(File.exist?(cache_file)).to eql false
151
182
  expect(connection).to receive(:auth_response).and_return(OpenStruct.new(connection_success_json))
@@ -176,6 +207,18 @@ describe NightcrawlerSwift::CLI::Runner do
176
207
  end
177
208
  end
178
209
 
210
+ context "when there is an error with connection cache" do
211
+ it "removes the cache file if it exists and allows the creation of a new one" do
212
+ expect(File.exist?(cache_file)).to eql false
213
+ allow(connection).to receive(:auth_response).and_return(OpenStruct.new(connection_success_json))
214
+ File.open(cache_file, "w") {|f| f.write("null")}
215
+ expect(File.exist?(cache_file)).to eql true
216
+ subject.run
217
+ expect(File.exist?(cache_file)).to eql true
218
+ expect(File.read(cache_file)).to eql connection_success_json.to_json
219
+ end
220
+ end
221
+
179
222
  context "when rescue Errno::ENOENT" do
180
223
  before do
181
224
  NightcrawlerSwift.logger = Logger.new(StringIO.new)
@@ -63,7 +63,9 @@ describe NightcrawlerSwift::Connection do
63
63
 
64
64
  it "stores the auth_response" do
65
65
  subject.connect!
66
- expect(subject.auth_response).to eql(OpenStruct.new(auth_success_json))
66
+ # This test uses 'eq' instead of 'eql' because in Ruby 1.9.x the method
67
+ # 'equal?' is different than '==' making this test fail
68
+ expect(subject.auth_response).to eq(OpenStruct.new(auth_success_json))
67
69
  end
68
70
 
69
71
  it "stores the token id" do
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe NightcrawlerSwift::Hash do
3
+ describe NightcrawlerSwift::Ext::Hash do
4
4
 
5
5
  subject do
6
6
  {"key1" => "value1", "key2" => "value2"}
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe NightcrawlerSwift::Ext::NilClass do
4
+
5
+ subject do
6
+ nil
7
+ end
8
+
9
+ describe "#to_h" do
10
+ it "always returns an empty hash" do
11
+ expect(subject.to_h).to eql({})
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe NightcrawlerSwift::Ext::OpenStruct do
4
+
5
+ let :hash do
6
+ {key1: "value", key2: true}
7
+ end
8
+
9
+ subject do
10
+ OpenStruct.new hash
11
+ end
12
+
13
+ describe "#to_h" do
14
+ it "returns a hash with the content" do
15
+ expect(subject.to_h).to eql hash
16
+ end
17
+
18
+ describe "for a blank object" do
19
+ subject do
20
+ OpenStruct.new
21
+ end
22
+
23
+ it "returns an empty hash" do
24
+ expect(subject.to_h).to eql({})
25
+ end
26
+ end
27
+ end
28
+
29
+ end
data/spec/spec_helper.rb CHANGED
@@ -2,7 +2,7 @@ require "codeclimate-test-reporter"
2
2
  CodeClimate::TestReporter.start
3
3
 
4
4
  require "rake"
5
- require "byebug"
5
+ RUBY_VERSION =~ /1\.9/ ? require("debugger") : require("byebug")
6
6
  require "nightcrawler_swift"
7
7
 
8
8
  # This file was generated by the `rspec --init` command. Conventionally, all
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.6.0
4
+ version: 0.7.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-25 00:00:00.000000000 Z
12
+ date: 2015-02-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
@@ -146,6 +146,8 @@ files:
146
146
  - lib/nightcrawler_swift/connection.rb
147
147
  - lib/nightcrawler_swift/exceptions.rb
148
148
  - lib/nightcrawler_swift/ext/hash.rb
149
+ - lib/nightcrawler_swift/ext/nil_class.rb
150
+ - lib/nightcrawler_swift/ext/open_struct.rb
149
151
  - lib/nightcrawler_swift/gateway.rb
150
152
  - lib/nightcrawler_swift/options.rb
151
153
  - lib/nightcrawler_swift/railtie.rb
@@ -170,6 +172,8 @@ files:
170
172
  - spec/lib/nightcrawler_swift/commands/upload_spec.rb
171
173
  - spec/lib/nightcrawler_swift/connection_spec.rb
172
174
  - spec/lib/nightcrawler_swift/ext/hash_spec.rb
175
+ - spec/lib/nightcrawler_swift/ext/nil_class_spec.rb
176
+ - spec/lib/nightcrawler_swift/ext/open_struct_spec.rb
173
177
  - spec/lib/nightcrawler_swift/gateway_spec.rb
174
178
  - spec/lib/nightcrawler_swift/options_spec.rb
175
179
  - spec/lib/nightcrawler_swift/tasks/asset_sync_spec.rb
@@ -219,6 +223,8 @@ test_files:
219
223
  - spec/lib/nightcrawler_swift/commands/upload_spec.rb
220
224
  - spec/lib/nightcrawler_swift/connection_spec.rb
221
225
  - spec/lib/nightcrawler_swift/ext/hash_spec.rb
226
+ - spec/lib/nightcrawler_swift/ext/nil_class_spec.rb
227
+ - spec/lib/nightcrawler_swift/ext/open_struct_spec.rb
222
228
  - spec/lib/nightcrawler_swift/gateway_spec.rb
223
229
  - spec/lib/nightcrawler_swift/options_spec.rb
224
230
  - spec/lib/nightcrawler_swift/tasks/asset_sync_spec.rb