nightcrawler_swift 0.1.1 → 0.2.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: 79434fd33fdd1553c96cfe7964b9b70dac81f9a2
4
- data.tar.gz: 70ea95d04f973e6cd967a18cc23e4ee4b3c63dfa
3
+ metadata.gz: 45a3c84d56e90690626dab9281b98746d80d2d1a
4
+ data.tar.gz: 9def0f86a48fb9c2d55d5404dff9f2128b9534e5
5
5
  SHA512:
6
- metadata.gz: 56e368d00cafc9d95b2245d801331f56387a78d6d3b4a91a7c0bd66efdbe561bde8544e93de50fde213ace5f4b03c57121f039694fdd51a8fb83daf19a71ae95
7
- data.tar.gz: 9ce11822c56e0f538a19bc5fcb658a385218b63b736f4fb39d5e0e427c5123502b0a145bf74fa9ccba9664037c6b0a6fbf90acb51edc06400b066b95e20193f8
6
+ metadata.gz: 2b05b0540f632e54157710557efb70fb27518b5a88e43e61093ea877b98bf60c4cf662c3cd838398694329eef1f30723f662d2fffb10df03d3edd8d1c701328f
7
+ data.tar.gz: e292d222c897f8a2d24ef1cd42a15123deeedc5a100184bc65d193f7ce5eb6b2ef2f3fa0bbb8f7f9c1791069aa815c8e734695d778d2b1102bc3ecfa011fd76f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nightcrawler_swift (0.1.1)
4
+ nightcrawler_swift (0.2.0)
5
5
  rest-client (>= 1.7.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Nightcrawler Swift
2
2
 
3
- Like the X-Men nightcrawler it teleports your assets to a OpenStack Swift bucket/container
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
 
5
5
  ## Installation
6
6
 
@@ -19,28 +19,25 @@ Or install it yourself as:
19
19
  ## Usage
20
20
 
21
21
  ### With Rails
22
- #### 1) You will need to configure your swift credentials
22
+ #### 1) Configure your swift credentials
23
+
24
+ _In config/application.rb_ or _config/environments/*.rb_
23
25
 
24
26
  ```ruby
25
- NightcrawlerSwift.configure({
26
- bucket: "rogue",
27
- tenant_name: "nightcrawler"
28
- username: "my_username1",
29
- password: "my_password1",
30
- auth_url: "https://auth.url.com:123/v2.0/tokens"
31
- })
27
+ config.nightcrawler_swift.bucket = "rogue"
28
+ config.nightcrawler_swift.tenent_name = "nightcrawler"
29
+ config.nightcrawler_swift.username = "my_username1"
30
+ config.nightcrawler_swift.password = "my_password1"
31
+ config.nightcrawler_swift.auth_url = "https://auth.url.com:123/v2.0/tokens"
32
32
  ```
33
33
 
34
- You could create an initializer with this code.
35
-
36
- #### 2) Load rake tasks into your Rakefile
34
+ By default it will use ```Rails.logger``` as logger, to change that use a different logger in configurations, like:
37
35
 
38
36
  ```ruby
39
- require 'nightcrawler_swift'
40
- load 'nightcrawler_swift/tasks/asset_sync.rake'
37
+ config.nightcrawler_swift.logger = Logger.new(STDOU)
41
38
  ```
42
39
 
43
- #### 3) Profit!
40
+ #### 2) Profit!
44
41
 
45
42
  ```sh
46
43
  rake nightcrawler_swift:rails:asset_sync
@@ -50,11 +47,25 @@ It will invoke ```rake assets:precompile``` and will copy your public directory
50
47
 
51
48
  ### Programatically
52
49
 
53
- #### 1) Configure
50
+ #### 1) Configure your swift credentials
51
+
52
+ ```ruby
53
+ NightcrawlerSwift.configure({
54
+ bucket: "rogue",
55
+ tenant_name: "nightcrawler"
56
+ username: "my_username1",
57
+ password: "my_password1",
58
+ auth_url: "https://auth.url.com:123/v2.0/tokens"
59
+ })
60
+ ```
61
+
62
+ By default it will use ```Logger.new(STDOUT)``` as logger, to change that use:
54
63
 
55
- Repeat the first step of Rails guide
64
+ ```ruby
65
+ NightcrawlerSwift.logger = Logger.new(StringIO.new)
66
+ ```
56
67
 
57
- #### 2) Call method sync with the desired directory
68
+ #### 2) Use method sync with the desired directory
58
69
 
59
70
  ```ruby
60
71
  NightcrawlerSwift.sync File.expand_path("./my-dir")
@@ -62,21 +73,21 @@ NightcrawlerSwift.sync File.expand_path("./my-dir")
62
73
 
63
74
  ## Commands
64
75
 
65
- NightcrawlerSwift packs some useful commands. All commands requires that the gem has already been configured and the connection established.
76
+ NightcrawlerSwift has some useful built-in commands. All commands require the configuration and a established connection.
66
77
 
67
- To Establish the connection, call:
78
+ To Establish the connection, use:
68
79
 
69
80
  ```ruby
70
81
  NightcrawlerSwift.connection.connect!
71
82
  ```
72
83
 
73
- To check if the connection is still valid, call:
84
+ To check if the connection is still valid, use:
74
85
 
75
86
  ```ruby
76
87
  NightcrawlerSwift.connection.connected?
77
88
  ```
78
89
 
79
- To reconnect just call ```NightcrawlerSwift.connection.connect!``` again.
90
+ To reconnect just use ```NightcrawlerSwift.connection.connect!``` again.
80
91
 
81
92
  ### Upload
82
93
 
@@ -86,6 +97,18 @@ upload.execute "my_file_path.txt", File.open("../my_file_fullpath.txt", "r")
86
97
  # true / false
87
98
  ```
88
99
 
100
+ _This upload command was not designed to send very large files_
101
+
102
+ ### Download
103
+
104
+ ```ruby
105
+ download = NightcrawlerSwift::Download.new
106
+ download.execute "my_file_path.txt"
107
+ # File content
108
+ ```
109
+
110
+ _This download command was not designed to retrieve very large files_
111
+
89
112
  ### List
90
113
 
91
114
  ```ruby
@@ -7,9 +7,11 @@ require "nightcrawler_swift/exceptions"
7
7
  require "nightcrawler_swift/command"
8
8
  require "nightcrawler_swift/connection"
9
9
  require "nightcrawler_swift/upload"
10
+ require "nightcrawler_swift/download"
10
11
  require "nightcrawler_swift/list"
11
12
  require "nightcrawler_swift/delete"
12
13
  require "nightcrawler_swift/sync"
14
+ require "nightcrawler_swift/railtie" if defined?(Rails)
13
15
 
14
16
  module NightcrawlerSwift
15
17
  class << self
@@ -1,6 +1,6 @@
1
1
  module NightcrawlerSwift
2
2
  class Connection
3
- attr_accessor :opts, :auth_response, :token_id, :expires_at, :admin_url, :upload_url
3
+ attr_accessor :opts, :auth_response, :token_id, :expires_at, :admin_url, :upload_url, :public_url
4
4
 
5
5
  # Hash with: bucket, tenant_name, username, password, auth_url
6
6
  #
@@ -9,36 +9,41 @@ module NightcrawlerSwift
9
9
  end
10
10
 
11
11
  def connect!
12
- response = RestClient.post(
13
- opts.auth_url,
14
- {
15
- auth: {
16
- tenantName: opts.tenant_name,
17
- passwordCredentials: {username: opts.username, password: opts.password}
18
- }
19
- }.to_json,
20
-
21
- content_type: :json,
22
- accept: :json,
23
- )
12
+ auth_response = authenticate!
24
13
 
25
- @auth_response = OpenStruct.new(JSON.parse(response.body))
26
- @token_id = @auth_response.access["token"]["id"]
27
- @expires_at = @auth_response.access["token"]["expires"]
14
+ @token_id = auth_response.access["token"]["id"]
15
+ @expires_at = auth_response.access["token"]["expires"]
28
16
  @expires_at = DateTime.parse(@expires_at).to_time
29
17
 
30
- @admin_url = @auth_response.access["serviceCatalog"].first["endpoints"].first["adminURL"]
18
+ @admin_url = auth_response.access["serviceCatalog"].first["endpoints"].first["adminURL"]
31
19
  @upload_url = "#{@admin_url}/#{opts.bucket}"
20
+ @public_url = auth_response.access["serviceCatalog"].first["endpoints"].first["publicURL"]
32
21
 
33
22
  NightcrawlerSwift.logger.info "Connected, token_id: #{@token_id}"
34
23
  self
35
-
36
- rescue StandardError => e
37
- raise Exceptions::ConnectionError.new(e)
38
24
  end
39
25
 
40
26
  def connected?
41
27
  !self.token_id.nil? and self.expires_at > Time.now
42
28
  end
29
+
30
+ private
31
+
32
+ def authenticate!
33
+ auth_options = {
34
+ tenantName: opts.tenant_name,
35
+ passwordCredentials: {username: opts.username, password: opts.password}
36
+ }
37
+
38
+ response = RestClient.post(
39
+ opts.auth_url, { auth: auth_options }.to_json,
40
+ content_type: :json,
41
+ accept: :json,
42
+ )
43
+
44
+ OpenStruct.new(JSON.parse(response.body))
45
+ rescue StandardError => e
46
+ raise Exceptions::ConnectionError.new(e)
47
+ end
43
48
  end
44
49
  end
@@ -0,0 +1,16 @@
1
+ module NightcrawlerSwift
2
+ class Download < Command
3
+
4
+ def execute path
5
+ response = get "#{connection.public_url}/#{path}"
6
+ response.body
7
+
8
+ rescue RestClient::ResourceNotFound => e
9
+ raise Exceptions::NotFoundError.new(e)
10
+
11
+ rescue StandardError => e
12
+ raise Exceptions::ConnectionError.new(e)
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ require "rails"
2
+ require "active_support/ordered_hash"
3
+
4
+ module NightcrawlerSwift
5
+ class Railtie < Rails::Railtie
6
+ railtie_name :nightcrawler_swift
7
+ config.nightcrawler_swift = ActiveSupport::OrderedOptions.new
8
+
9
+ rake_tasks do
10
+ load "nightcrawler_swift/tasks/asset_sync.rake"
11
+ end
12
+
13
+ initializer "nightcrawler_swift.configure" do |app|
14
+ settings = app.config.nightcrawler_swift
15
+ logger = settings.delete(:logger) || Rails.logger
16
+
17
+ NightcrawlerSwift.configure settings
18
+ NightcrawlerSwift.logger = logger
19
+ end
20
+ end
21
+ end
@@ -3,7 +3,7 @@ require "nightcrawler_swift"
3
3
  namespace :nightcrawler_swift do
4
4
  namespace :rails do
5
5
 
6
- desc "Synchronizes the assets"
6
+ desc "Synchronizes the public directory with OpenStack Swift"
7
7
  task asset_sync: ["assets:precompile", "environment"] do
8
8
  raise unless defined?(Rails)
9
9
  NightcrawlerSwift.sync File.join(Rails.root, "public")
@@ -1,3 +1,3 @@
1
1
  module NightcrawlerSwift
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -6,9 +6,9 @@ require 'nightcrawler_swift/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "nightcrawler_swift"
8
8
  spec.version = NightcrawlerSwift::VERSION
9
- spec.authors = ["tulios", "roberto"]
9
+ spec.authors = ["tulios", "robertosoares"]
10
10
  spec.email = ["ornelas.tulio@gmail.com", "roberto.tech@gmail.com"]
11
- spec.summary = %q{Like the X-Men nightcrawler it teleports your assets to a OpenStack Swift bucket/container}
11
+ spec.summary = %q{Like the X-Men nightcrawler this gem teleports your assets to a OpenStack Swift bucket/container}
12
12
  spec.description = spec.summary
13
13
  spec.homepage = "https://github.com/tulios/nightcrawler_swift"
14
14
  spec.license = "MIT"
@@ -60,16 +60,6 @@ describe NightcrawlerSwift::Connection do
60
60
  and_return(auth_success_response)
61
61
  end
62
62
 
63
- it "stores the auth response" do
64
- subject.connect!
65
- expect(subject.auth_response).to_not be_nil
66
-
67
-
68
- auth_success_json.keys.each do |key|
69
- expect(subject.auth_response.send(key)).to eql(auth_success_json[key])
70
- end
71
- end
72
-
73
63
  it "stores the token id" do
74
64
  subject.connect!
75
65
  expect(subject.token_id).to eql(auth_success_json["access"]["token"]["id"])
@@ -91,6 +81,11 @@ describe NightcrawlerSwift::Connection do
91
81
  expect(subject.upload_url).to eql("#{subject.admin_url}/#{opts[:bucket]}")
92
82
  end
93
83
 
84
+ it "stores the public_url" do
85
+ subject.connect!
86
+ expect(subject.public_url).to eql(auth_success_json["access"]["serviceCatalog"].first["endpoints"].first["publicURL"])
87
+ end
88
+
94
89
  it "returns self" do
95
90
  expect(subject.connect!).to eql(subject)
96
91
  end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe NightcrawlerSwift::Download do
4
+
5
+ subject do
6
+ NightcrawlerSwift::Download.new
7
+ end
8
+
9
+ describe "#execute" do
10
+ let :connection do
11
+ double :connection, public_url: "server-url"
12
+ end
13
+
14
+ let :execute do
15
+ subject.execute "file_path"
16
+ end
17
+
18
+ before do
19
+ allow(NightcrawlerSwift).to receive(:connection).and_return(connection)
20
+ allow(subject).to receive(:get).and_return(response)
21
+ end
22
+
23
+ context "success" do
24
+ let :response do
25
+ double(:response, code: 200, body: "content")
26
+ end
27
+
28
+ it "gets using upload url" do
29
+ execute
30
+ expect(subject).to have_received(:get).with("server-url/file_path")
31
+ end
32
+
33
+ it "returns body" do
34
+ expect(execute).to eql "content"
35
+ end
36
+ end
37
+
38
+ context "when the file does not exist" do
39
+ let :response do
40
+ double(:response, code: 404)
41
+ end
42
+
43
+ before do
44
+ allow(subject).to receive(:get).and_raise(RestClient::ResourceNotFound.new(response))
45
+ end
46
+
47
+ it "raises NightcrawlerSwift::Exceptions::NotFoundError" do
48
+ expect { execute }.to raise_error NightcrawlerSwift::Exceptions::NotFoundError
49
+ end
50
+ end
51
+
52
+ context "when another error happens" do
53
+ let :response do
54
+ double(:response, code: 500)
55
+ end
56
+
57
+ before do
58
+ allow(subject).to receive(:get).and_raise(RuntimeError.new(response))
59
+ end
60
+
61
+ it "raises NightcrawlerSwift::Exceptions::ConnectionError" do
62
+ expect { execute }.to raise_error NightcrawlerSwift::Exceptions::ConnectionError
63
+ end
64
+ end
65
+ end
66
+ end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nightcrawler_swift
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tulios
8
- - roberto
8
+ - robertosoares
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-08-26 00:00:00.000000000 Z
12
+ date: 2014-08-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
@@ -81,8 +81,8 @@ dependencies:
81
81
  - - ">="
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
- description: Like the X-Men nightcrawler it teleports your assets to a OpenStack Swift
85
- bucket/container
84
+ description: Like the X-Men nightcrawler this gem teleports your assets to a OpenStack
85
+ Swift bucket/container
86
86
  email:
87
87
  - ornelas.tulio@gmail.com
88
88
  - roberto.tech@gmail.com
@@ -103,8 +103,10 @@ files:
103
103
  - lib/nightcrawler_swift/command.rb
104
104
  - lib/nightcrawler_swift/connection.rb
105
105
  - lib/nightcrawler_swift/delete.rb
106
+ - lib/nightcrawler_swift/download.rb
106
107
  - lib/nightcrawler_swift/exceptions.rb
107
108
  - lib/nightcrawler_swift/list.rb
109
+ - lib/nightcrawler_swift/railtie.rb
108
110
  - lib/nightcrawler_swift/sync.rb
109
111
  - lib/nightcrawler_swift/tasks/asset_sync.rake
110
112
  - lib/nightcrawler_swift/upload.rb
@@ -117,6 +119,7 @@ files:
117
119
  - spec/lib/nightcrawler_swift/command_spec.rb
118
120
  - spec/lib/nightcrawler_swift/connection_spec.rb
119
121
  - spec/lib/nightcrawler_swift/delete_spec.rb
122
+ - spec/lib/nightcrawler_swift/download_spec.rb
120
123
  - spec/lib/nightcrawler_swift/list_spec.rb
121
124
  - spec/lib/nightcrawler_swift/sync_spec.rb
122
125
  - spec/lib/nightcrawler_swift/upload_spec.rb
@@ -145,8 +148,8 @@ rubyforge_project:
145
148
  rubygems_version: 2.2.2
146
149
  signing_key:
147
150
  specification_version: 4
148
- summary: Like the X-Men nightcrawler it teleports your assets to a OpenStack Swift
149
- bucket/container
151
+ summary: Like the X-Men nightcrawler this gem teleports your assets to a OpenStack
152
+ Swift bucket/container
150
153
  test_files:
151
154
  - spec/fixtures/assets/ex1.txt
152
155
  - spec/fixtures/assets/ex2.txt
@@ -155,6 +158,7 @@ test_files:
155
158
  - spec/lib/nightcrawler_swift/command_spec.rb
156
159
  - spec/lib/nightcrawler_swift/connection_spec.rb
157
160
  - spec/lib/nightcrawler_swift/delete_spec.rb
161
+ - spec/lib/nightcrawler_swift/download_spec.rb
158
162
  - spec/lib/nightcrawler_swift/list_spec.rb
159
163
  - spec/lib/nightcrawler_swift/sync_spec.rb
160
164
  - spec/lib/nightcrawler_swift/upload_spec.rb