qiniu-rs 1.0.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.
data/lib/qiniu/rs.rb ADDED
@@ -0,0 +1,114 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'qiniu/rs/version'
4
+
5
+ module Qiniu
6
+ module RS
7
+ autoload :Config, 'qiniu/rs/config'
8
+ autoload :Log, 'qiniu/rs/log'
9
+ autoload :Exception, 'qiniu/rs/exceptions'
10
+ autoload :Utils, 'qiniu/rs/utils'
11
+ autoload :Auth, 'qiniu/rs/auth'
12
+ autoload :IO, 'qiniu/rs/io'
13
+ autoload :RS, 'qiniu/rs/rs'
14
+ autoload :Image, 'qiniu/rs/image'
15
+
16
+ class << self
17
+
18
+ StatusOK = 200
19
+
20
+ def establish_connection!(opts = {})
21
+ Config.initialize_connect opts
22
+ end
23
+
24
+ def login!(user, pwd)
25
+ code, data = Auth.exchange_by_password!(user, pwd)
26
+ code == StatusOK
27
+ end
28
+
29
+ def put_auth(expires_in = nil, callback_url = nil)
30
+ code, data = IO.put_auth(expires_in, callback_url)
31
+ code == StatusOK ? data["url"] : false
32
+ end
33
+
34
+ def upload(url, local_file, bucket = '', key = '', mime_type = '', custom_meta = '', callback_params = {})
35
+ code, data = IO.put_file(url, local_file, bucket, key, mime_type, custom_meta, callback_params)
36
+ code == StatusOK
37
+ end
38
+
39
+ def stat(bucket, key)
40
+ code, data = RS.stat(bucket, key)
41
+ code == StatusOK ? data : false
42
+ end
43
+
44
+ def get(bucket, key, save_as = nil, expires_in = nil, version = nil)
45
+ code, data = RS.get(bucket, key, save_as, expires_in, version)
46
+ code == StatusOK ? data : false
47
+ end
48
+
49
+ def download(bucket, key, save_as = nil, expires_in = nil, version = nil)
50
+ code, data = RS.get(bucket, key, save_as, expires_in, version)
51
+ code == StatusOK ? data["url"] : false
52
+ end
53
+
54
+ def delete(bucket, key)
55
+ code, data = RS.delete(bucket, key)
56
+ code == StatusOK
57
+ end
58
+
59
+ def batch(command, bucket, keys)
60
+ code, data = RS.batch(command, bucket, keys)
61
+ code == StatusOK ? data : false
62
+ end
63
+
64
+ def batch_stat(bucket, keys)
65
+ code, data = RS.batch_stat(bucket, keys)
66
+ code == StatusOK ? data : false
67
+ end
68
+
69
+ def batch_get(bucket, keys)
70
+ code, data = RS.batch_get(bucket, keys)
71
+ code == StatusOK ? data : false
72
+ end
73
+
74
+ def batch_download(bucket, keys)
75
+ code, data = RS.batch_get(bucket, keys)
76
+ return false unless code == StatusOK
77
+ links = []
78
+ data.each { |e| links << e["data"]["url"] }
79
+ links
80
+ end
81
+
82
+ def batch_delete(bucket, keys)
83
+ code, data = RS.batch_delete(bucket, keys)
84
+ code == StatusOK ? data : false
85
+ end
86
+
87
+ def publish(domain, bucket)
88
+ code, data = RS.publish(domain, bucket)
89
+ code == StatusOK
90
+ end
91
+
92
+ def unpublish(domain)
93
+ code, data = RS.unpublish(domain)
94
+ code == StatusOK
95
+ end
96
+
97
+ def drop(bucket)
98
+ code, data = RS.drop(bucket)
99
+ code == StatusOK
100
+ end
101
+
102
+ def image_info(url)
103
+ code, data = Image.info(url)
104
+ code == StatusOK ? data : false
105
+ end
106
+
107
+ def image_preview_url(url, spec)
108
+ Image.preivew_url(url, spec)
109
+ end
110
+
111
+ end
112
+
113
+ end
114
+ end
data/lib/qiniu-rs.rb ADDED
@@ -0,0 +1,2 @@
1
+ # More logical way to require 'qiniu-rs'
2
+ require File.join(File.dirname(__FILE__), 'qiniu', 'rs')
data/qiniu-rs.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('../lib/qiniu/rs/version', __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.authors = ["why404"]
7
+ gem.email = ["why404@gmail.com"]
8
+ gem.description = %q{Qiniu Cloud Storage SDK for Ruby. See: http://docs.qiniutek.com/v1/sdk/ruby/}
9
+ gem.summary = %q{Qiniu Cloud Storage SDK for Ruby}
10
+ gem.homepage = "https://github.com/why404/qiniu-rs-sdk-for-ruby"
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "qiniu-rs"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = Qiniu::RS::VERSION
18
+
19
+ # specify any dependencies here; for example:
20
+ gem.add_development_dependency "rspec"
21
+ gem.add_development_dependency "fakeweb"
22
+ gem.add_runtime_dependency "rest-client"
23
+ gem.add_runtime_dependency "mime-types"
24
+ end
@@ -0,0 +1,58 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require 'qiniu/rs/auth'
5
+
6
+ module Qiniu
7
+ module RS
8
+ describe Auth do
9
+
10
+ before :all do
11
+ @username = "test@qbox.net"
12
+ @password = "test"
13
+
14
+ code, data = Qiniu::RS::Auth.exchange_by_password!(@username, @password)
15
+ code.should == 200
16
+ data.should be_an_instance_of(Hash)
17
+ data["access_token"].should_not be_empty
18
+ data["refresh_token"].should_not be_empty
19
+ data["refresh_token"].should_not be_empty
20
+
21
+ @access_token = data["access_token"]
22
+ @refresh_token = data["refresh_token"]
23
+ puts data.inspect
24
+ end
25
+
26
+ context ".exchange_by_password" do
27
+ it "should sign in failed when pass a non-existent username" do
28
+ code, data = Qiniu::RS::Auth.exchange_by_password!("a_non_existent_user@example.com", "password")
29
+ code.should == 401
30
+ data["error_code"].should == 11
31
+ data["error"].should == "failed_authentication"
32
+ puts data.inspect
33
+ end
34
+
35
+ it "should sign in failed when pass a wrong password" do
36
+ code, data = Qiniu::RS::Auth.exchange_by_password!(@username, "a-wrong-password")
37
+ code.should == 401
38
+ data["error_code"].should == 11
39
+ data["error"].should == "failed_authentication"
40
+ puts data.inspect
41
+ end
42
+ end
43
+
44
+ context ".exchange_by_refresh_token" do
45
+ it "should works" do
46
+ @refresh_token.should_not be_empty
47
+ code, data = Qiniu::RS::Auth.exchange_by_refresh_token!(@refresh_token)
48
+ code.should == 200
49
+ data["access_token"].should_not be_empty
50
+ data["refresh_token"].should_not be_empty
51
+ data["expires_in"].should_not be_zero
52
+ puts data.inspect
53
+ end
54
+ end
55
+
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,41 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require 'qiniu/rs/auth'
5
+ require 'qiniu/rs/rs'
6
+ require 'qiniu/rs/image'
7
+
8
+ module Qiniu
9
+ module RS
10
+ describe Image do
11
+
12
+ before :all do
13
+ code, data = Qiniu::RS::Auth.exchange_by_password!("test@qbox.net", "test")
14
+ code.should == 200
15
+ data.should be_an_instance_of(Hash)
16
+ data["access_token"].should_not be_empty
17
+ data["refresh_token"].should_not be_empty
18
+ data["refresh_token"].should_not be_empty
19
+ puts data.inspect
20
+
21
+ @bucket = "test_images"
22
+ @key = "test_image.jpg"
23
+ code2, data2 = Qiniu::RS::RS.get(@bucket, @key)
24
+ code2.should == 200
25
+ data2["url"].should_not be_empty
26
+ puts data2.inspect
27
+
28
+ @download_url = data2["url"]
29
+ end
30
+
31
+ context ".info" do
32
+ it "should works" do
33
+ code, data = Qiniu::RS::Image.info(@download_url)
34
+ code.should == 200
35
+ puts data.inspect
36
+ end
37
+ end
38
+
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,42 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require 'qiniu/rs/auth'
5
+ require 'qiniu/rs/io'
6
+ require 'digest/sha1'
7
+
8
+ module Qiniu
9
+ module RS
10
+ describe IO do
11
+
12
+ before :all do
13
+ code, data = Qiniu::RS::Auth.exchange_by_password!("test@qbox.net", "test")
14
+ code.should == 200
15
+ data.should be_an_instance_of(Hash)
16
+ data["access_token"].should_not be_empty
17
+ data["refresh_token"].should_not be_empty
18
+ data["refresh_token"].should_not be_empty
19
+ puts data.inspect
20
+
21
+ code2, data2 = Qiniu::RS::IO.put_auth()
22
+ code2.should == 200
23
+ data2["url"].should_not be_empty
24
+ data2["expiresIn"].should_not be_zero
25
+ puts data2.inspect
26
+
27
+ @put_url = data2["url"]
28
+ @bucket = "test"
29
+ @key = Digest::SHA1.hexdigest (Time.now.to_i+rand(100)).to_s
30
+ end
31
+
32
+ context ".put_file" do
33
+ it "should works" do
34
+ code, data = Qiniu::RS::IO.put_file(@put_url, __FILE__, @bucket, @key)
35
+ code.should == 200
36
+ puts data.inspect
37
+ end
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,116 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'digest/sha1'
4
+ require 'spec_helper'
5
+ require 'qiniu/rs/auth'
6
+ require 'qiniu/rs/io'
7
+ require 'qiniu/rs/rs'
8
+
9
+ module Qiniu
10
+ module RS
11
+ describe RS do
12
+
13
+ before :all do
14
+ code, data = Qiniu::RS::Auth.exchange_by_password!("test@qbox.net", "test")
15
+ code.should == 200
16
+ data.should be_an_instance_of(Hash)
17
+ data["access_token"].should_not be_empty
18
+ data["refresh_token"].should_not be_empty
19
+ data["refresh_token"].should_not be_empty
20
+ puts data.inspect
21
+
22
+ code2, data2 = Qiniu::RS::IO.put_auth()
23
+ code2.should == 200
24
+ data2["url"].should_not be_empty
25
+ data2["expiresIn"].should_not be_zero
26
+ puts data2.inspect
27
+
28
+ @put_url = data2["url"]
29
+ @bucket = "test"
30
+ @key = Digest::SHA1.hexdigest (Time.now.to_i+rand(100)).to_s
31
+ @domain = 'cdn.example.com'
32
+ end
33
+
34
+ context "IO.put_file" do
35
+ it "should works" do
36
+ code, data = Qiniu::RS::IO.put_file(@put_url, __FILE__, @bucket, @key)
37
+ code.should == 200
38
+ puts data.inspect
39
+ end
40
+ end
41
+
42
+ context ".stat" do
43
+ it "should works" do
44
+ code, data = Qiniu::RS::RS.stat(@bucket, @key)
45
+ code.should == 200
46
+ puts data.inspect
47
+ end
48
+ end
49
+
50
+ context ".get" do
51
+ it "should works" do
52
+ code, data = Qiniu::RS::RS.get(@bucket, @key, "rs_spec.rb", 1)
53
+ code.should == 200
54
+ puts data.inspect
55
+ end
56
+ end
57
+
58
+ context ".batch" do
59
+ it "should works" do
60
+ code, data = Qiniu::RS::RS.batch("stat", @bucket, [@key])
61
+ code.should == 200
62
+ puts data.inspect
63
+ end
64
+ end
65
+
66
+ context ".batch_stat" do
67
+ it "should works" do
68
+ code, data = Qiniu::RS::RS.batch_stat(@bucket, [@key])
69
+ code.should == 200
70
+ puts data.inspect
71
+ end
72
+ end
73
+
74
+ context ".batch_get" do
75
+ it "should works" do
76
+ code, data = Qiniu::RS::RS.batch_get(@bucket, [@key])
77
+ code.should == 200
78
+ puts data.inspect
79
+ end
80
+ end
81
+
82
+ context ".publish" do
83
+ it "should works" do
84
+ code, data = Qiniu::RS::RS.publish(@domain, @bucket)
85
+ code.should == 200
86
+ puts data.inspect
87
+ end
88
+ end
89
+
90
+ context ".unpublish" do
91
+ it "should works" do
92
+ code, data = Qiniu::RS::RS.unpublish(@domain)
93
+ code.should == 200
94
+ puts data.inspect
95
+ end
96
+ end
97
+
98
+ context ".delete" do
99
+ it "should works" do
100
+ code, data = Qiniu::RS::RS.delete(@bucket, @key)
101
+ code.should == 200
102
+ puts data.inspect
103
+ end
104
+ end
105
+
106
+ context ".drop" do
107
+ it "should works" do
108
+ code, data = Qiniu::RS::RS.drop(@bucket)
109
+ code.should == 200
110
+ puts data.inspect
111
+ end
112
+ end
113
+
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,49 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require 'fakeweb'
5
+ require 'qiniu/rs/utils'
6
+
7
+ module Qiniu
8
+ module RS
9
+ describe Utils do
10
+ before :all do
11
+ Struct.new("Response", :code, :body)
12
+ end
13
+
14
+ after :each do
15
+ FakeWeb.clean_registry
16
+ FakeWeb.allow_net_connect = true
17
+ end
18
+
19
+ context "safe_json_parse" do
20
+ it "should works" do
21
+ Utils.safe_json_parse('{"foo": "bar"}').should == {"foo" => "bar"}
22
+ Utils.safe_json_parse('{}').should == {}
23
+ end
24
+ end
25
+
26
+ context ".send_request_with" do
27
+ it "should works" do
28
+ FakeWeb.allow_net_connect = false
29
+ FakeWeb.register_uri(:get, "http://docs.qiniutek.com/", :body => {:abc => 123}.to_json)
30
+ res = Utils.send_request_with 'http://docs.qiniutek.com/', nil, :method => :get
31
+ res.should == [200, {"abc" => 123}]
32
+ end
33
+
34
+ [400, 500].each do |code|
35
+ context "upstream return http #{code}" do
36
+ it "should raise RestClient::RequestFailed" do
37
+ FakeWeb.allow_net_connect = false
38
+ FakeWeb.register_uri(:get, "http://docs.qiniutek.com/", :status => code)
39
+ lambda {
40
+ res = Utils.send_request_with 'http://docs.qiniutek.com/', nil, :method => :get
41
+ }.should raise_error RestClient::RequestFailed
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,10 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require 'qiniu/rs/version'
5
+
6
+ describe Qiniu::RS do
7
+ it "should has a VERSION" do
8
+ Qiniu::RS::VERSION.should =~ /^\d+\.\d+\.\d+?$/
9
+ end
10
+ end
@@ -0,0 +1,147 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require 'qiniu/rs'
5
+
6
+ module Qiniu
7
+ describe RS do
8
+
9
+ before :all do
10
+ @bucket = 'qiniu_rs_test'
11
+ @key = Digest::SHA1.hexdigest Time.now.to_s
12
+ @domain = 'cdn.example.com'
13
+ end
14
+
15
+ context ".login!" do
16
+ it "should works" do
17
+ result = Qiniu::RS.login!("test@qbox.net", "test")
18
+ result.should be_true
19
+ end
20
+ end
21
+
22
+ context ".put_auth" do
23
+ it "should works" do
24
+ result = Qiniu::RS.put_auth(10)
25
+ result.should_not be_false
26
+ result.should_not be_empty
27
+ puts result.inspect
28
+ end
29
+ end
30
+
31
+ context ".upload" do
32
+ it "should works" do
33
+ put_url = Qiniu::RS.put_auth(10)
34
+ put_url.should_not be_false
35
+ put_url.should_not be_empty
36
+ puts put_url.inspect
37
+ result = Qiniu::RS.upload(put_url, __FILE__, @bucket, @key)
38
+ result.should be_true
39
+ end
40
+ end
41
+
42
+ context ".stat" do
43
+ it "should works" do
44
+ result = Qiniu::RS.stat(@bucket, @key)
45
+ result.should_not be_false
46
+ result.should_not be_empty
47
+ puts result.inspect
48
+ end
49
+ end
50
+
51
+ context ".get" do
52
+ it "should works" do
53
+ result = Qiniu::RS.get(@bucket, @key, "rs_spec.rb", 10)
54
+ result.should_not be_false
55
+ result.should_not be_empty
56
+ puts result.inspect
57
+ end
58
+ end
59
+
60
+ context ".download" do
61
+ it "should works" do
62
+ result = Qiniu::RS.download(@bucket, @key, "rs_spec.rb", 10)
63
+ result.should_not be_false
64
+ result.should_not be_empty
65
+ puts result.inspect
66
+ end
67
+ end
68
+
69
+ context ".batch" do
70
+ it "should works" do
71
+ result = Qiniu::RS.batch("stat", @bucket, [@key])
72
+ result.should_not be_false
73
+ result.should_not be_empty
74
+ puts result.inspect
75
+ end
76
+ end
77
+
78
+ context ".batch_stat" do
79
+ it "should works" do
80
+ result = Qiniu::RS.batch_stat(@bucket, [@key])
81
+ result.should_not be_false
82
+ result.should_not be_empty
83
+ puts result.inspect
84
+ end
85
+ end
86
+
87
+ context ".batch_get" do
88
+ it "should works" do
89
+ result = Qiniu::RS.batch_get(@bucket, [@key])
90
+ result.should_not be_false
91
+ result.should_not be_empty
92
+ puts result.inspect
93
+ end
94
+ end
95
+
96
+ context ".batch_download" do
97
+ it "should works" do
98
+ result = Qiniu::RS.batch_download(@bucket, [@key])
99
+ result.should_not be_false
100
+ result.should_not be_empty
101
+ puts result.inspect
102
+ end
103
+ end
104
+
105
+ context ".publish" do
106
+ it "should works" do
107
+ result = Qiniu::RS.publish(@domain, @bucket)
108
+ result.should_not be_false
109
+ end
110
+ end
111
+
112
+ context ".unpublish" do
113
+ it "should works" do
114
+ result = Qiniu::RS.unpublish(@domain)
115
+ result.should_not be_false
116
+ end
117
+ end
118
+
119
+ context ".delete" do
120
+ it "should works" do
121
+ result = Qiniu::RS.delete(@bucket, @key)
122
+ result.should_not be_false
123
+ end
124
+ end
125
+
126
+ context ".drop" do
127
+ it "should works" do
128
+ result = Qiniu::RS.drop(@bucket)
129
+ result.should_not be_false
130
+ end
131
+ end
132
+
133
+ context ".image_info" do
134
+ it "should works" do
135
+ data = Qiniu::RS.get("test_images", "test_image.jpg")
136
+ data.should_not be_false
137
+ data.should_not be_empty
138
+ puts data.inspect
139
+ result = Qiniu::RS.image_info(data["url"])
140
+ result.should_not be_false
141
+ result.should_not be_empty
142
+ puts result.inspect
143
+ end
144
+ end
145
+
146
+ end
147
+ end
@@ -0,0 +1,12 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'bundler/setup'
4
+ require 'qiniu/rs'
5
+ require 'rspec'
6
+
7
+ RSpec.configure do |config|
8
+ config.before :all do
9
+ Qiniu::RS.establish_connection! :client_id => "abcd0c7edcdf914228ed8aa7c6cee2f2bc6155e2",
10
+ :client_secret => "fc9ef8b171a74e197b17f85ba23799860ddf3b9c"
11
+ end
12
+ end