Class: YandexDisk::Api
Instance Method Summary collapse
-
#copy(from, to) ⇒ Object
(also: #cp)
Example: yd.copy(‘/home/graph.pdf’, ‘my/work’) => true.
-
#create_path(path) ⇒ Object
(also: #mkdir)
Example: yd.create_path(‘/home/my/photos’) => true.
-
#delete(path) ⇒ Object
(also: #del)
Example: yd.delete(‘/home/graph.pdf’) => true.
-
#download(file, save_path = nil) ⇒ Object
Example: yd.download(‘/home/graph.pdf’, ‘/home’) => true (or file content).
-
#exist?(path) ⇒ Boolean
Example: yd.exist?(‘/home/graph.pdf’) => true.
-
#files(path = '', options = {}) ⇒ Object
(also: #ls)
Example: yd.files(‘/home’) => [=> ‘graph.pdf’, :created => (Time), :updated => (Time), :type => ‘pdf’, :size => 42432, :is_file => true].
-
#initialize(login, pwd) ⇒ Api
constructor
A new instance of Api.
-
#move(from, to) ⇒ Object
(also: #mv)
Example: yd.move(‘/home/graph.pdf’, ‘my/work’) => true.
-
#preview(path, size, save_to) ⇒ Object
Example: yd.preview(‘/home/cat.jpg’, 128, ‘/home/photo’) => true.
-
#properties(path, options = {}) ⇒ Object
Example: yd.properties(‘/home/graph.pdf’) => => ‘graph.pdf’, :created => (Time), :updated => (Time), :type => ‘pdf’, :size => 42432, :is_file => true, :public_url => nil.
-
#set_private(path) ⇒ Object
Example: yd.set_private(‘/home/graph.pdf’) => true.
-
#set_public(path) ⇒ Object
Example: yd.set_public(‘/home/graph.pdf’) => yadi.sk/d/#############.
-
#size(options = {}) ⇒ Object
Example: yd.size => => 312312, :used => 3123 Arguments: options: readable: return size in human readable format e.g, 100K 128M 1G (false for default).
-
#upload(file, path = '', options = {}) ⇒ Object
Example: yd.upload(‘/home/graph.pdf’, ‘my/work’) => true.
Constructor Details
#initialize(login, pwd) ⇒ Api
Returns a new instance of Api.
18 19 20 |
# File 'lib/yandex_disk/api.rb', line 18 def initialize(login, pwd) @token = 'Basic ' + Base64.encode64("#{login}:#{pwd}") end |
Instance Method Details
#copy(from, to) ⇒ Object Also known as: cp
Example:
yd.copy('/home/graph.pdf', 'my/work')
=> true
Arguments:
from: path to yandex disk directory or file
to: path to yandex disk directory
199 200 201 |
# File 'lib/yandex_disk/api.rb', line 199 def copy(from, to) move_copy(:copy, from, to) end |
#create_path(path) ⇒ Object Also known as: mkdir
Example:
yd.create_path('/home/my/photos')
=> true
Arguments:
path: path to yandex disk directory hierarchy
85 86 87 88 89 90 91 92 |
# File 'lib/yandex_disk/api.rb', line 85 def create_path(path) c_path = '' path.split('/').each do |p| next if p.empty? c_path << p + '/' send_request(:mkcol, :path => c_path) end end |
#delete(path) ⇒ Object Also known as: del
Example:
yd.delete('/home/graph.pdf')
=> true
Arguments:
path: path to yandex disk directory or file
222 223 224 |
# File 'lib/yandex_disk/api.rb', line 222 def delete(path) send_request(:delete, :path => path) end |
#download(file, save_path = nil) ⇒ Object
Example:
yd.download('/home/graph.pdf', '/home')
=> true (or file content)
Arguments:
file: path to yandex disk file
save_path: path to save. If save_path not present - return file content
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/yandex_disk/api.rb', line 55 def download(file, save_path = nil) option = {:path => file, :headers => {'TE' => 'chunked', 'Accept-Encoding' => 'gzip'}} send_request(:get, option) # unzip if zipped if gzip?(@response.header['Content-Encoding']) s_io = StringIO.new(@response.body) gz = Zlib::GzipReader.new(s_io) data = gz.read else data = @response.body end if save_path File.open(File.join(save_path, file.split('/').last), 'w'){|f| f.write(data)} return true else return data end end |
#exist?(path) ⇒ Boolean
Example:
yd.exist?('/home/graph.pdf')
=> true
Arguments:
path: path to yandex disk directory or file
119 120 121 122 123 124 |
# File 'lib/yandex_disk/api.rb', line 119 def exist?(path) body = '<?xml version="1.0" encoding="utf-8"?><propfind xmlns="DAV:"><prop><displayname/></prop></propfind>' send_propfind(0, :path => path, :body => body) rescue RequestError return false end |
#files(path = '', options = {}) ⇒ Object Also known as: ls
Example:
yd.files('/home')
=>
[{:name => 'graph.pdf',
:created => (Time),
:updated => (Time),
:type => 'pdf',
:size => 42432,
:is_file => true}]
Arguments:
path: path to yandex disk directory (default is <b>root</b>)
root: include information of root directory or not (<b>false</b> for default)
h_size: return size in human readable format e.g, 100K 128M 1G (false for default)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/yandex_disk/api.rb', line 171 def files(path = '', = {}) send_propfind(1, :path => path) xml = REXML::Document.new(@response.body) prop = 'd:propstat/d:prop/' files = [] xml.elements.each('d:multistatus/d:response') do |res| name = URI.decode(res.elements[prop + 'd:displayname'].text) next if ![:root] && path.split('/').last == name size = res.elements[prop + 'd:getcontentlength'].text.to_i rescue 0 files << {:name => name, :path => URI.decode(res.elements['d:href'].text), :created => res.elements[prop + 'd:creationdate'].text, :updated => res.elements[prop + 'd:getlastmodified'].text, :size => size.to_readable([:h_size]), :is_file => size > 0} end return files end |
#move(from, to) ⇒ Object Also known as: mv
Example:
yd.move('/home/graph.pdf', 'my/work')
=> true
Arguments:
from: path to yandex disk directory or file
to: path to yandex disk directory
211 212 213 |
# File 'lib/yandex_disk/api.rb', line 211 def move(from, to) move_copy(:move, from, to) end |
#preview(path, size, save_to) ⇒ Object
Example:
yd.preview('/home/cat.jpg', 128, '/home/photo')
=> true
Arguments:
path: path to yandex disk file
size: preview size (for details visit http://api.yandex.com/disk/doc/dg/reference/preview.xml)
save_to: path to save
261 262 263 264 |
# File 'lib/yandex_disk/api.rb', line 261 def preview(path, size, save_to) send_request(:get, :path => path, :preview => size.to_s) File.open(File.join(save_to, path.split('/').last), 'w'){|f| f.write(@response.body)} end |
#properties(path, options = {}) ⇒ Object
Example:
yd.properties('/home/graph.pdf')
=>
{:name => 'graph.pdf',
:created => (Time),
:updated => (Time),
:type => 'pdf',
:size => 42432,
:is_file => true,
:public_url => nil}
Arguments:
path: path to yandex disk directory or file
h_size: return size in human readable format e.g, 100K 128M 1G (false for default)
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/yandex_disk/api.rb', line 140 def properties(path, = {}) 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>' send_propfind(0, :path => path, :body => body) prop = 'd:multistatus/d:response/d:propstat/d:prop/' xml = REXML::Document.new(@response.body) type = xml.elements[prop + 'd:getcontenttype'].text size = xml.elements[prop + 'd:getcontentlength'].text.to_i return {:name => xml.elements[prop + 'd:displayname'].text, :created => xml.elements[prop + 'd:getlastmodified'].text, :updated => xml.elements[prop + 'd:getlastmodified'].text, :type => type ? type : 'dir', :size => size.to_readable([:h_size]), :is_file => size > 0, :public_url => xml.elements[prop + 'public_url'].text} end |
#set_private(path) ⇒ Object
Example:
yd.set_private('/home/graph.pdf')
=> true
Arguments:
path: path to yandex disk directory or file
246 247 248 249 250 251 |
# File 'lib/yandex_disk/api.rb', line 246 def set_private(path) body = '<propertyupdate xmlns="DAV:"><remove><prop><public_url xmlns="urn:yandex:disk:meta" /></prop></remove></propertyupdate>' send_request(:proppatch, :path => path, :body => body) xml = REXML::Document.new(@response.body) return xml.elements['d:multistatus/d:response/d:propstat/d:prop/public_url'].text.nil? end |
#set_public(path) ⇒ Object
Example:
yd.set_public('/home/graph.pdf')
=> http://yadi.sk/d/#############
Arguments:
path: path to yandex disk directory or file
233 234 235 236 237 238 |
# File 'lib/yandex_disk/api.rb', line 233 def set_public(path) body = '<propertyupdate xmlns="DAV:"><set><prop><public_url xmlns="urn:yandex:disk:meta">true</public_url></prop></set></propertyupdate>' send_request(:proppatch, :path => path, :body => body) xml = REXML::Document.new(@response.body) return xml.elements['d:multistatus/d:response/d:propstat/d:prop/public_url'].text end |
#size(options = {}) ⇒ Object
Example:
yd.size
=> {:available => 312312, :used => 3123}
Arguments:
options:
readable: return size in human readable format e.g, 100K 128M 1G (false for default)
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/yandex_disk/api.rb', line 101 def size( = {}) 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>' send_propfind(0, :body => body) xml = REXML::Document.new(@response.body) prop = 'd:multistatus/d:response/d:propstat/d:prop/' available_b = xml.elements[prop + 'd:quota-available-bytes'].text.to_i used_b = xml.elements[prop + 'd:quota-used-bytes'].text.to_i return {:available => available_b.to_readable([:h_size]), :used => used_b.to_readable([:h_size])} end |
#upload(file, path = '', options = {}) ⇒ Object
Example:
yd.upload('/home/graph.pdf', 'my/work')
=> true
Arguments:
file: path to file
path: path to yandex disk directory. If path not present - save to root
options:
chunk_size: file chunk size (default is 100)
force: create path structure if not exist (raise <b>RequestError</b> if <b>path</b> not exist for default)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/yandex_disk/api.rb', line 32 def upload(file, path = '', = {}) # valid file? raise RequestError, "File not found." if file.nil? || !File.file?(file) # create path create_path(path) if [:force] [:chunk_size] ||= 1024 @file = File.open(file) [:headers] = {'Expect' => '100-continue', 'Transfer-Encoding' => 'chunked', 'content-type' => 'application/binary'} send_request(:put, .merge({:path => File.join( path, File.basename(file) )})) @file.close return true end |