ucloud_storage 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,60 +2,65 @@
2
2
  require 'httparty'
3
3
 
4
4
  module UcloudStorage
5
- class TotalyWrongException < StandardError; end
6
- class NotAuthorized < StandardError; end
7
-
8
- class UcloudStorage
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
15
-
16
- def authorize
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
-
23
- case response.code
24
- when 200
25
- self.storage_url = response.headers["X-Storage-Url"]
26
- self.auth_token = response.headers["X-Auth-Token"]
27
- true
28
- when 401 then false
29
- else raise TotalyWrongException
30
- end
31
- end
32
-
33
- def upload(file_path, box_name, destination)
34
- raise NotAuthorized if storage_url.nil?
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
59
- end
60
- end
5
+ class TotalyWrongException < StandardError; end
6
+ class NotAuthorized < StandardError; end
7
+
8
+ class UcloudStorage
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
15
+
16
+ def authorize
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
+
23
+ case response.code
24
+ when 200
25
+ self.storage_url = response.headers["X-Storage-Url"]
26
+ self.auth_token = response.headers["X-Auth-Token"]
27
+ true
28
+ when 401 then false
29
+ else raise TotalyWrongException
30
+ end
31
+ end
32
+
33
+ def upload(file_path, box_name, destination, &block)
34
+ raise NotAuthorized if storage_url.nil?
35
+
36
+ file = File.new(file_path)
37
+ content_type = `file --mime-type #{file_path}`.split(": ").last
38
+
39
+ response = HTTParty.put(storage_url+ "/#{box_name}/#{destination}",
40
+ headers: {
41
+ "X-Auth-Token" => auth_token,
42
+ "Content-Type" => content_type,
43
+ "Content-Length" => file.size.to_s },
44
+ body: file.read)
45
+
46
+ if response.code == 401 and authorize
47
+ return upload(file_path,box_name,destination,&block)
48
+ end
49
+
50
+ yield response if block_given?
51
+
52
+ response.code == 201 ? true : false
53
+ end
54
+
55
+ def delete(box_name, destination)
56
+ raise NotAuthorized if storage_url.nil?
57
+
58
+ response = HTTParty.delete(storage_url+ "/#{box_name}/#{destination}",
59
+ headers: { "X-Auth-Token" => auth_token })
60
+
61
+ yield response if block_given?
62
+
63
+ response.code == 204 ? true : false
64
+ end
65
+ end
61
66
  end
@@ -1,3 +1,3 @@
1
1
  module UcloudStorage
2
- VERSION = "0.0.9"
2
+ VERSION = "0.0.10"
3
3
  end
@@ -6,138 +6,155 @@ 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
-
29
- let(:valid_ucloud) do
30
- file = File.open(File.join(File.dirname(__FILE__), "/support/auth_info.yml"))
31
- auth_info = YAML.load(file)
32
- ucloud = UcloudStorage.new(user: auth_info["valid_user"]["user"],
33
- pass: auth_info["valid_user"]["pass"])
34
- end
35
-
36
- let(:invalid_ucloud) do
37
- invlaid_ucloud = UcloudStorage.new
38
- invlaid_ucloud.user = "invalid_user@mintshop.com"
39
- invlaid_ucloud.pass = "please download mintshop"
40
- invlaid_ucloud
41
- end
42
-
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
50
- end
51
-
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
66
- end
67
- end
68
-
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
82
- end
83
-
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
92
-
93
- expect {
94
- valid_ucloud.upload(file_path, box, destination)
95
- }.to raise_error(Errno::ENOENT)
96
- end
97
-
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)
106
- end
107
-
108
- it 'yields response' do
109
- VCR.use_cassette('storage/v1/auth') do
110
- valid_ucloud.authorize
111
- end
112
-
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
123
- end
124
-
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
131
-
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
142
- end
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
+
29
+ let(:valid_ucloud) do
30
+ file = File.open(File.join(File.dirname(__FILE__), "/support/auth_info.yml"))
31
+ auth_info = YAML.load(file)
32
+ ucloud = UcloudStorage.new(user: auth_info["valid_user"]["user"],
33
+ pass: auth_info["valid_user"]["pass"])
34
+ end
35
+
36
+ let(:invalid_ucloud) do
37
+ invlaid_ucloud = UcloudStorage.new
38
+ invlaid_ucloud.user = "invalid_user@mintshop.com"
39
+ invlaid_ucloud.pass = "please download mintshop"
40
+ invlaid_ucloud
41
+ end
42
+
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
50
+ end
51
+
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
66
+ end
67
+ end
68
+
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
82
+ end
83
+
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
92
+
93
+ expect {
94
+ valid_ucloud.upload(file_path, box, destination)
95
+ }.to raise_error(Errno::ENOENT)
96
+ end
97
+
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)
106
+ end
107
+
108
+ it "should retry to upload if authorization failure response" do
109
+ VCR.use_cassette('storage/v1/auth') do
110
+ valid_ucloud.authorize
111
+ end
112
+
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
+ valid_ucloud.auth_token += "a"
117
+
118
+ VCR.use_cassette("v1/put_storage_object_with_auth_fail") do
119
+ valid_ucloud.upload(file_path, box, destination) do |response|
120
+ response.code.should == 201
121
+ end
122
+ end
123
+ end
124
+
125
+ it 'yields response' do
126
+ VCR.use_cassette('storage/v1/auth') do
127
+ valid_ucloud.authorize
128
+ end
129
+
130
+ file_path = File.join(File.dirname(__FILE__), "/fixtures/sample_file.txt")
131
+ box = 'dev'
132
+ destination = 'cropped_images/'+Pathname(file_path).basename.to_s
133
+
134
+ VCR.use_cassette("v1/put_storage_object") do
135
+ valid_ucloud.upload(file_path, box, destination) do |response|
136
+ response.code.should == 201
137
+ end
138
+ end
139
+ end
140
+ end
141
+
142
+ describe "#delete" do
143
+ it 'should delete updated object' do
144
+ valid_ucloud.authorize
145
+ file_path = File.join(File.dirname(__FILE__), "/fixtures/sample_file.txt")
146
+ box = 'dev_box'
147
+ destination = 'cropped_images/'+Pathname(file_path).basename.to_s
148
+
149
+ VCR.use_cassette("v1/put_storage_object_02") do
150
+ valid_ucloud.upload(file_path, box, destination)
151
+ end
152
+
153
+ VCR.use_cassette("v1/delete_storage_object_02") do
154
+ valid_ucloud.delete(box, destination) do |response|
155
+ response.code.should == 204
156
+ end.should == true
157
+ end
158
+ end
159
+ end
143
160
  end
@@ -5,7 +5,7 @@ Gem::Specification.new do |gem|
5
5
  gem.authors = ["Sooyong Wang", "Kunha Park"]
6
6
  gem.email = ["wangsy@wangsy.com", "potato9@gmail.com"]
7
7
  gem.description = %q{ucloud storage API}
8
- gem.summary = %q{simple API for authorize, upload files}
8
+ gem.summary = %q{simple API for authorize, upload/delete files}
9
9
  gem.homepage = "https://github.com/wangsy/ucloud-storage"
10
10
 
11
11
  gem.files = `git ls-files`.split($\)
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.9
4
+ version: 0.0.10
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-05-08 00:00:00.000000000 Z
13
+ date: 2013-06-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
- requirement: &70243330790660 !ruby/object:Gem::Requirement
17
+ requirement: &70267624686700 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *70243330790660
25
+ version_requirements: *70267624686700
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: vcr
28
- requirement: &70243330790220 !ruby/object:Gem::Requirement
28
+ requirement: &70267624686280 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70243330790220
36
+ version_requirements: *70267624686280
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: webmock
39
- requirement: &70243330789780 !ruby/object:Gem::Requirement
39
+ requirement: &70267624685860 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *70243330789780
47
+ version_requirements: *70267624685860
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: httparty
50
- requirement: &70243330789300 !ruby/object:Gem::Requirement
50
+ requirement: &70267624685440 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,7 +55,7 @@ dependencies:
55
55
  version: '0'
56
56
  type: :runtime
57
57
  prerelease: false
58
- version_requirements: *70243330789300
58
+ version_requirements: *70267624685440
59
59
  description: ucloud storage API
60
60
  email:
61
61
  - wangsy@wangsy.com
@@ -102,7 +102,7 @@ rubyforge_project: ucloudstorage
102
102
  rubygems_version: 1.8.5
103
103
  signing_key:
104
104
  specification_version: 3
105
- summary: simple API for authorize, upload files
105
+ summary: simple API for authorize, upload/delete files
106
106
  test_files:
107
107
  - spec/fixtures/sample_file.txt
108
108
  - spec/support/auth_info.sample.yml