Class: YandexDisk::Api

Inherits:
Object show all
Defined in:
lib/yandex_disk/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(login, pwd) ⇒ Api

Returns a new instance of Api.



16
17
18
# File 'lib/yandex_disk/api.rb', line 16

def initialize(, pwd)
  @token = 'Basic ' + Base64.encode64("#{}:#{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


189
190
191
# File 'lib/yandex_disk/api.rb', line 189

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


82
83
84
85
86
87
88
89
# File 'lib/yandex_disk/api.rb', line 82

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


212
213
214
# File 'lib/yandex_disk/api.rb', line 212

def delete(path)
  send_request(:delete, {:path => path})
end

#download(file, save_path) ⇒ Object

Example:

yd.download('/home/graph.pdf', '/home')
=> true

Arguments:

file: path to yandex disk file
save_path: path to save


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/yandex_disk/api.rb', line 55

def download(file, save_path)
  option = {:path => file,
            :headers => {'TE' => 'chunked',
                         'Accept-Encoding' => 'gzip'}}

  send_request(:get, option)

  data = nil
  # unzip if zipped
  if @response.header['Content-Encoding'] == 'gzip'
    sio = StringIO.new( @response.body )
    gz = Zlib::GzipReader.new( sio )
    data = gz.read
  else
    data = @response.body
  end
  File.open(File.join(save_path, file.split('/').last), 'w'){|f| f.write(data)}

  return true
end

#exist?(path) ⇒ Boolean

Example:

yd.exist?('/home/graph.pdf')
=> true

Arguments:

path: path to yandex disk directory or file

Returns:

  • (Boolean)


111
112
113
114
115
116
117
# File 'lib/yandex_disk/api.rb', line 111

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})
  return true
rescue RequestError
  return false
end

#files(path = '', with_root = true) ⇒ Object

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>)
with_root: include information of root directory or not (<b>false</b> for default)


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/yandex_disk/api.rb', line 162

def files(path = '', with_root = true)
  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 !with_root && path.split('/').last == name
    size = res.elements[prop + 'd:getcontentlength'].text.to_i

    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,
              :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


201
202
203
# File 'lib/yandex_disk/api.rb', line 201

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


251
252
253
254
# File 'lib/yandex_disk/api.rb', line 251

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) ⇒ 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


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/yandex_disk/api.rb', line 132

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,
          :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


236
237
238
239
240
241
# File 'lib/yandex_disk/api.rb', line 236

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


223
224
225
226
227
228
# File 'lib/yandex_disk/api.rb', line 223

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

#sizeObject

Example:

yd.size
=> {:available => 312312, :used => 3123}


95
96
97
98
99
100
101
102
103
# File 'lib/yandex_disk/api.rb', line 95

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/'

  return {:available => xml.elements[prop + 'd:quota-available-bytes'].text.to_i,
         :used => xml.elements[prop + 'd:quota-used-bytes'].text.to_i}
end

#upload(file, path = '', options = {}) ⇒ Object

TODO gzip file when sending Example:

yd.upload('/home/graph.pdf', 'my/work')
=> true

Arguments:

file: path to file
path: path to yandex disk directory (default is <b>root</b>)
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)

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/yandex_disk/api.rb', line 31

def upload(file, path = '', options = {})
  # valid file?
  raise RequestError, "File not found." if file.nil? || !File.file?(file)
  # create path
  create_path(path) if options[:force]
  options[:chunk_size] ||= 100
  @file = File.open(file)
  options[:headers] = {'Expect' => '100-continue',
                       #'Content-Encoding' => 'gzip',
                       'Transfer-Encoding' => 'chunked',
                       'content-type' => 'application/binary'}

  send_request(:put, options.merge({:path => File.join( path, File.basename(file) )}))
  @file.close
  return true
end