upyun-sdk 0.1.2 → 0.1.3

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: ef8e3fa6232933b7512f4052c5ac549e1cacf790
4
- data.tar.gz: 072ac5d0a2a6b80cfe78f003df2a52ae1fd6aa0d
3
+ metadata.gz: 4dbfcc73c74693148e1bc9c89ba60cecd5e36929
4
+ data.tar.gz: 6d242487be12d316c812dafa5930f94685f1ebdc
5
5
  SHA512:
6
- metadata.gz: 76b8ec04405fa90fd07e4b8f7dc8fb436d8bcb85e6c698b9e9e55c4bb62099ec32eb7583cf7ef8b4bb57e8e1e74d71f44cdbc95376c946b3b10024345e362bf6
7
- data.tar.gz: eb7559b2e6ae6f61928cb1a09040d2f10e4ff33558a64f705bfe283ca8aeff5262a60412d086386155651efcba581a261fa8f98a99d1e2367a6cb2462e3d6e7b
6
+ metadata.gz: 143e2bbb87aff0d49cf64021ce7974739d25d3dd1aa5ca70517af731fb758b7d34fbb9fcc84d71d83a8f890a218a23434aa4b31a4ae6eaea53ddf2aa8241c11d
7
+ data.tar.gz: b157b659b247e34f0d6daf7040321f907d4424791c87379c888e068d94520dbc3ce8631161ff2e14bc25f036a9765ed0eca6d0060b34dcb1c99e294db43fdbb8
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ upyun-sdk (0.1.3)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.5)
10
+ rake (10.1.0)
11
+ rspec (2.14.1)
12
+ rspec-core (~> 2.14.0)
13
+ rspec-expectations (~> 2.14.0)
14
+ rspec-mocks (~> 2.14.0)
15
+ rspec-core (2.14.7)
16
+ rspec-expectations (2.14.4)
17
+ diff-lcs (>= 1.1.3, < 2.0)
18
+ rspec-mocks (2.14.4)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ rake
25
+ rspec
26
+ upyun-sdk!
data/LICENSE CHANGED
@@ -1,22 +1,13 @@
1
- Copyright (c) 2013 calelelac
1
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2
+ Version 2, December 2004
2
3
 
3
- MIT License
4
+ Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
4
5
 
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
6
+ Everyone is permitted to copy and distribute verbatim or modified
7
+ copies of this license document, and changing it is allowed as long
8
+ as the name is changed.
12
9
 
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
10
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
15
12
 
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
data/README.md CHANGED
@@ -46,7 +46,7 @@ up.endpoint = 1
46
46
  up.put('/upyun-sdk/ascii.txt', 'abcdefghijklmnopqrstuvwxyz\n')
47
47
  ````
48
48
 
49
- 其中,参数 `auto_mkdir` 默认为 true,表示自动创建目录;`checksum`,默认 False,表示不进行 MD5 校验; `headers` ,可根据需求设置自定义 HTTP Header,例如作图参数 x-gmkerl-*, 具体请参考 [标准 API 上传文件](http://wiki.upyun.com/index.php?title=%E6%A0%87%E5%87%86API%E4%B8%8A%E4%BC%A0%E6%96%87%E4%BB%B6) 。
49
+ 其中,参数 `auto_mkdir` 默认为 true,表示自动创建目录; `checksum`,默认 False,表示不进行 MD5 校验; `headers` ,可根据需求设置自定义 HTTP Header,例如作图参数 x-gmkerl-*, 具体请参考 [标准 API 上传文件](http://wiki.upyun.com/index.php?title=%E6%A0%87%E5%87%86API%E4%B8%8A%E4%BC%A0%E6%96%87%E4%BB%B6) 。
50
50
 
51
51
  上传成功,返回 true; 失败返回错误信息。
52
52
 
@@ -85,7 +85,7 @@ up.delete('/upyun-sdk/temp/')
85
85
  ````
86
86
 
87
87
  注意删除目录时,必须保证目录为空。
88
- 删除成功,返回 true; 失败则返回 false。
88
+ 删除成功,返回 true; 失败则返回错误信息。
89
89
 
90
90
  ### 获取目录文件列表
91
91
 
@@ -94,7 +94,7 @@ res = up.getlist('/upyun-sdk/')
94
94
  ````
95
95
 
96
96
  该方法默认获取根目录列表信息。
97
- 获取成功,返回一个包含该目录下所有目录或文件条目信息的 Hash 对象数组; 失败则返回错误信息。
97
+ 获取成功,返回一个包含该目录下所有目录或文件条目信息的数组; 失败则返回错误信息。
98
98
 
99
99
 
100
100
  ### 获取空间使用情况
@@ -112,4 +112,5 @@ res = up.usage()
112
112
 
113
113
  ## Thanks
114
114
 
115
- [gccyugi](https://github.com/gccyugi)
115
+ [gccyugi](https://github.com/gccyugi)
116
+ [megayu](https://github.com/megayu)
data/lib/upyun/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Upyun
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/upyun-sdk.rb CHANGED
@@ -11,11 +11,11 @@ class UpYun
11
11
  attr_accessor :bucket, :username, :password
12
12
  attr_reader :endpoint
13
13
 
14
- def initialize(bucket, username, password, endpoint=nil)
14
+ def initialize(bucket, username, password, endpoint=0)
15
15
  @bucket = bucket
16
16
  @username = username
17
17
  @password = password
18
- self.endpoint = endpoint || 0
18
+ self.endpoint = endpoint
19
19
  end
20
20
 
21
21
  # public API
@@ -25,32 +25,34 @@ class UpYun
25
25
  end
26
26
 
27
27
  # 上传文件
28
- def put(path, data, auto_mkdir=true, checksum=nil, headers=nil)
29
- headers = headers || {}
28
+ def put(path, data, auto_mkdir=true, checksum=nil, options={})
29
+ headers = {}
30
30
  headers['mkdir'] = auto_mkdir.to_s
31
31
  headers['Content-MD5'] = checksum if checksum
32
32
  headers['Content-Length'] = data.size.to_s
33
+ headers.merge(options)
33
34
  res = http_request('PUT', path, data, headers)
34
- res_ok?(res) ? true : res_errmsg(res)
35
+ res_msg(res)
35
36
  end
36
37
 
37
38
  # 下载文件
38
39
  def get(path)
39
40
  res = http_request('GET', path)
40
- res_ok?(res) ? res.body : res_errmsg(res)
41
+ res_msg(res, res.body)
41
42
  end
42
43
 
43
44
  # 获取文件信息
44
45
  def getinfo(path)
45
46
  res = http_request('HEAD', path)
46
- if res_ok?(res)
47
- return {
47
+ if res.code.eql? '200'
48
+ {
48
49
  :file_type => res['x-upyun-file-type'],
49
50
  :file_size => res['x-upyun-file-size'],
50
51
  :file_date => res['x-upyun-file-date']
51
52
  }
53
+ else
54
+ errmsg(res)
52
55
  end
53
- res_errmsg(res)
54
56
  end
55
57
 
56
58
  # 创建目录
@@ -58,55 +60,47 @@ class UpYun
58
60
  headers = {'folder' => 'true'}
59
61
  headers['mkdir'] = auto_mkdir.to_s
60
62
  res = http_request('POST', path, nil, headers)
61
- res_ok?(res) ? true : res_errmsg(res)
63
+ res_msg(res)
62
64
  end
63
65
 
64
66
  # 删除目录或文件
65
67
  def delete(path)
66
68
  res = http_request('DELETE', path)
67
- res_ok?(res) ? true : res_errmsg(res)
69
+ res_msg(res)
68
70
  end
69
71
 
70
72
  # 获取目录文件列表
71
73
  def getlist(path='/')
72
74
  res = http_request('GET', path)
73
- if res_ok?(res)
74
- list = res.body.split("\n").map do |file|
75
- name, type, size, time = file.split("\t")
76
- {
77
- :name => name,
78
- :type => type,
79
- :size => size,
80
- :time => time
81
- }
75
+ if res.code.eql? '200'
76
+ res.body.split("\n").map do |file|
77
+ Hash[[:name, :type, :size, :time].zip(file.split("\t"))]
82
78
  end
83
- return list
79
+ else
80
+ errmsg(res)
84
81
  end
85
- res_errmsg(res)
86
82
  end
87
83
 
88
84
  # 获取空间使用情况
89
85
  def usage(path='/')
90
86
  res = http_request('GET', "#{path}?usage")
91
- res_ok?(res) ? res.body : res_errmsg(res)
87
+ res_msg(res, res.body)
92
88
  end
93
89
 
94
90
  private
95
91
 
96
- def res_ok?(res)
97
- res.code.eql? '200'
92
+ def res_msg(res, msg=true)
93
+ (res.code.eql? '200') ? msg : errmsg(res)
98
94
  end
99
95
 
100
- def res_errmsg(res)
101
- "#{res.code} #{res.message}"
96
+ def errmsg(res)
97
+ { :code => res.code, :message => res.message }
102
98
  end
103
99
 
104
- def http_request(method, path, data=nil, headers=nil)
105
- base = "http://#{@endpoint}/#{@bucket}"
106
- url = File.join(base, path)
100
+ def http_request(method, path, data=nil, headers={})
101
+ url = "http://#{@endpoint}/#{@bucket}#{path}"
107
102
  uri = URI.parse(URI.encode(url))
108
103
 
109
- headers = headers || {}
110
104
  date = Time.now.utc.strftime('%a, %d %b %Y %H:%M:%S GMT')
111
105
  length = headers['Content-Length'] || 0
112
106
  headers['Date'] = date
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ require 'bundler/setup'
4
+
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+
8
+ require 'upyun-sdk'
data/spec/up_spec.rb ADDED
@@ -0,0 +1,60 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe 'Up' do
4
+
5
+ before :all do
6
+ # @up = UpYun.new('bucket', 'username', 'password', 0)
7
+ @up = UpYun.new('redmine', 'woohoo', 'MH2RqEFgfoEi', 0)
8
+ end
9
+
10
+ f = 'abcdefghijklmnopqrstuvwxyz\n'
11
+ dir = '/upyun-sdk'
12
+
13
+ context 'put file' do
14
+ it 'does put file' do
15
+ @up.put("#{dir}/ascii.txt", f).should be_true
16
+ end
17
+ end
18
+
19
+ context 'get file' do
20
+ it 'does get file' do
21
+ @up.get("#{dir}/ascii.txt").size.should == f.size
22
+ end
23
+ end
24
+
25
+ context 'getinfo' do
26
+ it 'do get file info' do
27
+ @up.getinfo("#{dir}/ascii.txt")[:file_size].to_i.should == f.size
28
+ end
29
+ end
30
+
31
+ context 'getlist' do
32
+ it 'do get directory list' do
33
+ @up.getlist("#{dir}")[0][:size].to_i.should == f.size
34
+ end
35
+ end
36
+
37
+ context 'usage' do
38
+ it 'do get directory usage' do
39
+ @up.usage("#{dir}").to_i.should == f.size
40
+ end
41
+ end
42
+
43
+ context 'mkdir' do
44
+ it 'do create directory' do
45
+ @up.mkdir("#{dir}/temp/").should be_true
46
+ end
47
+ end
48
+
49
+ context 'delete dir' do
50
+ it 'do delete directory' do
51
+ @up.delete("#{dir}/temp/").should be_true
52
+ end
53
+ end
54
+
55
+ context 'delete file' do
56
+ it 'do delete file' do
57
+ @up.delete("#{dir}/ascii.txt").should be_true
58
+ end
59
+ end
60
+ end
data/upyun-sdk.gemspec CHANGED
@@ -18,6 +18,6 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
21
  spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec"
23
23
  end
metadata CHANGED
@@ -1,31 +1,31 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: upyun-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - woo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-01 00:00:00.000000000 Z
11
+ date: 2013-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
- version: '1.3'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - '>='
25
25
  - !ruby/object:Gem::Version
26
- version: '1.3'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - '>='
@@ -46,11 +46,16 @@ extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
48
  - .gitignore
49
+ - .rspec
50
+ - Gemfile
51
+ - Gemfile.lock
49
52
  - LICENSE
50
53
  - README.md
51
54
  - Rakefile
52
55
  - lib/upyun-sdk.rb
53
56
  - lib/upyun/version.rb
57
+ - spec/spec_helper.rb
58
+ - spec/up_spec.rb
54
59
  - upyun-sdk.gemspec
55
60
  homepage: https://github.com/klwoohoo/upyun-sdk
56
61
  licenses:
@@ -76,4 +81,6 @@ rubygems_version: 2.0.6
76
81
  signing_key:
77
82
  specification_version: 4
78
83
  summary: Upyun SDK for Ruby.
79
- test_files: []
84
+ test_files:
85
+ - spec/spec_helper.rb
86
+ - spec/up_spec.rb