upyun 1.0.3 → 1.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8c53c29e162493c788f931b0eab39850a404fe70
4
- data.tar.gz: 04a51ddba3f0eff2d3862ee4d3ede5d15f948a96
3
+ metadata.gz: 1e8945188015a56c10d3c46361797fd39ae8679a
4
+ data.tar.gz: c694593f3027180c55e9bfb5915f8fbe4ff94fa5
5
5
  SHA512:
6
- metadata.gz: d26b50b05d5ed484d3b308676772fc84a9f6b2885baef919b421dd4d57dc8632c680510827aae5ef92f7cbabb221f45d328d02c5bd1cb7f91bf3129f83c848f9
7
- data.tar.gz: 5f01e1b9c185217cca77b94ac433f2bd379632ee169caf17b74cf0f199044dcbc466fd1f272258b7481816d7c545a3619de251d0852723ff0085149aab5da069
6
+ metadata.gz: 108b4b9642f54e17b928f7482fa73e9d875d1bc3db952908dc41dabda035d8158dc98eec202afdfb9a41f97ab8452acec0111bca0ee150fb363ac52e4b1e0119
7
+ data.tar.gz: 1412e721624c65c989ab5e909994663e97ffa613815bdebe4b969e79d56e9bb60f48b7e0453f9d833fdc5727cdbc1dd22eacd08ac6a357dab08fbe21592820ac
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## CHANGE LOG
2
2
 
3
+ ### v1.0.4
4
+ - 修正上传的参数为文件内容或者是文件描述符
5
+ - ActiveSupport 的依赖放宽到 3.2.8 或以上
6
+
3
7
  ### v1.0.3
4
8
  - 请求失败时返回的 Hash 中增加 `:request_id` 字段;
5
9
  - 对于图片空间,上传时返回的 Hash 中包含图片详情信息;
data/README.md CHANGED
@@ -13,7 +13,7 @@
13
13
  在 `Gemfile` 中加入以下代码
14
14
 
15
15
  ```ruby
16
- gem 'upyun', '~> 1.0.3'
16
+ gem 'upyun', '~> 1.0.4'
17
17
  ```
18
18
 
19
19
  然后执行如下命令安装:
@@ -74,12 +74,13 @@ upyun.endpoint = Upyun::ED_CMCC
74
74
  > 这种方式只指定了又拍云必选的 `Date`, `Content-Length` 两个 Header,其它 Header 信息均未指定
75
75
 
76
76
  ```ruby
77
- upyun.put('/save/to/path', 'file or binary')
77
+ upyun.put('/save/to/path', File.new('file.txt', 'rb')) # 上传一个文件
78
+ upyun.put('/save/to/path', 'binary') # 直接上传内容
78
79
  ```
79
80
  **参数**
80
81
 
81
82
  * `/save/to/path`: 文件在 UPYUN 空间的保存路径
82
- * `file or binary`:本地文件路径或文件内容
83
+ * `file or binary`:已打开的文件描述符或文件内容,如果为文件描述符,在上传结束后该描述符会自动关闭
83
84
 
84
85
  ##### 自定义方式
85
86
  您也可以选择使用 API 允许的额外可选 HTTP Header 参数,以使用 API 提供的预处理等功能:
@@ -260,8 +261,10 @@ upyun.endpoint = Upyun::ED_CMCC
260
261
  > 使用简化版本,将不使用额外的策略参数:
261
262
 
262
263
  ```ruby
263
- upyun.upload('file')
264
+ upyun.upload('filepath.png')
265
+ upyun.upload(File.new('filepath.png'))
264
266
  ```
267
+ 参数可以是文件路径或者已经打开的文件文件描述符
265
268
 
266
269
  **返回**
267
270
  上传结果返回一个 `Hash` 结构:
data/lib/upyun/form.rb CHANGED
@@ -52,7 +52,7 @@ module Upyun
52
52
  payload = {
53
53
  policy: policy(base_opts.merge(opts)),
54
54
  signature: signature,
55
- file: File.new(file, 'rb')
55
+ file: file.is_a?(File) ? file : File.new(file, 'rb')
56
56
  }
57
57
 
58
58
  rest_client.post(payload, {'User-Agent' => "Upyun-Ruby-SDK-#{VERSION}"}) do |res|
data/lib/upyun/rest.rb CHANGED
@@ -17,13 +17,9 @@ module Upyun
17
17
  end
18
18
 
19
19
  def put(path, file, headers={})
20
- raise ArgumentError, "'file' is not an instance of String" unless file.is_a?(String)
21
20
  headers = headers.merge({'mkdir' => true}) unless headers.key?('mkdir')
22
- options = if File.file?(file)
23
- {body: File.read(file), length: File.size(file), headers: headers}
24
- else
25
- {body: file, length: file.length, headers: headers}
26
- end
21
+ body = file.respond_to?(:read) ? IO.binread(file) : file
22
+ options = {body: body, length: size(file), headers: headers}
27
23
 
28
24
  # If the type of current bucket is Picture,
29
25
  # put an image maybe return a set of headers
@@ -39,6 +35,8 @@ module Upyun
39
35
  end
40
36
 
41
37
  res == {} ? true : res
38
+ ensure
39
+ file.close if file.respond_to?(:close)
42
40
  end
43
41
 
44
42
  def get(path, savepath=nil)
@@ -162,5 +160,13 @@ module Upyun
162
160
  sign = "#{method.to_s.upcase}&#{path}&#{date}&#{length}&#{@password}"
163
161
  "UpYun #{@operator}:#{md5(sign)}"
164
162
  end
163
+
164
+ def size(param)
165
+ if param.respond_to?(:size)
166
+ param.size
167
+ elsif param.is_a?(IO)
168
+ param.stat.size
169
+ end
170
+ end
165
171
  end
166
172
  end
data/lib/upyun/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Upyun
2
- VERSION = "1.0.3"
2
+ VERSION = "1.0.4"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -2,3 +2,9 @@ require 'coveralls'
2
2
  Coveralls.wear!
3
3
 
4
4
  require 'upyun'
5
+
6
+ class String
7
+ def self.random(length=5)
8
+ ('a'..'z').sort_by {rand}[0, length].join
9
+ end
10
+ end
data/spec/upyun_spec.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  # encoding: utf-8
2
-
3
2
  require File.dirname(__FILE__) + '/spec_helper'
4
3
  require 'uri'
5
4
 
@@ -25,10 +24,10 @@ describe "Upyun Restful API Basic testing" do
25
24
  end
26
25
 
27
26
  describe ".put" do
28
- before { @path = '/ruby-sdk/foo/test.jpg' }
27
+ before { @path = "/ruby-sdk/foo/#{String.random}/test.jpg" }
29
28
 
30
29
  it "PUT a file" do
31
- expect(@upyun.put(@path, @file)).to be true
30
+ expect(@upyun.put(@path, File.new(@file, 'rb'))).to be true
32
31
  end
33
32
 
34
33
  it "PUT a binary string" do
@@ -42,11 +41,11 @@ describe "Upyun Restful API Basic testing" do
42
41
  'x-gmkerl-value' => 42,
43
42
  'x-gmkerl-unsharp' => true
44
43
  }
45
- expect(@upyun.put(@path, @file, headers)).to be true
44
+ expect(@upyun.put(@path, File.new(@file, 'rb'), headers)).to be true
46
45
  end
47
46
 
48
- describe "PUT a file while the path is not encoded" do
49
- before(:all) { @path_cn = '/ruby-sdk/这是中文路径/foo.txt' }
47
+ describe "PUT while the path is not encoded" do
48
+ before(:all) { @path_cn = '/ruby-sdk/foo/这是中文路径/foo.txt' }
50
49
 
51
50
  it "should success" do
52
51
  expect(@upyun.put(@path_cn, @str)).to be true
@@ -65,7 +64,7 @@ describe "Upyun Restful API Basic testing" do
65
64
 
66
65
  it "put a file to Picture bucket should return the image's metadata" do
67
66
  upyunp = Upyun::Rest.new('sdkimg', 'tester', 'grjxv2mxELR3', {}, Upyun::ED_TELECOM)
68
- metas = upyunp.put(@path, @file, content_type: 'image/jpeg')
67
+ metas = upyunp.put(@path, File.new(@file), content_type: 'image/jpeg')
69
68
  expect(metas).to include(:width, :height, :frames, :file_type)
70
69
  expect(metas[:width].is_a?(Integer))
71
70
  expect(metas[:height].is_a?(Integer))
@@ -78,7 +77,7 @@ describe "Upyun Restful API Basic testing" do
78
77
 
79
78
  describe ".get" do
80
79
  before :all do
81
- @path = '/ruby-sdk/foo/test.jpg'
80
+ @path = "/ruby-sdk/foo/#{String.random}/test.jpg"
82
81
  @upyun.put(@path, @str, {'Content-Type' => 'text/plain'})
83
82
  end
84
83
 
@@ -87,14 +86,14 @@ describe "Upyun Restful API Basic testing" do
87
86
  end
88
87
 
89
88
  it "GET a file and save" do
90
- expect(@upyun.get(@path, './save.jpg')).not_to eq(404)
91
- expect(File.exists?('./save.jpg')).to be true
89
+ expect(@upyun.get(@path, './save.jpg')).to eq(@str.length)
90
+ expect(File.exists?('./save.jpg')).to eq(true)
92
91
  expect(File.read('./save.jpg')).to eq(@str)
93
92
  File.delete('./save.jpg')
94
93
  end
95
94
 
96
95
  it "GET a not-exist file" do
97
- res = @upyun.get('/ruby-sdk/foo/test-not-exist.jpg')
96
+ res = @upyun.get("/ruby-sdk/foo/#{String.random}/test-not-exist.jpg")
98
97
  expect(res.is_a?(Hash) && res[:error][:code] == 404)
99
98
  end
100
99
 
@@ -103,7 +102,7 @@ describe "Upyun Restful API Basic testing" do
103
102
 
104
103
  describe ".getinfo" do
105
104
  before :all do
106
- @dir = '/ruby-sdk/foo'
105
+ @dir = "/ruby-sdk/foo/#{String.random}"
107
106
  @path = "#{@dir}/test.jpg"
108
107
  @upyun.put(@path, @str, {'Content-Type' => 'text/plain'})
109
108
  end
@@ -123,8 +122,8 @@ describe "Upyun Restful API Basic testing" do
123
122
 
124
123
  describe ".delete" do
125
124
  before do
126
- @path = '/ruby-sdk/foo/test.jpg'
127
- @upyun.put(@path, @file)
125
+ @path = "/ruby-sdk/foo/#{String.random}/test.jpg"
126
+ @upyun.put(@path, File.new(@file))
128
127
  end
129
128
 
130
129
  it "DELETE a file" do
@@ -133,7 +132,7 @@ describe "Upyun Restful API Basic testing" do
133
132
  end
134
133
 
135
134
  describe ".mkdir" do
136
- before(:all) { @path = '/ruby-sdk/foo/dir' }
135
+ before(:all) { @path = "/ruby-sdk/foo/dir/#{String.random}" }
137
136
 
138
137
  it "should success" do
139
138
  expect(@upyun.mkdir(@path)).to be true
@@ -144,8 +143,8 @@ describe "Upyun Restful API Basic testing" do
144
143
 
145
144
  describe ".getlist" do
146
145
  before :all do
147
- @dir = '/ruby-sdk/foo'
148
- 10.times { |i| @upyun.put("#@dir/#{i}", @file) }
146
+ @dir = "/ruby-sdk/foo/#{String.random}"
147
+ 10.times { |i| @upyun.put("#@dir/#{i}", File.new(@file)) }
149
148
  end
150
149
 
151
150
  it "should get a list of file record" do
@@ -189,7 +188,7 @@ describe "Form Upload", current: true do
189
188
  end
190
189
 
191
190
  describe ".upload" do
192
- it "with default attributes should success" do
191
+ it "with file path should success" do
193
192
  res = @form.upload(@file)
194
193
  expect(res.keys).to include(:code, :message, :url, :time)
195
194
  expect(res[:code]).to eq(200)
@@ -198,6 +197,15 @@ describe "Form Upload", current: true do
198
197
  expect(res[:url]).to eq("/#{now.year}/#{now.mon}/#{now.day}/upyun.jpg")
199
198
  end
200
199
 
200
+ it "with file descriptor should success" do
201
+ fd = File.new(@file, 'rb')
202
+ res = @form.upload(fd)
203
+ expect(res.keys).to include(:code, :message, :url, :time)
204
+ expect(res[:code]).to eq(200)
205
+ expect(res[:message]).to match(/ok/)
206
+ expect(fd.closed?).to eq(true)
207
+ end
208
+
201
209
  it "set 'save-key' should success" do
202
210
  res = @form.upload(@file, {'save-key' => 'name-ed keypath'})
203
211
  expect(res[:code]).to eq(200)
data/upyun.gemspec CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency "rest-client", ">= 1.6.7"
22
- spec.add_dependency "activesupport"
22
+ spec.add_dependency "activesupport", ">= 3.2.8"
23
23
 
24
24
  spec.add_development_dependency "rspec", "~> 2.6"
25
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: upyun
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - jsvisa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-26 00:00:00.000000000 Z
11
+ date: 2014-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - '>='
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 3.2.8
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 3.2.8
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement