BtSync 0.5.3 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.rubocop.yml +3 -0
- data/.travis.yml +2 -2
- data/README.md +1 -0
- data/btsync.gemspec +1 -0
- data/lib/btsync.rb +90 -50
- data/lib/btsync/communicator.rb +36 -15
- data/lib/btsync/directory.rb +106 -51
- data/lib/btsync/version.rb +2 -1
- data/spec/BtSync/bt_sync_spec.rb +85 -59
- data/spec/BtSync/directory_spec.rb +77 -30
- data/spec/fixtures/cassettes/Remove-Default-Host.yml +3 -3
- data/spec/fixtures/cassettes/Setup-BtSync-Directory.yml +48 -46
- data/spec/fixtures/cassettes/Setup-BtSync.yml +287 -32
- data/spec/fixtures/cassettes/Setup-Directory-Settings.yml +6 -6
- data/spec/fixtures/cassettes/add-folder-list.yml +33 -30
- data/spec/fixtures/cassettes/add-folder.yml +67 -10
- data/spec/fixtures/cassettes/add-known-host.yml +8 -9
- data/spec/fixtures/cassettes/change_download_limit.yml +9 -9
- data/spec/fixtures/cassettes/change_listening_port.yml +9 -9
- data/spec/fixtures/cassettes/change_name.yml +9 -9
- data/spec/fixtures/cassettes/change_secret_custom.yml +59 -0
- data/spec/fixtures/cassettes/change_secret_empty.yml +59 -0
- data/spec/fixtures/cassettes/change_secret_error.yml +30 -0
- data/spec/fixtures/cassettes/change_upload_limit.yml +9 -9
- data/spec/fixtures/cassettes/check_for_new_version.yml +30 -0
- data/spec/fixtures/cassettes/check_is_writable.yml +32 -0
- data/spec/fixtures/cassettes/check_speeds.yml +33 -0
- data/spec/fixtures/cassettes/delete-directory.yml +379 -0
- data/spec/fixtures/cassettes/get-folders.yml +19 -76
- data/spec/fixtures/cassettes/get-os-type.yml +3 -3
- data/spec/fixtures/cassettes/get-peers.yml +8 -7
- data/spec/fixtures/cassettes/get-preferences.yml +24 -24
- data/spec/fixtures/cassettes/get-settings.yml +3 -3
- data/spec/fixtures/cassettes/get_dir.yml +93 -0
- data/spec/fixtures/cassettes/get_read_only_secret.yml +32 -0
- data/spec/fixtures/cassettes/{get-version.yml → get_version.yml} +4 -4
- data/spec/fixtures/cassettes/remove-folder-list.yml +19 -17
- data/spec/fixtures/cassettes/remove-folder.yml +36 -33
- data/spec/fixtures/cassettes/remove-known-host.yml +35 -8
- data/spec/fixtures/cassettes/reset_device_name.yml +9 -9
- data/spec/fixtures/cassettes/reset_download_limit.yml +9 -9
- data/spec/fixtures/cassettes/reset_listening_port.yml +9 -9
- data/spec/fixtures/cassettes/reset_secret.yml +31 -0
- data/spec/fixtures/cassettes/reset_upload_limit.yml +9 -9
- data/spec/fixtures/cassettes/set-preferences-delete.yml +11 -11
- data/spec/fixtures/cassettes/set-preferences-dht.yml +11 -11
- data/spec/fixtures/cassettes/set-preferences-hosts.yml +11 -11
- data/spec/fixtures/cassettes/set-preferences-lan.yml +11 -11
- data/spec/fixtures/cassettes/set-preferences-relay.yml +11 -11
- data/spec/fixtures/cassettes/set-preferences-tracker.yml +11 -11
- data/spec/fixtures/cassettes/view-folders.yml +3 -3
- data/spec/spec_helper.rb +7 -8
- metadata +39 -4
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
Yzk2ZWQ3OTY5NDA5NzVhMDgxOWY3NWU3NDVjNmMwZDE5OWE4NzRiOA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
OGUzNjQyNmM5OGJlMDc0N2NhNjQ3MmY2YzI4OTNmN2M1YTE5YzQ2Nw==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MWNhNzgzZTJlYjZkYmMwMjEwZjk1OWExZjg0NTc1MDE0Y2MwYjM4ZjJkN2Mx
|
10
|
+
NDVmNjE5ZWUxMTEyZWQ3MTk2YjkyNWYyNGYxYTI4YWNhZGQ2NzRkMzU1ZmFj
|
11
|
+
YzRhYWI1YzNiYzFhMmUwMDBiZjJjZWRiZmI3YzAwOWFlZGQyZTY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NTA5NTdmNDg0M2QwYTZjNTYyNjMxMWMzNmJkZGY0MzNlMDFlZGU1N2M4ZWEz
|
14
|
+
ZDM4ZjRhNzA0ZTI4Y2VmM2M3MDY5ODA1YWZiOTU4MGZkMDA3YzU1NmM2YjFj
|
15
|
+
ZjUxZDllZTE0ZGJlZTcwNDNjNDcxYWQ5ODAwY2E3YmFiODcxMDg=
|
data/.rubocop.yml
ADDED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/ChrisMacNaughton/BtSync.png?branch=master)](https://travis-ci.org/ChrisMacNaughton/BtSync)
|
4
4
|
[![Code Climate](https://codeclimate.com/github/ChrisMacNaughton/BtSync.png)](https://codeclimate.com/github/ChrisMacNaughton/BtSync)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/ChrisMacNaughton/BtSync/badge.png)](https://coveralls.io/r/ChrisMacNaughton/BtSync)
|
5
6
|
|
6
7
|
BtSync is a library to help you interact with Bittorrent Sync in Ruby
|
7
8
|
|
data/btsync.gemspec
CHANGED
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.add_development_dependency "rspec"
|
24
24
|
spec.add_development_dependency "webmock"
|
25
25
|
spec.add_development_dependency "vcr"
|
26
|
+
spec.add_development_dependency "coveralls"
|
26
27
|
|
27
28
|
spec.add_runtime_dependency "httparty"
|
28
29
|
spec.add_runtime_dependency "json"
|
data/lib/btsync.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require 'httparty'
|
2
3
|
lib = File.expand_path('../btsync', __FILE__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
@@ -8,80 +9,96 @@ require 'directory'
|
|
8
9
|
class BtSync
|
9
10
|
include BtCommunicator
|
10
11
|
include HTTParty
|
11
|
-
default_params :
|
12
|
-
def initialize
|
13
|
-
@opts =
|
14
|
-
:protocol => "http",
|
15
|
-
:uri => "localhost",
|
16
|
-
:port => "8888",
|
17
|
-
:user => "admin",
|
18
|
-
:password => "AdminPassword"}
|
19
|
-
@opts.merge!(options.symbolize)
|
20
|
-
@opts[:uri].gsub!(/^(https?:\/\/){1,}/i, '')
|
21
|
-
@port = @opts[:port]
|
22
|
-
@user = @opts[:user]
|
23
|
-
@pass = @opts[:password]
|
12
|
+
default_params output: 'json'
|
13
|
+
def initialize(options = {})
|
14
|
+
@opts = setup_opts(options)
|
24
15
|
@errors = []
|
25
16
|
@token_cache = 0
|
26
17
|
end
|
18
|
+
|
27
19
|
def errors
|
28
20
|
errors = @errors
|
29
21
|
@errors = []
|
30
22
|
errors
|
31
23
|
end
|
24
|
+
|
32
25
|
def folders
|
33
|
-
f = get_folder_list[
|
26
|
+
f = get_folder_list['folders']
|
34
27
|
folders = []
|
35
28
|
f.each do |folder|
|
36
|
-
folders << Directory.new(folder[
|
29
|
+
folders << Directory.new(folder['name'], folder['secret'], self)
|
37
30
|
end
|
38
31
|
folders
|
39
32
|
end
|
33
|
+
|
40
34
|
def upload_limit
|
41
|
-
get_settings[
|
35
|
+
get_settings['ulrate'].to_i
|
42
36
|
end
|
37
|
+
|
43
38
|
def download_limit
|
44
|
-
get_settings[
|
39
|
+
get_settings['dlrate'].to_i
|
45
40
|
end
|
41
|
+
|
46
42
|
def device_name
|
47
|
-
get_settings[
|
43
|
+
get_settings['devicename']
|
48
44
|
end
|
45
|
+
|
49
46
|
def listening_port
|
50
|
-
get_settings[
|
47
|
+
get_settings['listeningport'].to_i
|
51
48
|
end
|
52
|
-
|
53
|
-
|
49
|
+
|
50
|
+
def upload_limit=(opt)
|
51
|
+
change_setting 'ulrate', opt
|
54
52
|
end
|
55
|
-
|
56
|
-
|
53
|
+
|
54
|
+
def download_limit=(opt)
|
55
|
+
change_setting 'dlrate', opt
|
57
56
|
end
|
58
|
-
|
59
|
-
|
57
|
+
|
58
|
+
def device_name=(opt)
|
59
|
+
change_setting 'devicename', opt
|
60
60
|
end
|
61
|
-
|
62
|
-
|
61
|
+
|
62
|
+
def listening_port=(opt)
|
63
|
+
change_setting 'listeningport', opt
|
63
64
|
end
|
64
|
-
def change_setting type, opt
|
65
|
-
options = get_settings.merge!({type => opt})
|
66
65
|
|
67
|
-
|
66
|
+
def change_setting(type, opt)
|
67
|
+
options = get_settings.merge!({ type => opt })
|
68
|
+
|
69
|
+
get(path('setsettings'), query: options)
|
68
70
|
end
|
71
|
+
|
69
72
|
def get_speed
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
73
|
+
{
|
74
|
+
up: up,
|
75
|
+
down: down
|
76
|
+
}
|
77
|
+
end
|
78
|
+
|
79
|
+
def up
|
80
|
+
l = get_folder_list['speed'].split(', ')[0].split(' ')
|
81
|
+
{speed: l[0].to_f, metric: l[1]}
|
82
|
+
end
|
83
|
+
|
84
|
+
def down
|
85
|
+
l = get_folder_list['speed'].split(', ')[1].split(' ')
|
86
|
+
{speed: l[0].to_f, metric: l[1]}
|
74
87
|
end
|
75
|
-
|
88
|
+
|
89
|
+
def remove_folder(folder_name, my_secret = nil)
|
76
90
|
my_secret ||= secret(folder_name)
|
77
|
-
|
91
|
+
query = { name: folder_name, secret: my_secret }
|
92
|
+
get(path('removefolder'), query: query)
|
78
93
|
true
|
79
94
|
end
|
80
|
-
|
95
|
+
|
96
|
+
def add_folder(folder_name, my_secret = nil)
|
81
97
|
my_secret ||= generate_secret
|
82
|
-
|
83
|
-
|
84
|
-
|
98
|
+
query = { name: folder_name, secret: my_secret }
|
99
|
+
res = get(path('addsyncfolder'), query: query)
|
100
|
+
unless res['error'] == 0
|
101
|
+
@errors << res['message']
|
85
102
|
return false
|
86
103
|
end
|
87
104
|
Directory.new(folder_name, my_secret, self)
|
@@ -89,32 +106,55 @@ class BtSync
|
|
89
106
|
|
90
107
|
def get_settings
|
91
108
|
res = get(path('getsettings'))
|
92
|
-
res.parsed_response[
|
109
|
+
res.parsed_response['settings']
|
93
110
|
end
|
111
|
+
|
94
112
|
def get_os_type
|
95
113
|
res = get(path('getostype'))
|
96
|
-
res.parsed_response[
|
114
|
+
res.parsed_response['os']
|
97
115
|
end
|
116
|
+
|
98
117
|
def get_version
|
99
118
|
res = get(path('getversion'))
|
100
|
-
res.parsed_response[
|
119
|
+
res.parsed_response['version']
|
101
120
|
end
|
121
|
+
|
102
122
|
def check_new_version
|
103
123
|
res = get(path('checknewversion'))
|
104
|
-
res.parsed_response[
|
124
|
+
res.parsed_response['version']
|
105
125
|
end
|
106
126
|
|
107
|
-
def get_dir
|
108
|
-
res = get(path('getdir'), :
|
109
|
-
res.parsed_response[
|
127
|
+
def get_dir(with_dir = '/')
|
128
|
+
res = get(path('getdir'), query: { 'dir' => with_dir })
|
129
|
+
res.parsed_response['folders'].map { |f| f.gsub!('//', '/') }
|
110
130
|
end
|
111
131
|
|
112
|
-
def secret
|
113
|
-
f = folders.select{|folder| folder.name == with_dir}.first
|
132
|
+
def secret(with_dir)
|
133
|
+
f = folders.select { |folder| folder.name == with_dir }.first
|
114
134
|
f.secret
|
115
135
|
end
|
136
|
+
|
116
137
|
private
|
117
138
|
|
139
|
+
def setup_opts(opts)
|
140
|
+
opt = defaults.merge!(opts.symbolize)
|
141
|
+
opt[:uri].gsub!(%r(^(https?://){1,})i, '')
|
142
|
+
@port = opt[:port]
|
143
|
+
@user = opt[:user]
|
144
|
+
@pass = opt[:password]
|
145
|
+
opt
|
146
|
+
end
|
147
|
+
|
148
|
+
def defaults
|
149
|
+
{
|
150
|
+
protocol: 'http',
|
151
|
+
uri: 'localhost',
|
152
|
+
port: '8888',
|
153
|
+
user: 'admin',
|
154
|
+
password: 'AdminPassword'
|
155
|
+
}
|
156
|
+
end
|
157
|
+
|
118
158
|
def get_folder_list
|
119
159
|
res = get(path('getsyncfolders'))
|
120
160
|
@folder_list = res.parsed_response
|
@@ -123,7 +163,7 @@ end
|
|
123
163
|
class Hash
|
124
164
|
def symbolize
|
125
165
|
r = {}
|
126
|
-
self.each do |k,v|
|
166
|
+
self.each do |k, v|
|
127
167
|
if k.is_a? String
|
128
168
|
r[k.to_symbol] = v
|
129
169
|
else
|
data/lib/btsync/communicator.rb
CHANGED
@@ -1,68 +1,89 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
module BtCommunicator
|
2
3
|
include HTTParty
|
4
|
+
attr_reader :opts
|
3
5
|
disable_rails_query_string_format
|
4
|
-
default_params :
|
6
|
+
default_params output: 'json'
|
5
7
|
def generate_secret
|
6
8
|
res = get(path('generatesecret'))
|
7
|
-
res.parsed_response[
|
9
|
+
res.parsed_response['secret']
|
8
10
|
end
|
11
|
+
|
9
12
|
def port
|
10
13
|
@opts[:port]
|
11
14
|
end
|
15
|
+
|
12
16
|
def uri
|
13
17
|
@opts[:uri]
|
14
18
|
end
|
19
|
+
|
15
20
|
def protocol
|
16
21
|
@opts[:protocol]
|
17
22
|
end
|
23
|
+
|
18
24
|
def user
|
19
25
|
@opts[:user]
|
20
26
|
end
|
27
|
+
|
21
28
|
def password
|
22
29
|
@opts[:password]
|
23
30
|
end
|
24
|
-
|
25
|
-
@opts
|
26
|
-
end
|
31
|
+
|
27
32
|
def auth
|
28
|
-
{:
|
33
|
+
{ username: user, password: password }
|
29
34
|
end
|
30
|
-
|
35
|
+
|
36
|
+
def token(force = false)
|
31
37
|
@token_cache ||= 0
|
32
|
-
time = DateTime.now.strftime(
|
38
|
+
time = DateTime.now.strftime('%s').to_i
|
33
39
|
if time > @token_cache + 600 || force
|
34
|
-
@token = request_token(force).
|
40
|
+
@token = request_token(force).
|
41
|
+
gsub('</div></html>', '').
|
42
|
+
gsub("<html><div id='token' style='display:none;'>", '')
|
35
43
|
@token_cache = time
|
36
44
|
end
|
37
45
|
@cookies = nil if force
|
38
46
|
@token
|
39
47
|
end
|
48
|
+
|
40
49
|
def cookies
|
41
|
-
@cookies ||= request_token.headers[
|
50
|
+
@cookies ||= request_token.headers['set-cookie'].split('; ')[0]
|
42
51
|
end
|
52
|
+
|
43
53
|
def root_url
|
44
54
|
"#{starter}#{uri}:#{port}/"
|
45
55
|
end
|
46
56
|
|
47
|
-
def get
|
48
|
-
opts = {
|
57
|
+
def get(path, opts = {})
|
58
|
+
opts = {
|
59
|
+
headers: {
|
60
|
+
'Cookie' => cookies
|
61
|
+
}, query: {
|
62
|
+
|
63
|
+
}, basic_auth: auth }.merge(opts)
|
49
64
|
self.class.get(path, opts)
|
50
65
|
end
|
51
66
|
|
52
|
-
def request_token
|
67
|
+
def request_token(force = false)
|
53
68
|
@last_request ||= 0
|
54
69
|
t = DateTime.now.strftime('%s').to_i
|
55
70
|
if @request_token.nil? || force || (@last_request + 600) < t
|
56
71
|
@last_request = t
|
57
|
-
@request_token = self.class.get(
|
72
|
+
@request_token = self.class.get(token_path, basic_auth: auth)
|
58
73
|
else
|
59
74
|
@request_token
|
60
75
|
end
|
61
76
|
end
|
77
|
+
|
78
|
+
def token_path
|
79
|
+
"#{root_url}gui/token.html"
|
80
|
+
end
|
81
|
+
|
62
82
|
def starter
|
63
83
|
"#{protocol}://"
|
64
84
|
end
|
65
|
-
|
85
|
+
|
86
|
+
def path(action_name)
|
66
87
|
"#{root_url}gui/?token=#{token}&action=#{action_name}"
|
67
88
|
end
|
68
89
|
end
|
data/lib/btsync/directory.rb
CHANGED
@@ -1,15 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
class BtSync
|
2
3
|
class Directory
|
3
4
|
include HTTParty
|
4
5
|
include BtCommunicator
|
5
|
-
default_params :
|
6
|
-
attr_reader :secret, :name
|
6
|
+
default_params output: 'json'
|
7
|
+
attr_reader :secret, :name, :errors
|
7
8
|
|
8
|
-
def initialize
|
9
|
+
def initialize(name, secret, btsync)
|
9
10
|
@name = name
|
10
11
|
@secret = secret
|
11
12
|
|
12
|
-
@opts = btsync.
|
13
|
+
@opts = btsync.opts
|
13
14
|
|
14
15
|
find_or_create
|
15
16
|
|
@@ -17,105 +18,149 @@ class BtSync
|
|
17
18
|
end
|
18
19
|
|
19
20
|
def destroy
|
20
|
-
get(path('removefolder'), :
|
21
|
-
self.instance_variables.each{|v| v = nil}
|
21
|
+
get(path('removefolder'), query: { name: name, secret: secret })
|
22
|
+
self.instance_variables.each { |v| v = nil }
|
22
23
|
end
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
|
25
|
+
def update_secret(new_secret = generate_secret)
|
26
|
+
query = secret_params(new_secret)
|
27
|
+
res = get(path('updatesecret'), query: query)
|
28
|
+
p = res.parsed_response
|
29
|
+
if p != {} && p != '\r\ninvalid request'
|
27
30
|
@secret = new_secret
|
28
31
|
true
|
29
32
|
else
|
30
|
-
|
33
|
+
if p == {}
|
34
|
+
@errors << "Invalid Secret"
|
35
|
+
else
|
36
|
+
@errors << res.parsed_response
|
37
|
+
end
|
31
38
|
false
|
32
39
|
end
|
33
40
|
end
|
41
|
+
|
34
42
|
def folders
|
35
|
-
res = get(path('getdir'), :
|
36
|
-
res.parsed_response[
|
43
|
+
res = get(path('getdir'), query: { dir: @name })
|
44
|
+
res.parsed_response['folders']
|
37
45
|
end
|
46
|
+
|
38
47
|
def peers
|
39
|
-
res = get(path('getsyncfolders')
|
40
|
-
|
41
|
-
|
48
|
+
res = get(path('getsyncfolders'))
|
49
|
+
r = res.parsed_response['folders'].select { |f| f['name'] == name }.first
|
50
|
+
r['peers']
|
42
51
|
end
|
52
|
+
|
43
53
|
def known_hosts
|
44
|
-
|
54
|
+
query = { name: name, secret: secret }
|
55
|
+
res = get(path('getknownhosts'), query: query)
|
45
56
|
hosts = {}
|
46
|
-
res[
|
57
|
+
res['hosts'].map { |h| hosts[h['index']] = h['peer'] }
|
47
58
|
hosts
|
48
59
|
end
|
49
|
-
|
50
|
-
|
60
|
+
|
61
|
+
def add_host(host, port)
|
62
|
+
query = { name: name, secret: secret, addr: host, port: port }
|
63
|
+
get(path('addknownhosts'), query: query)
|
51
64
|
true
|
52
65
|
end
|
53
|
-
|
54
|
-
|
66
|
+
|
67
|
+
def remove_host(index)
|
68
|
+
query = { name: name, secret: secret, index: index }
|
69
|
+
res = get(path('removeknownhosts'), query: query)
|
55
70
|
if res.parsed_response != {}
|
56
71
|
res.parsed_response
|
57
72
|
else
|
58
73
|
true
|
59
74
|
end
|
60
75
|
end
|
61
|
-
|
62
|
-
|
76
|
+
|
77
|
+
def remove_host_by_ip(ip, port = nil)
|
78
|
+
known_hosts.each do |id, host|
|
79
|
+
host = host.split(':')
|
80
|
+
if ip == host[0]
|
81
|
+
next if port != host[1] unless port.nil?
|
82
|
+
|
83
|
+
remove_host(id)
|
84
|
+
end
|
85
|
+
end
|
63
86
|
end
|
87
|
+
|
64
88
|
def use_tracker=(opt)
|
65
89
|
set_pref('usetracker', opt)
|
66
90
|
end
|
91
|
+
|
67
92
|
def use_tracker?
|
68
|
-
bool(preferences[
|
93
|
+
bool(preferences['usetracker'])
|
69
94
|
end
|
95
|
+
|
70
96
|
def use_hosts=(opt)
|
71
97
|
set_pref('usehosts', opt)
|
72
98
|
end
|
99
|
+
|
73
100
|
def use_hosts?
|
74
|
-
bool(preferences[
|
101
|
+
bool(preferences['usehosts'])
|
75
102
|
end
|
103
|
+
|
76
104
|
def search_lan=(opt)
|
77
105
|
set_pref('searchlan', opt)
|
78
106
|
end
|
107
|
+
|
79
108
|
def search_lan?
|
80
|
-
bool(preferences[
|
109
|
+
bool(preferences['searchlan'])
|
81
110
|
end
|
111
|
+
|
82
112
|
def search_dht=(opt)
|
83
113
|
set_pref('searchdht', opt)
|
84
114
|
end
|
115
|
+
|
85
116
|
def search_dht?
|
86
|
-
bool(preferences[
|
117
|
+
bool(preferences['searchdht'])
|
87
118
|
end
|
119
|
+
|
88
120
|
def use_relay=(opt)
|
89
121
|
set_pref('relay', opt)
|
90
122
|
end
|
123
|
+
|
91
124
|
def use_relay?
|
92
|
-
bool(preferences[
|
125
|
+
bool(preferences['relay'])
|
93
126
|
end
|
127
|
+
|
94
128
|
def delete_to_trash=(opt)
|
95
129
|
set_pref('deletetotrash', opt)
|
96
130
|
end
|
131
|
+
|
97
132
|
def delete_to_trash?
|
98
|
-
bool(preferences[
|
133
|
+
bool(preferences['deletetotrash'])
|
99
134
|
end
|
100
|
-
|
101
|
-
|
135
|
+
|
136
|
+
def is_writable?
|
137
|
+
bool(preferences['iswritable'])
|
102
138
|
end
|
139
|
+
|
103
140
|
def preferences
|
104
|
-
res = get(path('getfolderpref'), :
|
105
|
-
res.parsed_response[
|
141
|
+
res = get(path('getfolderpref'), query: { name: @name, secret: @secret })
|
142
|
+
res.parsed_response['folderpref']
|
106
143
|
end
|
144
|
+
|
107
145
|
def read_only_secret
|
108
|
-
preferences[
|
146
|
+
preferences['readonlysecret']
|
109
147
|
end
|
148
|
+
|
110
149
|
private
|
111
|
-
|
112
|
-
|
150
|
+
|
151
|
+
def set_pref(pref, opt)
|
152
|
+
get(path('setfolderpref'), query: make_opts(pref, opt))
|
113
153
|
true
|
114
154
|
end
|
155
|
+
|
115
156
|
def default_settings
|
116
|
-
|
117
|
-
|
118
|
-
|
157
|
+
get(path('setfolderpref'), query: defaults)
|
158
|
+
end
|
159
|
+
|
160
|
+
def defaults
|
161
|
+
{
|
162
|
+
'name' => @name,
|
163
|
+
'secret' => @secret,
|
119
164
|
'relay' => 1,
|
120
165
|
'usetracker' => 1,
|
121
166
|
'searchlan' => 1,
|
@@ -123,37 +168,47 @@ class BtSync
|
|
123
168
|
'deletetotrash' => 1,
|
124
169
|
'usehosts' => 1
|
125
170
|
}
|
126
|
-
get(path('setfolderpref'), :query => opts )
|
127
171
|
end
|
128
|
-
|
172
|
+
|
173
|
+
def make_opts(name, opt)
|
129
174
|
opts = preferences
|
130
175
|
opts[name] = bool_to_i(opt)
|
131
176
|
opts.delete('readonlysecret')
|
132
|
-
opts.merge!({:
|
177
|
+
opts.merge!({ name: @name, secret: @secret })
|
133
178
|
end
|
134
|
-
|
179
|
+
|
180
|
+
def secret_params(s)
|
181
|
+
{
|
182
|
+
name: @name,
|
183
|
+
secret: @secret,
|
184
|
+
newsecret: s
|
185
|
+
}
|
186
|
+
end
|
187
|
+
|
188
|
+
def bool(i)
|
189
|
+
i = i.to_i
|
135
190
|
if i == 0
|
136
191
|
false
|
137
|
-
elsif i == 1
|
138
|
-
true
|
139
192
|
else
|
140
|
-
|
193
|
+
true
|
141
194
|
end
|
142
195
|
end
|
143
|
-
|
196
|
+
|
197
|
+
def bool_to_i(bool)
|
144
198
|
if bool
|
145
199
|
1
|
146
200
|
else
|
147
201
|
0
|
148
202
|
end
|
149
203
|
end
|
204
|
+
|
150
205
|
def find_or_create
|
151
206
|
res = get(path('getsyncfolders'))
|
152
|
-
folder_list = res.parsed_response[
|
153
|
-
if folder_list.map{|f| f[
|
207
|
+
folder_list = res.parsed_response['folders']
|
208
|
+
if folder_list.map { |f| f['name'] }.include? name
|
154
209
|
true
|
155
210
|
else
|
156
|
-
res = get(path('addsyncfolder'), :
|
211
|
+
res = get(path('addsyncfolder'), query: { name: name, secret: secret })
|
157
212
|
end
|
158
213
|
end
|
159
214
|
end
|