yandex_disk 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.
- checksums.yaml +15 -0
- data/lib/yandex_disk/api.rb +343 -0
- data/lib/yandex_disk/cfg.rb +7 -0
- data/lib/yandex_disk/chunked.rb +23 -0
- data/lib/yandex_disk/ext.rb +13 -0
- data/lib/yandex_disk.rb +13 -0
- data/spec/files/README +6 -0
- data/spec/files/sample.jpg +0 -0
- data/spec/files/sample.txt +1 -0
- data/spec/spec_helper.rb +39 -0
- data/spec/yandex_disk_spec.rb +180 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZDJiNmU3ZWM2ZWUwMzVmMzU0NmJmMTBkMzczZjU4NmY1ODk1OGNiYg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NjE3MjRiM2Y0YzAxNmE2ZGFmNmM0ZGQwZGNhZWRlMTFlMTdhNGJkZA==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
OTgyMzRkZDUwYmFlNjMyYzU3ZDIwZmViNWVhZGQ4MzkwNGQ5NDhlNmI5OGY3
|
10
|
+
ZTM1MzRhMzA0YjMxMDJiMjU3MjA1YmJkYTdhMDBmYWQ5NWI2YzBjNGVhMTc2
|
11
|
+
ZDA5NjcyZWRhYjdlODUxMzliYzZjNzdiZDE3YjczYTVjZmNmNWE=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YTg0OTZjMTU1OTBjMDJhYTcwOTNhYWYyMjI0ZWY1ZDliZTBlYjVkMmZkNjFk
|
14
|
+
YjA3Mzk2NWMyZDM2NTM4ZTc3YTNlOWUxNmU3YjRiZTMxOGI2ODkyMzEyOWVl
|
15
|
+
MzkyMjA4NjQxMjJmMTk5M2E0M2Y1Yzk5N2ZjYTU2NjhhOGM4NDU=
|
@@ -0,0 +1,343 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
require 'zlib'
|
4
|
+
require 'base64'
|
5
|
+
require 'rexml/document'
|
6
|
+
|
7
|
+
require 'yandex_disk/cfg'
|
8
|
+
require 'yandex_disk/ext'
|
9
|
+
require 'yandex_disk/chunked'
|
10
|
+
|
11
|
+
module YandexDisk
|
12
|
+
class RequestError < Exception; end
|
13
|
+
|
14
|
+
class Api
|
15
|
+
|
16
|
+
def initialize(login, pwd)
|
17
|
+
@token = 'Basic ' + Base64.encode64("#{login}:#{pwd}")
|
18
|
+
end
|
19
|
+
|
20
|
+
# TODO gzip file when sending
|
21
|
+
# Example:
|
22
|
+
# yd.upload('/home/graph.pdf', 'my/work')
|
23
|
+
# => true
|
24
|
+
#
|
25
|
+
# Arguments:
|
26
|
+
# file: path to file
|
27
|
+
# path: path to yandex disk directory (default is <b>root</b>)
|
28
|
+
# options:
|
29
|
+
# chunk_size: file chunk size (default is 100)
|
30
|
+
# force: create path structure if not exist (raise <b>RequestError</b> if <b>path</b> not exist for default)
|
31
|
+
def upload(file, path = '', options = {})
|
32
|
+
# valid file?
|
33
|
+
raise RequestError, "File not found." if file.nil? || !File.file?(file)
|
34
|
+
# create path
|
35
|
+
create_path(path) if options[:force]
|
36
|
+
options[:chunk_size] ||= 100
|
37
|
+
@file = File.open(file)
|
38
|
+
options[:headers] = {'Expect' => '100-continue',
|
39
|
+
#'Content-Encoding' => 'gzip',
|
40
|
+
'Transfer-Encoding' => 'chunked',
|
41
|
+
'content-type' => 'application/binary'}
|
42
|
+
|
43
|
+
send_request(:put, options.merge({:path => File.join( path, File.basename(file) )}))
|
44
|
+
@file.close
|
45
|
+
return true
|
46
|
+
end
|
47
|
+
|
48
|
+
# Example:
|
49
|
+
# yd.download('/home/graph.pdf', '/home')
|
50
|
+
# => true
|
51
|
+
#
|
52
|
+
# Arguments:
|
53
|
+
# file: path to yandex disk file
|
54
|
+
# save_path: path to save
|
55
|
+
def download(file, save_path)
|
56
|
+
option = {:path => file,
|
57
|
+
:headers => {'TE' => 'chunked',
|
58
|
+
'Accept-Encoding' => 'gzip'}}
|
59
|
+
|
60
|
+
send_request(:get, option)
|
61
|
+
|
62
|
+
data = nil
|
63
|
+
# unzip if zipped
|
64
|
+
if @response.header['Content-Encoding'] == 'gzip'
|
65
|
+
sio = StringIO.new( @response.body )
|
66
|
+
gz = Zlib::GzipReader.new( sio )
|
67
|
+
data = gz.read
|
68
|
+
else
|
69
|
+
data = @response.body
|
70
|
+
end
|
71
|
+
File.open(File.join(save_path, file.split('/').last), 'w'){|f| f.write(data)}
|
72
|
+
|
73
|
+
return true
|
74
|
+
end
|
75
|
+
|
76
|
+
# Example:
|
77
|
+
# yd.create_path('/home/my/photos')
|
78
|
+
# => true
|
79
|
+
#
|
80
|
+
# Arguments:
|
81
|
+
# path: path to yandex disk directory hierarchy
|
82
|
+
def create_path(path)
|
83
|
+
c_path = ''
|
84
|
+
path.split('/').each do |p|
|
85
|
+
next if p.empty?
|
86
|
+
c_path << p + '/'
|
87
|
+
send_request(:mkcol, {:path => c_path})
|
88
|
+
end
|
89
|
+
end
|
90
|
+
alias_method :mkdir, :create_path
|
91
|
+
|
92
|
+
# Example:
|
93
|
+
# yd.size
|
94
|
+
# => {:available => 312312, :used => 3123}
|
95
|
+
def size
|
96
|
+
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
|
+
send_propfind(0, {:body => body})
|
98
|
+
xml = REXML::Document.new(@response.body)
|
99
|
+
prop = 'd:multistatus/d:response/d:propstat/d:prop/'
|
100
|
+
|
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}
|
103
|
+
end
|
104
|
+
|
105
|
+
# Example:
|
106
|
+
# yd.exist?('/home/graph.pdf')
|
107
|
+
# => true
|
108
|
+
#
|
109
|
+
# Arguments:
|
110
|
+
# path: path to yandex disk directory or file
|
111
|
+
def exist?(path)
|
112
|
+
body = '<?xml version="1.0" encoding="utf-8"?><propfind xmlns="DAV:"><prop><displayname/></prop></propfind>'
|
113
|
+
send_propfind(0, {:path => path, :body => body})
|
114
|
+
return true
|
115
|
+
rescue RequestError
|
116
|
+
return false
|
117
|
+
end
|
118
|
+
|
119
|
+
# Example:
|
120
|
+
# yd.properties('/home/graph.pdf')
|
121
|
+
# =>
|
122
|
+
# {:name => 'graph.pdf',
|
123
|
+
# :created => (Time),
|
124
|
+
# :updated => (Time),
|
125
|
+
# :type => 'pdf',
|
126
|
+
# :size => 42432,
|
127
|
+
# :is_file => true,
|
128
|
+
# :public_url => nil}
|
129
|
+
#
|
130
|
+
# Arguments:
|
131
|
+
# path: path to yandex disk directory or file
|
132
|
+
def properties(path)
|
133
|
+
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
|
+
send_propfind(0, {:path => path, :body => body})
|
135
|
+
prop = 'd:multistatus/d:response/d:propstat/d:prop/'
|
136
|
+
xml = REXML::Document.new(@response.body)
|
137
|
+
type = xml.elements[prop + 'd:getcontenttype'].text
|
138
|
+
size = xml.elements[prop + 'd:getcontentlength'].text.to_i
|
139
|
+
|
140
|
+
return {:name => xml.elements[prop + 'd:displayname'].text,
|
141
|
+
:created => xml.elements[prop + 'd:getlastmodified'].text,
|
142
|
+
:updated => xml.elements[prop + 'd:getlastmodified'].text,
|
143
|
+
:type => type ? type : 'dir',
|
144
|
+
:size => size,
|
145
|
+
:is_file => size > 0,
|
146
|
+
:public_url => xml.elements[prop + 'public_url'].text}
|
147
|
+
end
|
148
|
+
|
149
|
+
# Example:
|
150
|
+
# yd.files('/home')
|
151
|
+
# =>
|
152
|
+
# [{:name => 'graph.pdf',
|
153
|
+
# :created => (Time),
|
154
|
+
# :updated => (Time),
|
155
|
+
# :type => 'pdf',
|
156
|
+
# :size => 42432,
|
157
|
+
# :is_file => true}]
|
158
|
+
#
|
159
|
+
# Arguments:
|
160
|
+
# 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)
|
163
|
+
send_propfind(1, {:path => path})
|
164
|
+
xml = REXML::Document.new(@response.body)
|
165
|
+
prop = 'd:propstat/d:prop/'
|
166
|
+
files = []
|
167
|
+
xml.elements.each('d:multistatus/d:response') do |res|
|
168
|
+
name = URI.decode(res.elements[prop + 'd:displayname'].text)
|
169
|
+
next if !with_root && path.split('/').last == name
|
170
|
+
size = res.elements[prop + 'd:getcontentlength'].text.to_i
|
171
|
+
|
172
|
+
files << {:name => name,
|
173
|
+
:path => URI.decode(res.elements['d:href'].text),
|
174
|
+
:created => res.elements[prop + 'd:creationdate'].text,
|
175
|
+
:updated => res.elements[prop + 'd:getlastmodified'].text,
|
176
|
+
:size => size,
|
177
|
+
:is_file => size > 0}
|
178
|
+
end
|
179
|
+
return files
|
180
|
+
end
|
181
|
+
|
182
|
+
# Example:
|
183
|
+
# yd.copy('/home/graph.pdf', 'my/work')
|
184
|
+
# => true
|
185
|
+
#
|
186
|
+
# Arguments:
|
187
|
+
# from: path to yandex disk directory or file
|
188
|
+
# to: path to yandex disk directory
|
189
|
+
def copy(from, to)
|
190
|
+
move_copy(:copy, from, to)
|
191
|
+
end
|
192
|
+
alias_method :cp, :copy
|
193
|
+
|
194
|
+
# Example:
|
195
|
+
# yd.move('/home/graph.pdf', 'my/work')
|
196
|
+
# => true
|
197
|
+
#
|
198
|
+
# Arguments:
|
199
|
+
# from: path to yandex disk directory or file
|
200
|
+
# to: path to yandex disk directory
|
201
|
+
def move(from, to)
|
202
|
+
move_copy(:move, from, to)
|
203
|
+
end
|
204
|
+
alias_method :mv, :move
|
205
|
+
|
206
|
+
# Example:
|
207
|
+
# yd.delete('/home/graph.pdf')
|
208
|
+
# => true
|
209
|
+
#
|
210
|
+
# Arguments:
|
211
|
+
# path: path to yandex disk directory or file
|
212
|
+
def delete(path)
|
213
|
+
send_request(:delete, {:path => path})
|
214
|
+
end
|
215
|
+
alias_method :del, :delete
|
216
|
+
|
217
|
+
# Example:
|
218
|
+
# yd.set_public('/home/graph.pdf')
|
219
|
+
# => http://yadi.sk/d/#############
|
220
|
+
#
|
221
|
+
# Arguments:
|
222
|
+
# path: path to yandex disk directory or file
|
223
|
+
def set_public(path)
|
224
|
+
body = '<propertyupdate xmlns="DAV:"><set><prop><public_url xmlns="urn:yandex:disk:meta">true</public_url></prop></set></propertyupdate>'
|
225
|
+
send_request(:proppatch, {:path => path, :body => body})
|
226
|
+
xml = REXML::Document.new(@response.body)
|
227
|
+
return xml.elements['d:multistatus/d:response/d:propstat/d:prop/public_url'].text
|
228
|
+
end
|
229
|
+
|
230
|
+
# Example:
|
231
|
+
# yd.set_private('/home/graph.pdf')
|
232
|
+
# => true
|
233
|
+
#
|
234
|
+
# Arguments:
|
235
|
+
# path: path to yandex disk directory or file
|
236
|
+
def set_private(path)
|
237
|
+
body = '<propertyupdate xmlns="DAV:"><remove><prop><public_url xmlns="urn:yandex:disk:meta" /></prop></remove></propertyupdate>'
|
238
|
+
send_request(:proppatch, {:path => path, :body => body})
|
239
|
+
xml = REXML::Document.new(@response.body)
|
240
|
+
return xml.elements['d:multistatus/d:response/d:propstat/d:prop/public_url'].text.nil?
|
241
|
+
end
|
242
|
+
|
243
|
+
# Example:
|
244
|
+
# yd.preview('/home/cat.jpg', 128, '/home/photo')
|
245
|
+
# => true
|
246
|
+
#
|
247
|
+
# Arguments:
|
248
|
+
# path: path to yandex disk file
|
249
|
+
# size: preview size (for details visit http://api.yandex.com/disk/doc/dg/reference/preview.xml)
|
250
|
+
# save_to: path to save
|
251
|
+
def preview(path, size, save_to)
|
252
|
+
send_request(:get, {:path => path, :preview => size.to_s})
|
253
|
+
File.open(File.join(save_to, path.split('/').last), 'w'){|f| f.write(@response.body)}
|
254
|
+
end
|
255
|
+
|
256
|
+
########## private ##########
|
257
|
+
private
|
258
|
+
|
259
|
+
def create_dest(from, to)
|
260
|
+
prop = properties(from)
|
261
|
+
to = prop[:is_file] ? to.gsub(/\/$/,'') + '/' + prop[:name] : to
|
262
|
+
return '/' + to
|
263
|
+
end
|
264
|
+
|
265
|
+
def move_copy(method, from, to)
|
266
|
+
send_request(method, {:path => from,
|
267
|
+
:headers => {'Destination' => create_dest(from, to)}})
|
268
|
+
end
|
269
|
+
|
270
|
+
def send_propfind(depth, options = {})
|
271
|
+
headers = {:headers => {'Depth' => depth.to_s,
|
272
|
+
'Content-Type' => 'text/xml; charset="utf-8"'}}
|
273
|
+
send_request(:propfind, options.merge!(headers))
|
274
|
+
end
|
275
|
+
|
276
|
+
def send_request(method, args = {})
|
277
|
+
# headers
|
278
|
+
headers = {'Authorization' => @token}
|
279
|
+
headers.merge!(args[:headers]) if args[:headers]
|
280
|
+
uri = URI.parse(YandexDisk::API_URL)
|
281
|
+
# path
|
282
|
+
path = ''
|
283
|
+
begin
|
284
|
+
unless args[:path].blank?
|
285
|
+
path = URI.encode( args[:path].split('/').reject{|it| it.blank?}.join('/') )
|
286
|
+
raise Exception if path.empty?
|
287
|
+
# image preview
|
288
|
+
path << "?preview&size=#{args[:preview]}" if args[:preview]
|
289
|
+
end
|
290
|
+
rescue Exception => e
|
291
|
+
raise RequestError, 'Path has bad format.'
|
292
|
+
end
|
293
|
+
request_path = uri.request_uri + path
|
294
|
+
# init
|
295
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
296
|
+
# ssl
|
297
|
+
if uri.scheme == 'https'
|
298
|
+
http.use_ssl = true
|
299
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
300
|
+
end
|
301
|
+
# debug
|
302
|
+
http.set_debug_output($stderr) if YandexDisk::DEBUG
|
303
|
+
# method
|
304
|
+
req = nil
|
305
|
+
case method
|
306
|
+
when :put then
|
307
|
+
req = Net::HTTP::Put.new(request_path, headers)
|
308
|
+
req.body_stream = Chunked.new(@file, args[:chunk_size])
|
309
|
+
when :get then
|
310
|
+
req = Net::HTTP::Get.new(request_path, headers)
|
311
|
+
when :mkcol then
|
312
|
+
req = Net::HTTP::Mkcol.new(request_path, headers)
|
313
|
+
when :propfind then
|
314
|
+
req = Net::HTTP::Propfind.new(request_path, headers)
|
315
|
+
when :copy then
|
316
|
+
req = Net::HTTP::Copy.new(request_path, headers)
|
317
|
+
when :move then
|
318
|
+
req = Net::HTTP::Move.new(request_path, headers)
|
319
|
+
when :delete then
|
320
|
+
req = Net::HTTP::Delete.new(request_path, headers)
|
321
|
+
when :proppatch
|
322
|
+
req = Net::HTTP::Proppatch.new(request_path, headers)
|
323
|
+
else
|
324
|
+
raise RequestError, "Method #{method} not supported."
|
325
|
+
end
|
326
|
+
# start
|
327
|
+
req.body = args[:body] if args[:body]
|
328
|
+
http.start{|h| @response = h.request(req) }
|
329
|
+
successful?(method, request_path)
|
330
|
+
end
|
331
|
+
|
332
|
+
def successful?(method, path)
|
333
|
+
if [200, 201, 207].include?(@response.code.to_i) ||
|
334
|
+
@response.body.include?('resource already exists') ||
|
335
|
+
(method == :delete && @response.body.include?('resource not found'))
|
336
|
+
true
|
337
|
+
else
|
338
|
+
raise RequestError, "#{@response.code.to_i} #{@response.message}: #{@response.body} on #{path}"
|
339
|
+
end
|
340
|
+
end
|
341
|
+
end
|
342
|
+
|
343
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# gist from sasimpson
|
2
|
+
module YandexDisk
|
3
|
+
class Chunked
|
4
|
+
def initialize(data, chunk_size)
|
5
|
+
@size = chunk_size
|
6
|
+
if data.respond_to? :read
|
7
|
+
@file = data
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def read(foo)
|
12
|
+
if @file
|
13
|
+
@file.read(@size)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
def eof!
|
17
|
+
@file.eof!
|
18
|
+
end
|
19
|
+
def eof?
|
20
|
+
@file.eof?
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/yandex_disk.rb
ADDED
data/spec/files/README
ADDED
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
Hi developer.
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'simplecov'
|
3
|
+
SimpleCov.start
|
4
|
+
require 'rubygems'
|
5
|
+
require 'bundler/setup'
|
6
|
+
require 'yandex_disk'
|
7
|
+
require 'fileutils'
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.fail_fast = true
|
11
|
+
config.before(:each) do
|
12
|
+
init_test_files
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# login and password for yandex disk
|
17
|
+
LOGIN = ''
|
18
|
+
PWD = ''
|
19
|
+
|
20
|
+
# test settings
|
21
|
+
FILES_PATH = File.dirname(__FILE__) + '/files'
|
22
|
+
DOWNLOAD_PATH = FILES_PATH + '/downloads'
|
23
|
+
FILE_TEXT = 'Hi developer.'
|
24
|
+
|
25
|
+
def init_test_files
|
26
|
+
FileUtils.mkdir_p(DOWNLOAD_PATH) if !Dir.exist?(DOWNLOAD_PATH)
|
27
|
+
@text_file = FILES_PATH + '/sample.txt'
|
28
|
+
File.open(@text_file, 'w+'){|f| f.write(FILE_TEXT)} unless File.file? @text_file
|
29
|
+
@text_file.freeze
|
30
|
+
@image_file = FILES_PATH + '/sample.jpg'
|
31
|
+
@image_file.freeze
|
32
|
+
raise Exception, "File sample.jpg not found in #{FILES_PATH}" unless File.file? @image_file
|
33
|
+
end
|
34
|
+
|
35
|
+
def clear!
|
36
|
+
@yd.delete('/my').should be_true
|
37
|
+
FileUtils.rm_rf DOWNLOAD_PATH
|
38
|
+
end
|
39
|
+
|
@@ -0,0 +1,180 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'net/http'
|
4
|
+
require 'uri'
|
5
|
+
require 'fastimage'
|
6
|
+
|
7
|
+
describe YandexDisk do
|
8
|
+
|
9
|
+
it 'should return version' do
|
10
|
+
YandexDisk.version.should equal YandexDisk::VERSION
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'validate' do
|
14
|
+
it 'authorization' do
|
15
|
+
yd = YandexDisk.login('', '')
|
16
|
+
expect{yd.size}.to raise_error YandexDisk::RequestError
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'file' do
|
20
|
+
yd = YandexDisk.login('', '')
|
21
|
+
expect{yd.upload('not/exist/file')}.to raise_error YandexDisk::RequestError
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe 'api' do
|
26
|
+
before(:each) do
|
27
|
+
@yd = YandexDisk.login(LOGIN, PWD)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should return available and used space' do
|
31
|
+
size = @yd.size
|
32
|
+
expect(size[:used] > 0 && size[:available] > 0).to be_true
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should return list of files' do
|
36
|
+
@yd.files.each do |item|
|
37
|
+
[:name, :path, :created, :updated, :size, :is_file].each{|res| expect(!item[res].to_s.empty?).to be_true}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should return file or dir properties' do
|
42
|
+
# file
|
43
|
+
@yd.upload(@text_file, 'my', {:force => true})
|
44
|
+
properties1 = @yd.properties('my/' + File.basename(@text_file))
|
45
|
+
[:created, :updated, :type, :size, :is_file].each{|res| expect(properties1[res].to_s.empty?).to be_false}
|
46
|
+
properties1[:type].should eq 'text/plain'
|
47
|
+
# dir
|
48
|
+
dir = '/my/photo'
|
49
|
+
@yd.create_path(dir)
|
50
|
+
properties2 = @yd.properties(dir)
|
51
|
+
properties2[:is_file].should be_false
|
52
|
+
clear!
|
53
|
+
end
|
54
|
+
|
55
|
+
describe 'directory' do
|
56
|
+
it 'should be created' do
|
57
|
+
dir = 'my/photos/cats'
|
58
|
+
@yd.create_path(dir).should be_true
|
59
|
+
@yd.properties(dir)[:is_file].should be_false
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should be copied' do
|
63
|
+
# create directories with Cyrillic name
|
64
|
+
src = 'my/старые файлы'
|
65
|
+
des = 'my/здесь новые файлы'
|
66
|
+
|
67
|
+
@yd.create_path(src)
|
68
|
+
@yd.create_path(des)
|
69
|
+
# create 2 files
|
70
|
+
files = {'file1.txt' => 'data one', 'file2.txt' => 'data two'}
|
71
|
+
files.each do |file, text|
|
72
|
+
new_file = File.join(FILES_PATH, file)
|
73
|
+
File.open(new_file, 'w'){|f| f.write(text)}
|
74
|
+
@yd.upload(new_file, src).should be_true
|
75
|
+
end
|
76
|
+
# copy directory
|
77
|
+
@yd.copy(src, des)
|
78
|
+
# check files
|
79
|
+
@yd.files(src, false).size.should eq 2
|
80
|
+
files.each do |file, text|
|
81
|
+
download_and_validate(File.join(des, file), text)
|
82
|
+
end
|
83
|
+
# clear
|
84
|
+
files.each{|file, text| File.delete( File.join(FILES_PATH, file) )}
|
85
|
+
end
|
86
|
+
|
87
|
+
after(:each) do
|
88
|
+
clear!
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe 'file' do
|
93
|
+
it 'should be uploaded' do
|
94
|
+
@yd.upload(@text_file, 'my', {:force => true}).should be_true
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'should be downloaded and valid' do
|
98
|
+
@yd.upload(@text_file, 'my', {:force => true}).should be_true
|
99
|
+
path = 'my/' + File.basename(@text_file)
|
100
|
+
download_and_validate(path)
|
101
|
+
end
|
102
|
+
# its related to copy dir
|
103
|
+
it 'should by copied to new directory' do
|
104
|
+
@yd.upload(@text_file, 'my', {:force => true})
|
105
|
+
f_name = File.basename(@text_file)
|
106
|
+
file = 'my/' + f_name
|
107
|
+
new_path = 'my/text'
|
108
|
+
@yd.create_path(new_path)
|
109
|
+
@yd.copy(file, new_path)
|
110
|
+
# file still exist in src path
|
111
|
+
@yd.exist?(file).should be_true
|
112
|
+
download_and_validate(File.join(new_path, f_name))
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'should by moved to new directory' do
|
116
|
+
@yd.upload(@text_file, 'my', {:force => true})
|
117
|
+
f_name = File.basename(@text_file)
|
118
|
+
file = 'my/' + f_name
|
119
|
+
new_path = 'my/text'
|
120
|
+
@yd.create_path(new_path)
|
121
|
+
@yd.move(file, new_path)
|
122
|
+
# file not exist in src path
|
123
|
+
@yd.exist?(file).should be_false
|
124
|
+
download_and_validate(File.join(new_path, f_name))
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'should by deleted' do
|
128
|
+
# upload file
|
129
|
+
@yd.upload(@text_file)
|
130
|
+
f_name = File.basename(@text_file)
|
131
|
+
download_and_validate(f_name)
|
132
|
+
@yd.delete(f_name)
|
133
|
+
@yd.exist?(f_name).should be_false
|
134
|
+
end
|
135
|
+
|
136
|
+
it 'should be public'do
|
137
|
+
@yd.upload(@image_file, 'my', {:force => true})
|
138
|
+
# mask: http://yadi.sk/d/#############
|
139
|
+
expect(@yd.set_public('my/' + File.basename(@image_file)) =~ /http:\/\/yadi\.sk\/d\/.+/).to be_true
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'should be private'do
|
143
|
+
@yd.upload(@text_file, 'my', {:force => true})
|
144
|
+
f_name = 'my/' + File.basename(@text_file)
|
145
|
+
# mask: http://yadi.sk/d/#############
|
146
|
+
expect(@yd.set_public(f_name) =~ /http:\/\/yadi\.sk\/d\/.+/).to be_true
|
147
|
+
expect(@yd.set_private(f_name)).to be_true
|
148
|
+
end
|
149
|
+
|
150
|
+
describe 'preview' do
|
151
|
+
before(:each)do
|
152
|
+
@yd.upload(@image_file, 'my', {:force => true}).should be_true
|
153
|
+
@f_name = File.basename(@image_file)
|
154
|
+
end
|
155
|
+
|
156
|
+
it 'should return image in M size'do
|
157
|
+
@yd.preview('my/' + @f_name, 'm', DOWNLOAD_PATH)
|
158
|
+
File.file?( File.join(DOWNLOAD_PATH, @f_name) ).should be_true
|
159
|
+
end
|
160
|
+
|
161
|
+
it 'should return image in 300x250 size'do
|
162
|
+
@yd.preview('my/' + @f_name, '300x250', DOWNLOAD_PATH)
|
163
|
+
FastImage.size( File.join(DOWNLOAD_PATH, @f_name) ).should eq [300, 250]
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
after(:each) do
|
168
|
+
clear!
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
end
|
173
|
+
|
174
|
+
def download_and_validate(download_file, text = FILE_TEXT)
|
175
|
+
@yd.download(download_file, DOWNLOAD_PATH).should be_true
|
176
|
+
file = download_file.split('/').last
|
177
|
+
expect( File.read( File.join(DOWNLOAD_PATH, file) ) ).to eq(text)
|
178
|
+
end
|
179
|
+
|
180
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yandex_disk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Boris Murga
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: simplecov
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: fastimage
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: An easy-to-use client library for Yandex Disk API
|
56
|
+
email: denwwer.c4@gmail.com
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- lib/yandex_disk.rb
|
62
|
+
- lib/yandex_disk/chunked.rb
|
63
|
+
- lib/yandex_disk/ext.rb
|
64
|
+
- lib/yandex_disk/api.rb
|
65
|
+
- lib/yandex_disk/cfg.rb
|
66
|
+
- spec/spec_helper.rb
|
67
|
+
- spec/files/README
|
68
|
+
- spec/files/sample.jpg
|
69
|
+
- spec/files/sample.txt
|
70
|
+
- spec/yandex_disk_spec.rb
|
71
|
+
homepage: https://github.com/denwwer/yandex_disk
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message: Thanks for installation, check https://github.com/denwwer/yandex_disk
|
76
|
+
for news.
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.1.9
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: API for Yandex Disk
|
96
|
+
test_files:
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
- spec/files/README
|
99
|
+
- spec/files/sample.jpg
|
100
|
+
- spec/files/sample.txt
|
101
|
+
- spec/yandex_disk_spec.rb
|