yandex-disk 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c71e6ea1679231d1f83ea5afeb8a2eb379f65b18
4
- data.tar.gz: 8f92f22ce3ec568e9c5746a71ceb5c3a5d797bdf
3
+ metadata.gz: 57ed5ceebc98623641748f8f16b410c8b66118c4
4
+ data.tar.gz: c832f15ada10ebdbb3c770138d34941a25c6e518
5
5
  SHA512:
6
- metadata.gz: dacd55bae8a4d95c9d2bfd77309ae5bf80595567f4cb3cece204a4d67e4ff56b15991fbe02ddcf6245cfbe31c4bbadbda5f9b9d1bb2b2f53a3c49dce670dd6f2
7
- data.tar.gz: fbcafcd34a7a00d709ad6661ea0061a5c2bda0be542f0989cc436a70ce815a9fb457f83c94513e1c4004ad4f9e6ba2b3b33bdf5f6c620aeced4a48b93c179c49
6
+ metadata.gz: f592e2c576488414f639f3985ece832deac747078e47f30987f562556027b9faa7e7094eeb55e3d9a0a287d68dc53a60ccc13cf17c2865a30b689fd479cae910
7
+ data.tar.gz: fc156a49408e7235bda13f34c566235df523acef5a6bbda78bcad32890c7a2c9d6bbd149e7941623c28d1edab4ff8283c66cd98930db2482bdec0d05c4aebaae
@@ -25,7 +25,7 @@ module Backup
25
25
  src = File.join(Config.tmp_path, filename)
26
26
  dest = File.join(remote_path, filename)
27
27
  Logger.info "Storing '#{ dest }'..."
28
- disk.put(src, dest)
28
+ disk.put!(src, dest)
29
29
  end
30
30
  end
31
31
 
@@ -7,77 +7,112 @@ module Yandex
7
7
  module Disk
8
8
  class Client
9
9
 
10
- def initialize options={}
11
- @http = Faraday.new(:url => 'https://webdav.yandex.ru') do |builder|
12
- builder.request :authorization, "OAuth", options[:access_token]
10
+ def initialize options={}
11
+ @http = Faraday.new(:url => 'https://webdav.yandex.ru') do |builder|
12
+ builder.request :authorization, "OAuth", options[:access_token]
13
13
 
14
- builder.response :follow_redirects
14
+ builder.response :follow_redirects
15
15
 
16
- if faraday_configurator = options[:faraday_configurator]
17
- faraday_configurator.call(builder)
18
- else
19
- builder.adapter :excon
16
+ if faraday_configurator = options[:faraday_configurator]
17
+ faraday_configurator.call(builder)
18
+ else
19
+ builder.adapter :excon
20
+ end
20
21
  end
21
22
  end
22
- end
23
23
 
24
- def put src, dest
25
- res = @http.put do |req|
26
- req.url dest
27
- req.headers['Content-Type'] = 'application/binary'
28
- req.body = File.open(src)
24
+ def put src, dest
25
+ put_response(src, dest).success?
29
26
  end
30
- res.success?
31
- end
32
27
 
33
- def mkcol path
34
- request = @http.build_request('MKCOL') do |req|
35
- req.url(path)
28
+ def put! src, dest
29
+ res = put_response(src, dest)
30
+ raise res.body unless res.success?
36
31
  end
37
32
 
38
- env = request.to_env(@http)
39
- res = @http.app.call(env)
40
- res.success?
41
- end
33
+ def mkcol path
34
+ mkcol_response(path).success?
35
+ end
42
36
 
43
- alias_method :mkdir, :mkcol
37
+ def mkcol!
38
+ res = mkcol_response(path)
39
+ raise res.body unless res.success?
40
+ end
44
41
 
45
- def mkdir_p path
46
- path_parts = []
47
- path.split('/').each do |part|
48
- path_parts << part
49
- mkdir(path_parts.join('/'))
42
+ def get path
43
+ @http.get(path)
50
44
  end
51
- end
52
45
 
53
- def get path
54
- @http.get(path)
55
- end
46
+ alias_method :mkdir, :mkcol
56
47
 
57
- def copy src, dest
58
- file_operation 'COPY', src, dest
59
- end
48
+ def mkdir_p path
49
+ path_parts = []
50
+ path.split('/').each do |part|
51
+ path_parts << part
52
+ mkdir(path_parts.join('/'))
53
+ end
54
+ end
60
55
 
61
- def move src, dest
62
- file_operation 'MOVE', src, dest
63
- end
56
+ def copy src, dest
57
+ file_operation_response('COPY', src, dest).success?
58
+ end
64
59
 
65
- def delete path
66
- @http.delete(path).success?
67
- end
60
+ def copy! src, dest
61
+ res = file_operation_response('COPY', src, dest)
62
+ raise res.body unless res.success?
63
+ end
64
+
65
+ def move src, dest
66
+ file_operation_response('MOVE', src, dest).success?
67
+ end
68
68
 
69
- private
69
+ def move! src, dest
70
+ res = file_operation_response('MOVE', src, dest)
71
+ raise res.body unless res.success?
72
+ end
70
73
 
71
- def file_operation op, src, dest
72
- request = @http.build_request(op) do |req|
73
- req.url(path)
74
- req.headers['Destination'] = dest
74
+ def delete path
75
+ delete_response(path).success?
75
76
  end
76
77
 
77
- env = request.to_env(@http)
78
- res = @http.app.call(env)
79
- res.success?
78
+ def delete! path
79
+ res = delete_response(path)
80
+ raise res.body unless res.success?
81
+ end
82
+
83
+ private
84
+
85
+ def put_response src, dest
86
+ @http.put do |req|
87
+ req.url dest
88
+ req.headers['Content-Type'] = 'application/binary'
89
+ req.body = File.open(src)
90
+ end
91
+ end
92
+
93
+ def mkcol_response path
94
+ request = @http.build_request('MKCOL') do |req|
95
+ req.url(path)
96
+ end
97
+
98
+ env = request.to_env(@http)
99
+ @http.app.call(env)
100
+ end
101
+
102
+ def file_operation_response op, src, dest
103
+ request = @http.build_request(op) do |req|
104
+ req.url(path)
105
+ req.headers['Destination'] = dest
106
+ end
107
+
108
+ env = request.to_env(@http)
109
+ res = @http.app.call(env)
110
+ res.success?
111
+ end
112
+
113
+ def delete_response path
114
+ @http.delete(path)
115
+ end
80
116
  end
81
117
  end
82
- end
83
118
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Yandex
4
4
  module Disk
5
- VERSION = "0.0.1"
5
+ VERSION = "0.0.2"
6
6
  end
7
7
  end
@@ -17,6 +17,17 @@ class YandexDiskTest < MiniTest::Unit::TestCase
17
17
  assert_equal File.read('README.md'), remote.body
18
18
  end
19
19
 
20
+ def test_put!
21
+ assert_raises RuntimeError do
22
+ begin
23
+ @disk.put!('README.md', '/folder/that/doesnt/exists/README.md')
24
+ rescue RuntimeError => e
25
+ assert_equal 'store: parent folder was not found', e.message
26
+ raise e
27
+ end
28
+ end
29
+ end
30
+
20
31
  def test_delete_and_mkdir
21
32
  @disk.delete('/a')
22
33
  assert !@disk.mkdir('/a/b')
@@ -25,6 +36,18 @@ class YandexDiskTest < MiniTest::Unit::TestCase
25
36
  assert @disk.delete('/a')
26
37
  end
27
38
 
39
+ def test_delete!
40
+ @disk.delete('/a')
41
+ assert_raises RuntimeError do
42
+ begin
43
+ @disk.delete!('/a')
44
+ rescue RuntimeError => e
45
+ assert_equal 'resource not found', e.message
46
+ raise e
47
+ end
48
+ end
49
+ end
50
+
28
51
  def test_put_in_nested_folder
29
52
  assert !@disk.put('README.md', '/new_folder/README.md')
30
53
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yandex-disk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yury Korolev