Class: Yandex::Disk::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/yandex/disk/client.rb

Defined Under Namespace

Modules: Request

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/yandex/disk/client.rb', line 14

def initialize options={}
  @timeout = options[:timeout] || 300
  @http = Faraday.new(:url => 'https://webdav.yandex.ru') do |builder|
    if options[:access_token] && options[:access_token].length > 0
      builder.request :authorization, "OAuth", options[:access_token]
    elsif options[:login] && options[:password]
      basic_token = Base64.encode64("#{options[:login]}:#{options[:password]}")
      builder.request :authorization, "Basic", basic_token
    else
      raise ArgumentError, 'No :access_token or :login and :password'
    end

    if faraday_configurator = options[:faraday_configurator]
      faraday_configurator.call(builder)
    else
      builder.adapter :excon
    end
  end
end

Instance Method Details

#copy(src, dest) ⇒ Object



76
77
78
# File 'lib/yandex/disk/client.rb', line 76

def copy src, dest
  file_operation_response('COPY', src, dest).success?
end

#copy!(src, dest) ⇒ Object



80
81
82
83
# File 'lib/yandex/disk/client.rb', line 80

def copy! src, dest
  res = file_operation_response('COPY', src, dest)
  raise res.body unless res.success?
end

#delete(path) ⇒ Object



94
95
96
# File 'lib/yandex/disk/client.rb', line 94

def delete path
  delete_response(path).success?
end

#delete!(path) ⇒ Object



98
99
100
101
# File 'lib/yandex/disk/client.rb', line 98

def delete! path
  res = delete_response(path)
  raise res.body unless res.success?
end

#get(path) ⇒ Object



52
53
54
# File 'lib/yandex/disk/client.rb', line 52

def get path
  @http.get(path)
end

#list(path) ⇒ Object



61
62
63
64
# File 'lib/yandex/disk/client.rb', line 61

def list path
  request = Request::List.new(@http, path)
  request.perform
end

#mkcol(path) ⇒ Object Also known as: mkdir



43
44
45
# File 'lib/yandex/disk/client.rb', line 43

def mkcol path
  mkcol_response(path).success?
end

#mkcol!Object



47
48
49
50
# File 'lib/yandex/disk/client.rb', line 47

def mkcol!
  res = mkcol_response(path)
  raise res.body unless res.success?
end

#mkdir_p(path) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/yandex/disk/client.rb', line 68

def mkdir_p path
  path_parts = []
  path.split('/').each do |part|
    path_parts << part
    mkdir(path_parts.join('/'))
  end
end

#move(src, dest) ⇒ Object



85
86
87
# File 'lib/yandex/disk/client.rb', line 85

def move src, dest
  file_operation_response('MOVE', src, dest).success?
end

#move!(src, dest) ⇒ Object



89
90
91
92
# File 'lib/yandex/disk/client.rb', line 89

def move! src, dest
  res = file_operation_response('MOVE', src, dest)
  raise res.body unless res.success?
end

#put(src, dest) ⇒ Object



34
35
36
# File 'lib/yandex/disk/client.rb', line 34

def put src, dest
  put_response(src, dest).success?
end

#put!(src, dest) ⇒ Object



38
39
40
41
# File 'lib/yandex/disk/client.rb', line 38

def put! src, dest
  res = put_response(src, dest)
  raise res.body unless res.success?
end

#spaceObject



56
57
58
59
# File 'lib/yandex/disk/client.rb', line 56

def space
  request = Request::Space.new(@http)
  request.perform
end