yandex_disk 1.0.0 → 1.1.1

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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZDJiNmU3ZWM2ZWUwMzVmMzU0NmJmMTBkMzczZjU4NmY1ODk1OGNiYg==
4
+ YzJiNjlmZTg4ODAxNDRhM2RkNzE0NDMwNDgxNDgwMzRjZGU3N2EwOQ==
5
5
  data.tar.gz: !binary |-
6
- NjE3MjRiM2Y0YzAxNmE2ZGFmNmM0ZGQwZGNhZWRlMTFlMTdhNGJkZA==
6
+ NDM5NjVmNmY3N2Q2NjhiMTllNTE4NDFjZjQxNjM2ZWJjMzk5YTc4Yg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OTgyMzRkZDUwYmFlNjMyYzU3ZDIwZmViNWVhZGQ4MzkwNGQ5NDhlNmI5OGY3
10
- ZTM1MzRhMzA0YjMxMDJiMjU3MjA1YmJkYTdhMDBmYWQ5NWI2YzBjNGVhMTc2
11
- ZDA5NjcyZWRhYjdlODUxMzliYzZjNzdiZDE3YjczYTVjZmNmNWE=
9
+ YWVhZGNkZTg5MmUyMmI0MWNlZmJhZmFlMDY5NjdlYWY3MzYyNDdlMmY5ZDUy
10
+ ZWEyOWY0ZWQyY2UzZTRmOGI2MTA5OWFiMzA5YzYxMzJjNTdkOTk5M2M5ZTRj
11
+ Yjc5NDliYTVmMWNiYTlkMDJjZDU2YjU0ODQ0OGEwZDcwOTY4MWU=
12
12
  data.tar.gz: !binary |-
13
- YTg0OTZjMTU1OTBjMDJhYTcwOTNhYWYyMjI0ZWY1ZDliZTBlYjVkMmZkNjFk
14
- YjA3Mzk2NWMyZDM2NTM4ZTc3YTNlOWUxNmU3YjRiZTMxOGI2ODkyMzEyOWVl
15
- MzkyMjA4NjQxMjJmMTk5M2E0M2Y1Yzk5N2ZjYTU2NjhhOGM4NDU=
13
+ OGU1YTAyZGRkM2MwYWQyY2Q5NzBiYjkxZjE1NWY2YTYwMjU5ZTdjMDA2ODgx
14
+ OGNiMWQ4MGI1YTVkMDJlYjA0ZTZhOTQ4YjJlODA5ZjY2MGJjN2FjOTU5MmFj
15
+ YzQyZGEwMWJjNDNiMzYwMDkwYzY4NTgzM2VkODZkYWUyNDc1ZWU=
@@ -17,7 +17,6 @@ module YandexDisk
17
17
  @token = 'Basic ' + Base64.encode64("#{login}:#{pwd}")
18
18
  end
19
19
 
20
- # TODO gzip file when sending
21
20
  # Example:
22
21
  # yd.upload('/home/graph.pdf', 'my/work')
23
22
  # => true
@@ -33,10 +32,9 @@ module YandexDisk
33
32
  raise RequestError, "File not found." if file.nil? || !File.file?(file)
34
33
  # create path
35
34
  create_path(path) if options[:force]
36
- options[:chunk_size] ||= 100
35
+ options[:chunk_size] ||= 1024
37
36
  @file = File.open(file)
38
37
  options[:headers] = {'Expect' => '100-continue',
39
- #'Content-Encoding' => 'gzip',
40
38
  'Transfer-Encoding' => 'chunked',
41
39
  'content-type' => 'application/binary'}
42
40
 
@@ -62,8 +60,8 @@ module YandexDisk
62
60
  data = nil
63
61
  # unzip if zipped
64
62
  if @response.header['Content-Encoding'] == 'gzip'
65
- sio = StringIO.new( @response.body )
66
- gz = Zlib::GzipReader.new( sio )
63
+ s_io = StringIO.new(@response.body)
64
+ gz = Zlib::GzipReader.new(s_io)
67
65
  data = gz.read
68
66
  else
69
67
  data = @response.body
@@ -92,14 +90,19 @@ module YandexDisk
92
90
  # Example:
93
91
  # yd.size
94
92
  # => {:available => 312312, :used => 3123}
95
- def size
93
+ # Arguments:
94
+ # options:
95
+ # readable: return size in human readable format e.g, 100K 128M 1G (false for default)
96
+ def size(options = {})
96
97
  body = '<?xml version="1.0" encoding="utf-8"?><D:propfind xmlns:D="DAV:"><D:prop><D:quota-available-bytes/><D:quota-used-bytes/></D:prop></D:propfind>'
97
98
  send_propfind(0, {:body => body})
98
99
  xml = REXML::Document.new(@response.body)
99
100
  prop = 'd:multistatus/d:response/d:propstat/d:prop/'
101
+ available_b = xml.elements[prop + 'd:quota-available-bytes'].text.to_i
102
+ used_b = xml.elements[prop + 'd:quota-used-bytes'].text.to_i
100
103
 
101
- return {:available => xml.elements[prop + 'd:quota-available-bytes'].text.to_i,
102
- :used => xml.elements[prop + 'd:quota-used-bytes'].text.to_i}
104
+ return {:available => available_b.to_readable(options[:h_size]),
105
+ :used => used_b.to_readable(options[:h_size])}
103
106
  end
104
107
 
105
108
  # Example:
@@ -129,7 +132,8 @@ module YandexDisk
129
132
  #
130
133
  # Arguments:
131
134
  # path: path to yandex disk directory or file
132
- def properties(path)
135
+ # h_size: return size in human readable format e.g, 100K 128M 1G (false for default)
136
+ def properties(path, options = {})
133
137
  body = '<?xml version="1.0" encoding="utf-8"?><propfind xmlns="DAV:"><prop><displayname/><creationdate/><getlastmodified/><getcontenttype/><getcontentlength/><public_url xmlns="urn:yandex:disk:meta"/></prop></propfind>'
134
138
  send_propfind(0, {:path => path, :body => body})
135
139
  prop = 'd:multistatus/d:response/d:propstat/d:prop/'
@@ -141,7 +145,7 @@ module YandexDisk
141
145
  :created => xml.elements[prop + 'd:getlastmodified'].text,
142
146
  :updated => xml.elements[prop + 'd:getlastmodified'].text,
143
147
  :type => type ? type : 'dir',
144
- :size => size,
148
+ :size => size.to_readable(options[:h_size]),
145
149
  :is_file => size > 0,
146
150
  :public_url => xml.elements[prop + 'public_url'].text}
147
151
  end
@@ -158,26 +162,28 @@ module YandexDisk
158
162
  #
159
163
  # Arguments:
160
164
  # path: path to yandex disk directory (default is <b>root</b>)
161
- # with_root: include information of root directory or not (<b>false</b> for default)
162
- def files(path = '', with_root = true)
165
+ # root: include information of root directory or not (<b>false</b> for default)
166
+ # h_size: return size in human readable format e.g, 100K 128M 1G (false for default)
167
+ def files(path = '', options = {})
163
168
  send_propfind(1, {:path => path})
164
169
  xml = REXML::Document.new(@response.body)
165
170
  prop = 'd:propstat/d:prop/'
166
171
  files = []
167
172
  xml.elements.each('d:multistatus/d:response') do |res|
168
173
  name = URI.decode(res.elements[prop + 'd:displayname'].text)
169
- next if !with_root && path.split('/').last == name
174
+ next if !options[:root] && path.split('/').last == name
170
175
  size = res.elements[prop + 'd:getcontentlength'].text.to_i
171
176
 
172
177
  files << {:name => name,
173
178
  :path => URI.decode(res.elements['d:href'].text),
174
179
  :created => res.elements[prop + 'd:creationdate'].text,
175
180
  :updated => res.elements[prop + 'd:getlastmodified'].text,
176
- :size => size,
181
+ :size => size.to_readable(options[:h_size]),
177
182
  :is_file => size > 0}
178
183
  end
179
184
  return files
180
185
  end
186
+ alias_method :ls, :files
181
187
 
182
188
  # Example:
183
189
  # yd.copy('/home/graph.pdf', 'my/work')
@@ -273,6 +279,7 @@ module YandexDisk
273
279
  send_request(:propfind, options.merge!(headers))
274
280
  end
275
281
 
282
+ # return true if successful
276
283
  def send_request(method, args = {})
277
284
  # headers
278
285
  headers = {'Authorization' => @token}
@@ -1,5 +1,5 @@
1
1
  module YandexDisk
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.1'
3
3
  HOME_PAGE = 'https://github.com/denwwer/yandex_disk'
4
4
  API_URL = 'https://webdav.yandex.ru'
5
5
  # DON'T TURN ON DEBUG FOR PRODUCTION
@@ -13,9 +13,11 @@ module YandexDisk
13
13
  @file.read(@size)
14
14
  end
15
15
  end
16
+
16
17
  def eof!
17
18
  @file.eof!
18
19
  end
20
+
19
21
  def eof?
20
22
  @file.eof?
21
23
  end
@@ -10,4 +10,21 @@ module YandexDisk
10
10
  self.nil? || !self || self.empty?
11
11
  end
12
12
  end
13
+
14
+ class ::Integer
15
+ def to_readable(convert)
16
+ return self unless convert
17
+
18
+ conv = {'Byte' => 1024,
19
+ 'KB' => 1024**2,
20
+ 'MB' => 1024**3,
21
+ 'GB' => 1024**4}
22
+
23
+ conv.each do |suf, size|
24
+ next if self >= size
25
+ return "%.2f %s" % [ self / (size / 1024).to_f, suf ]
26
+ end
27
+ end
28
+ end
29
+
13
30
  end
data/spec/spec_helper.rb CHANGED
@@ -2,7 +2,6 @@
2
2
  require 'simplecov'
3
3
  SimpleCov.start
4
4
  require 'rubygems'
5
- require 'bundler/setup'
6
5
  require 'yandex_disk'
7
6
  require 'fileutils'
8
7
 
@@ -1,7 +1,5 @@
1
1
  #encoding: UTF-8
2
2
  require 'spec_helper'
3
- require 'net/http'
4
- require 'uri'
5
3
  require 'fastimage'
6
4
 
7
5
  describe YandexDisk do
@@ -27,9 +25,18 @@ describe YandexDisk do
27
25
  @yd = YandexDisk.login(LOGIN, PWD)
28
26
  end
29
27
 
30
- it 'should return available and used space' do
31
- size = @yd.size
32
- expect(size[:used] > 0 && size[:available] > 0).to be_true
28
+ describe 'size' do
29
+ it 'should be in bytes' do
30
+ size = @yd.size
31
+ expect(size[:used] > 0 && size[:available] > 0).to be_true
32
+ end
33
+
34
+ it 'should be in readable format' do
35
+ size = @yd.size(:h_size => true)
36
+ mask = /Byte|KB|MB|GB/
37
+ expect(size[:used].match(mask)[0].empty? &&
38
+ size[:available].match(mask)[0].empty?).to be_false
39
+ end
33
40
  end
34
41
 
35
42
  it 'should return list of files' do
@@ -76,7 +83,7 @@ describe YandexDisk do
76
83
  # copy directory
77
84
  @yd.copy(src, des)
78
85
  # check files
79
- @yd.files(src, false).size.should eq 2
86
+ @yd.files(src).size.should eq 2
80
87
  files.each do |file, text|
81
88
  download_and_validate(File.join(des, file), text)
82
89
  end
@@ -94,31 +101,31 @@ describe YandexDisk do
94
101
  @yd.upload(@text_file, 'my', {:force => true}).should be_true
95
102
  end
96
103
 
97
- it 'should be downloaded and valid' do
104
+ it 'should be downloaded and validate' do
98
105
  @yd.upload(@text_file, 'my', {:force => true}).should be_true
99
106
  path = 'my/' + File.basename(@text_file)
100
107
  download_and_validate(path)
101
108
  end
102
109
  # its related to copy dir
103
- it 'should by copied to new directory' do
110
+ it 'should by copied' do
104
111
  @yd.upload(@text_file, 'my', {:force => true})
105
112
  f_name = File.basename(@text_file)
106
113
  file = 'my/' + f_name
107
114
  new_path = 'my/text'
108
- @yd.create_path(new_path)
109
- @yd.copy(file, new_path)
115
+ @yd.create_path(new_path).should be_true
116
+ @yd.copy(file, new_path).should be_true
110
117
  # file still exist in src path
111
118
  @yd.exist?(file).should be_true
112
119
  download_and_validate(File.join(new_path, f_name))
113
120
  end
114
121
 
115
- it 'should by moved to new directory' do
122
+ it 'should by moved' do
116
123
  @yd.upload(@text_file, 'my', {:force => true})
117
124
  f_name = File.basename(@text_file)
118
125
  file = 'my/' + f_name
119
126
  new_path = 'my/text'
120
127
  @yd.create_path(new_path)
121
- @yd.move(file, new_path)
128
+ @yd.move(file, new_path).should be_true
122
129
  # file not exist in src path
123
130
  @yd.exist?(file).should be_false
124
131
  download_and_validate(File.join(new_path, f_name))
@@ -129,7 +136,7 @@ describe YandexDisk do
129
136
  @yd.upload(@text_file)
130
137
  f_name = File.basename(@text_file)
131
138
  download_and_validate(f_name)
132
- @yd.delete(f_name)
139
+ @yd.delete(f_name).should be_true
133
140
  @yd.exist?(f_name).should be_false
134
141
  end
135
142
 
@@ -154,14 +161,19 @@ describe YandexDisk do
154
161
  end
155
162
 
156
163
  it 'should return image in M size'do
157
- @yd.preview('my/' + @f_name, 'm', DOWNLOAD_PATH)
164
+ @yd.preview('my/' + @f_name, 'm', DOWNLOAD_PATH).should be_true
158
165
  File.file?( File.join(DOWNLOAD_PATH, @f_name) ).should be_true
159
166
  end
160
167
 
161
168
  it 'should return image in 300x250 size'do
162
- @yd.preview('my/' + @f_name, '300x250', DOWNLOAD_PATH)
169
+ @yd.preview('my/' + @f_name, '300x250', DOWNLOAD_PATH).should be_true
163
170
  FastImage.size( File.join(DOWNLOAD_PATH, @f_name) ).should eq [300, 250]
164
171
  end
172
+
173
+ it 'should return image in 128 pixels wide'do
174
+ @yd.preview('my/' + @f_name, 128, DOWNLOAD_PATH).should be_true
175
+ FastImage.size( File.join(DOWNLOAD_PATH, @f_name) )[0].should eq 128
176
+ end
165
177
  end
166
178
 
167
179
  after(:each) do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yandex_disk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Boris Murga
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-28 00:00:00.000000000 Z
11
+ date: 2013-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -65,8 +65,6 @@ files:
65
65
  - lib/yandex_disk/cfg.rb
66
66
  - spec/spec_helper.rb
67
67
  - spec/files/README
68
- - spec/files/sample.jpg
69
- - spec/files/sample.txt
70
68
  - spec/yandex_disk_spec.rb
71
69
  homepage: https://github.com/denwwer/yandex_disk
72
70
  licenses:
@@ -96,6 +94,4 @@ summary: API for Yandex Disk
96
94
  test_files:
97
95
  - spec/spec_helper.rb
98
96
  - spec/files/README
99
- - spec/files/sample.jpg
100
- - spec/files/sample.txt
101
97
  - spec/yandex_disk_spec.rb
Binary file
@@ -1 +0,0 @@
1
- Hi developer.