ucloud_storage 0.0.8 → 0.0.9
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.
- data/README.md +21 -3
- data/lib/ucloud_storage.rb +9 -2
- data/lib/ucloud_storage/configuration.rb +14 -0
- data/lib/ucloud_storage/ucloud_storage.rb +39 -23
- data/lib/ucloud_storage/version.rb +1 -1
- data/spec/support/vcr.rb +1 -0
- data/spec/ucloud_storage_spec.rb +105 -40
- metadata +30 -69
- data/Gemfile.lock +0 -38
data/README.md
CHANGED
@@ -18,11 +18,29 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
ucloud = UcloudStorage.new
|
22
|
-
|
23
|
-
ucloud.pass = "API KEY"
|
21
|
+
ucloud = UcloudStorage.new(user: 'email', pass: 'API_KEY')
|
22
|
+
|
24
23
|
ucloud.authoize
|
25
24
|
ucloud.upload(filepath, boxname, destination)
|
25
|
+
ucloud.delte(boxname, destination)
|
26
|
+
|
27
|
+
## Configuration
|
28
|
+
|
29
|
+
Set default user/pass info
|
30
|
+
|
31
|
+
UcloudStorage.configure do |config|
|
32
|
+
config.user = 'email'
|
33
|
+
config.pass = 'API KEY'
|
34
|
+
end
|
35
|
+
|
36
|
+
## Response block
|
37
|
+
|
38
|
+
Every request yields a response
|
39
|
+
|
40
|
+
ucloud.upload(filepath, boxname, dest) do |response|
|
41
|
+
response_code = response.code
|
42
|
+
end
|
43
|
+
|
26
44
|
|
27
45
|
## Contributing
|
28
46
|
|
data/lib/ucloud_storage.rb
CHANGED
@@ -1,8 +1,15 @@
|
|
1
1
|
require_relative 'ucloud_storage/ucloud_storage'
|
2
|
+
require_relative 'ucloud_storage/configuration'
|
2
3
|
require_relative 'ucloud_storage/version'
|
4
|
+
require 'forwardable'
|
3
5
|
|
4
6
|
module UcloudStorage
|
5
|
-
|
6
|
-
|
7
|
+
class << self
|
8
|
+
extend Forwardable
|
9
|
+
def_delegators Configuration, :user, :pass, :configure
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.new(options={})
|
13
|
+
UcloudStorage.new(options)
|
7
14
|
end
|
8
15
|
end
|
@@ -6,40 +6,56 @@ module UcloudStorage
|
|
6
6
|
class NotAuthorized < StandardError; end
|
7
7
|
|
8
8
|
class UcloudStorage
|
9
|
-
attr_accessor :user, :pass, :storage_url, :auth_token
|
9
|
+
attr_accessor :user, :pass, :storage_url, :auth_token
|
10
|
+
|
11
|
+
def initialize(options={})
|
12
|
+
@user = options.fetch(:user) { Configuration.user }
|
13
|
+
@pass = options.fetch(:pass) { Configuration.pass }
|
14
|
+
end
|
10
15
|
|
11
16
|
def authorize
|
12
|
-
response = HTTParty.get("https://api.ucloudbiz.olleh.com/storage/v1/auth/",
|
13
|
-
|
17
|
+
response = HTTParty.get("https://api.ucloudbiz.olleh.com/storage/v1/auth/",
|
18
|
+
headers: { "X-Storage-User" => user,
|
19
|
+
"X-Storage-Pass" => pass })
|
20
|
+
|
21
|
+
yield response if block_given?
|
22
|
+
|
14
23
|
case response.code
|
15
|
-
when 200
|
24
|
+
when 200
|
16
25
|
self.storage_url = response.headers["X-Storage-Url"]
|
17
26
|
self.auth_token = response.headers["X-Auth-Token"]
|
18
27
|
true
|
19
|
-
when 401 then
|
20
|
-
|
21
|
-
else
|
22
|
-
raise TotalyWrongException
|
28
|
+
when 401 then false
|
29
|
+
else raise TotalyWrongException
|
23
30
|
end
|
24
31
|
end
|
25
32
|
|
26
33
|
def upload(file_path, box_name, destination)
|
27
34
|
raise NotAuthorized if storage_url.nil?
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
35
|
+
|
36
|
+
file = File.new(file_path)
|
37
|
+
content_type = `file --mime-type #{file_path}`.split(": ").last
|
38
|
+
response = HTTParty.put(storage_url+ "/#{box_name}/#{destination}",
|
39
|
+
headers: {
|
40
|
+
"X-Auth-Token" => auth_token,
|
41
|
+
"Content-Type" => content_type,
|
42
|
+
"Content-Length" => file.size.to_s },
|
43
|
+
body: file.read)
|
44
|
+
|
45
|
+
yield response if block_given?
|
46
|
+
|
47
|
+
response.code == 201 ? true : false
|
48
|
+
end
|
49
|
+
|
50
|
+
def delete(box_name, destination)
|
51
|
+
raise NotAuthorized if storage_url.nil?
|
52
|
+
|
53
|
+
response = HTTParty.delete(storage_url+ "/#{box_name}/#{destination}",
|
54
|
+
headers: { "X-Auth-Token" => auth_token })
|
55
|
+
|
56
|
+
yield response if block_given?
|
57
|
+
|
58
|
+
response.code == 204 ? true : false
|
43
59
|
end
|
44
60
|
end
|
45
61
|
end
|
data/spec/support/vcr.rb
CHANGED
data/spec/ucloud_storage_spec.rb
CHANGED
@@ -6,13 +6,31 @@ require 'support/vcr'
|
|
6
6
|
require 'yaml'
|
7
7
|
|
8
8
|
describe UcloudStorage do
|
9
|
+
context "set user/pass info" do
|
10
|
+
before do
|
11
|
+
UcloudStorage.configure do |config|
|
12
|
+
config.user = "abc@mintshop.com"
|
13
|
+
config.pass = "my_api_key"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
it "can set default user/pass info" do
|
18
|
+
UcloudStorage.user.should == "abc@mintshop.com"
|
19
|
+
UcloudStorage.pass.should == "my_api_key"
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'uses default uesr/pass info when initialized' do
|
23
|
+
ucloud = UcloudStorage.new
|
24
|
+
ucloud.user.should == "abc@mintshop.com"
|
25
|
+
ucloud.pass.should == "my_api_key"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
9
29
|
let(:valid_ucloud) do
|
10
|
-
ucloud = UcloudStorage.new
|
11
30
|
file = File.open(File.join(File.dirname(__FILE__), "/support/auth_info.yml"))
|
12
31
|
auth_info = YAML.load(file)
|
13
|
-
ucloud.user
|
14
|
-
|
15
|
-
ucloud
|
32
|
+
ucloud = UcloudStorage.new(user: auth_info["valid_user"]["user"],
|
33
|
+
pass: auth_info["valid_user"]["pass"])
|
16
34
|
end
|
17
35
|
|
18
36
|
let(:invalid_ucloud) do
|
@@ -22,57 +40,104 @@ describe UcloudStorage do
|
|
22
40
|
invlaid_ucloud
|
23
41
|
end
|
24
42
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
43
|
+
describe '#authorize' do
|
44
|
+
it "can authorize with valid user/pass" do
|
45
|
+
VCR.use_cassette("storage/v1/auth") do
|
46
|
+
valid_ucloud.authorize.should == true
|
47
|
+
valid_ucloud.storage_url.should_not be_nil
|
48
|
+
valid_ucloud.auth_token.should_not be_nil
|
49
|
+
end
|
30
50
|
end
|
31
|
-
end
|
32
51
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
52
|
+
it "cannot authorize with invalid user/pass" do
|
53
|
+
VCR.use_cassette("storage/v1/auth_fail") do
|
54
|
+
invalid_ucloud.authorize.should == false
|
55
|
+
invalid_ucloud.storage_url.should be_nil
|
56
|
+
invalid_ucloud.auth_token.should be_nil
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'yields response' do
|
61
|
+
VCR.use_cassette("storage/v1/auth") do
|
62
|
+
valid_ucloud.authorize do |response|
|
63
|
+
response.code.should == 200
|
64
|
+
end
|
65
|
+
end
|
38
66
|
end
|
39
67
|
end
|
40
68
|
|
41
|
-
|
42
|
-
|
43
|
-
|
69
|
+
describe "#upload" do
|
70
|
+
it "can upload a file" do
|
71
|
+
VCR.use_cassette('storage/v1/auth') do
|
72
|
+
valid_ucloud.authorize
|
73
|
+
end
|
74
|
+
|
75
|
+
file_path = File.join(File.dirname(__FILE__), "/fixtures/sample_file.txt")
|
76
|
+
box = 'dev'
|
77
|
+
destination = 'cropped_images/'+Pathname(file_path).basename.to_s
|
78
|
+
|
79
|
+
VCR.use_cassette("v1/put_storage_object") do
|
80
|
+
valid_ucloud.upload(file_path, box, destination).should be_true
|
81
|
+
end
|
44
82
|
end
|
45
83
|
|
46
|
-
|
47
|
-
|
48
|
-
|
84
|
+
it "should fail to upload with invalid file path" do
|
85
|
+
VCR.use_cassette('storage/v1/auth') do
|
86
|
+
valid_ucloud.authorize
|
87
|
+
end
|
88
|
+
|
89
|
+
file_path = File.join(File.dirname(__FILE__), "/fixtures/no_sample_file.txt")
|
90
|
+
box = 'dev'
|
91
|
+
destination = 'cropped_images/'+Pathname(file_path).basename.to_s
|
49
92
|
|
50
|
-
|
51
|
-
|
93
|
+
expect {
|
94
|
+
valid_ucloud.upload(file_path, box, destination)
|
95
|
+
}.to raise_error(Errno::ENOENT)
|
52
96
|
end
|
53
|
-
end
|
54
97
|
|
55
|
-
|
56
|
-
|
57
|
-
|
98
|
+
it "should fail to upload without authorization" do
|
99
|
+
file_path = File.join(File.dirname(__FILE__), "/fixtures/sample_file.txt")
|
100
|
+
box = 'dev'
|
101
|
+
destination = 'cropped_images/'+Pathname(file_path).basename.to_s
|
102
|
+
|
103
|
+
expect {
|
104
|
+
valid_ucloud.upload(file_path, box, destination).should be_true
|
105
|
+
}.to raise_error(UcloudStorage::NotAuthorized)
|
58
106
|
end
|
59
107
|
|
60
|
-
|
61
|
-
|
62
|
-
|
108
|
+
it 'yields response' do
|
109
|
+
VCR.use_cassette('storage/v1/auth') do
|
110
|
+
valid_ucloud.authorize
|
111
|
+
end
|
63
112
|
|
64
|
-
|
65
|
-
|
66
|
-
|
113
|
+
file_path = File.join(File.dirname(__FILE__), "/fixtures/sample_file.txt")
|
114
|
+
box = 'dev'
|
115
|
+
destination = 'cropped_images/'+Pathname(file_path).basename.to_s
|
116
|
+
|
117
|
+
VCR.use_cassette("v1/put_storage_object") do
|
118
|
+
valid_ucloud.upload(file_path, box, destination) do |response|
|
119
|
+
response.code.should == 201
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
67
123
|
end
|
68
124
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
125
|
+
describe "#delete" do
|
126
|
+
it 'should delete updated object' do
|
127
|
+
valid_ucloud.authorize
|
128
|
+
file_path = File.join(File.dirname(__FILE__), "/fixtures/sample_file.txt")
|
129
|
+
box = 'dev_box'
|
130
|
+
destination = 'cropped_images/'+Pathname(file_path).basename.to_s
|
73
131
|
|
74
|
-
|
75
|
-
|
76
|
-
|
132
|
+
VCR.use_cassette("v1/put_storage_object_02") do
|
133
|
+
valid_ucloud.upload(file_path, box, destination)
|
134
|
+
end
|
135
|
+
|
136
|
+
VCR.use_cassette("v1/delete_storage_object_02") do
|
137
|
+
valid_ucloud.delete(box, destination) do |response|
|
138
|
+
response.code.should == 204
|
139
|
+
end.should == true
|
140
|
+
end
|
141
|
+
end
|
77
142
|
end
|
78
143
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ucloud_storage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-
|
13
|
+
date: 2013-05-08 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rspec
|
17
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirement: &70243330790660 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,15 +22,10 @@ dependencies:
|
|
22
22
|
version: '0'
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
|
-
version_requirements:
|
26
|
-
none: false
|
27
|
-
requirements:
|
28
|
-
- - ! '>='
|
29
|
-
- !ruby/object:Gem::Version
|
30
|
-
version: '0'
|
25
|
+
version_requirements: *70243330790660
|
31
26
|
- !ruby/object:Gem::Dependency
|
32
27
|
name: vcr
|
33
|
-
requirement: !ruby/object:Gem::Requirement
|
28
|
+
requirement: &70243330790220 !ruby/object:Gem::Requirement
|
34
29
|
none: false
|
35
30
|
requirements:
|
36
31
|
- - ! '>='
|
@@ -38,15 +33,10 @@ dependencies:
|
|
38
33
|
version: '0'
|
39
34
|
type: :development
|
40
35
|
prerelease: false
|
41
|
-
version_requirements:
|
42
|
-
none: false
|
43
|
-
requirements:
|
44
|
-
- - ! '>='
|
45
|
-
- !ruby/object:Gem::Version
|
46
|
-
version: '0'
|
36
|
+
version_requirements: *70243330790220
|
47
37
|
- !ruby/object:Gem::Dependency
|
48
38
|
name: webmock
|
49
|
-
requirement: !ruby/object:Gem::Requirement
|
39
|
+
requirement: &70243330789780 !ruby/object:Gem::Requirement
|
50
40
|
none: false
|
51
41
|
requirements:
|
52
42
|
- - ! '>='
|
@@ -54,15 +44,10 @@ dependencies:
|
|
54
44
|
version: '0'
|
55
45
|
type: :development
|
56
46
|
prerelease: false
|
57
|
-
version_requirements:
|
58
|
-
none: false
|
59
|
-
requirements:
|
60
|
-
- - ! '>='
|
61
|
-
- !ruby/object:Gem::Version
|
62
|
-
version: '0'
|
47
|
+
version_requirements: *70243330789780
|
63
48
|
- !ruby/object:Gem::Dependency
|
64
49
|
name: httparty
|
65
|
-
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirement: &70243330789300 !ruby/object:Gem::Requirement
|
66
51
|
none: false
|
67
52
|
requirements:
|
68
53
|
- - ! '>='
|
@@ -70,12 +55,7 @@ dependencies:
|
|
70
55
|
version: '0'
|
71
56
|
type: :runtime
|
72
57
|
prerelease: false
|
73
|
-
version_requirements:
|
74
|
-
none: false
|
75
|
-
requirements:
|
76
|
-
- - ! '>='
|
77
|
-
- !ruby/object:Gem::Version
|
78
|
-
version: '0'
|
58
|
+
version_requirements: *70243330789300
|
79
59
|
description: ucloud storage API
|
80
60
|
email:
|
81
61
|
- wangsy@wangsy.com
|
@@ -84,36 +64,21 @@ executables: []
|
|
84
64
|
extensions: []
|
85
65
|
extra_rdoc_files: []
|
86
66
|
files:
|
87
|
-
-
|
88
|
-
|
89
|
-
-
|
90
|
-
|
91
|
-
-
|
92
|
-
|
93
|
-
-
|
94
|
-
|
95
|
-
-
|
96
|
-
|
97
|
-
-
|
98
|
-
|
99
|
-
-
|
100
|
-
|
101
|
-
-
|
102
|
-
bGliL3VjbG91ZF9zdG9yYWdlLnJi
|
103
|
-
- !binary |-
|
104
|
-
bGliL3VjbG91ZF9zdG9yYWdlL3VjbG91ZF9zdG9yYWdlLnJi
|
105
|
-
- !binary |-
|
106
|
-
bGliL3VjbG91ZF9zdG9yYWdlL3ZlcnNpb24ucmI=
|
107
|
-
- !binary |-
|
108
|
-
c3BlYy9maXh0dXJlcy9zYW1wbGVfZmlsZS50eHQ=
|
109
|
-
- !binary |-
|
110
|
-
c3BlYy9zdXBwb3J0L2F1dGhfaW5mby5zYW1wbGUueW1s
|
111
|
-
- !binary |-
|
112
|
-
c3BlYy9zdXBwb3J0L3Zjci5yYg==
|
113
|
-
- !binary |-
|
114
|
-
c3BlYy91Y2xvdWRfc3RvcmFnZV9zcGVjLnJi
|
115
|
-
- !binary |-
|
116
|
-
dWNsb3VkX3N0b3JhZ2UuZ2Vtc3BlYw==
|
67
|
+
- .gitignore
|
68
|
+
- CHANGELOG
|
69
|
+
- Gemfile
|
70
|
+
- LICENSE
|
71
|
+
- README.md
|
72
|
+
- Rakefile
|
73
|
+
- lib/ucloud_storage.rb
|
74
|
+
- lib/ucloud_storage/configuration.rb
|
75
|
+
- lib/ucloud_storage/ucloud_storage.rb
|
76
|
+
- lib/ucloud_storage/version.rb
|
77
|
+
- spec/fixtures/sample_file.txt
|
78
|
+
- spec/support/auth_info.sample.yml
|
79
|
+
- spec/support/vcr.rb
|
80
|
+
- spec/ucloud_storage_spec.rb
|
81
|
+
- ucloud_storage.gemspec
|
117
82
|
homepage: https://github.com/wangsy/ucloud-storage
|
118
83
|
licenses: []
|
119
84
|
post_install_message:
|
@@ -134,16 +99,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
134
99
|
version: '0'
|
135
100
|
requirements: []
|
136
101
|
rubyforge_project: ucloudstorage
|
137
|
-
rubygems_version: 1.8.
|
102
|
+
rubygems_version: 1.8.5
|
138
103
|
signing_key:
|
139
104
|
specification_version: 3
|
140
105
|
summary: simple API for authorize, upload files
|
141
106
|
test_files:
|
142
|
-
-
|
143
|
-
|
144
|
-
-
|
145
|
-
|
146
|
-
- !binary |-
|
147
|
-
c3BlYy9zdXBwb3J0L3Zjci5yYg==
|
148
|
-
- !binary |-
|
149
|
-
c3BlYy91Y2xvdWRfc3RvcmFnZV9zcGVjLnJi
|
107
|
+
- spec/fixtures/sample_file.txt
|
108
|
+
- spec/support/auth_info.sample.yml
|
109
|
+
- spec/support/vcr.rb
|
110
|
+
- spec/ucloud_storage_spec.rb
|
data/Gemfile.lock
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
ucloud_storage (0.0.7)
|
5
|
-
httparty
|
6
|
-
|
7
|
-
GEM
|
8
|
-
remote: https://rubygems.org/
|
9
|
-
specs:
|
10
|
-
addressable (2.3.4)
|
11
|
-
crack (0.3.2)
|
12
|
-
diff-lcs (1.2.4)
|
13
|
-
httparty (0.11.0)
|
14
|
-
multi_json (~> 1.0)
|
15
|
-
multi_xml (>= 0.5.2)
|
16
|
-
multi_json (1.7.2)
|
17
|
-
multi_xml (0.5.3)
|
18
|
-
rspec (2.13.0)
|
19
|
-
rspec-core (~> 2.13.0)
|
20
|
-
rspec-expectations (~> 2.13.0)
|
21
|
-
rspec-mocks (~> 2.13.0)
|
22
|
-
rspec-core (2.13.1)
|
23
|
-
rspec-expectations (2.13.0)
|
24
|
-
diff-lcs (>= 1.1.3, < 2.0)
|
25
|
-
rspec-mocks (2.13.1)
|
26
|
-
vcr (2.4.0)
|
27
|
-
webmock (1.11.0)
|
28
|
-
addressable (>= 2.2.7)
|
29
|
-
crack (>= 0.3.2)
|
30
|
-
|
31
|
-
PLATFORMS
|
32
|
-
ruby
|
33
|
-
|
34
|
-
DEPENDENCIES
|
35
|
-
rspec
|
36
|
-
ucloud_storage!
|
37
|
-
vcr
|
38
|
-
webmock
|