trans-api 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,354 @@
1
+
2
+ require 'rubygems'
3
+ require 'test/unit'
4
+ require File.expand_path(File.dirname(__FILE__) + "/../../lib/trans-api")
5
+
6
+
7
+
8
+ #
9
+ # Unit test for Transmission RPC+json
10
+ # https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt
11
+ # Revision: 13328 (2012/11/16)
12
+ #
13
+
14
+ class TransTorrentObject < Test::Unit::TestCase
15
+
16
+ CONFIG = { host: "localhost", port: 9091, user: "admin", pass: "admin", path: "/transmission/rpc" }
17
+
18
+ def setup
19
+ Trans::Api::Client.config = CONFIG
20
+ Trans::Api::Torrent.default_fields = [ :id, :status, :name ]
21
+
22
+ # add a testing torrent
23
+ file = File.expand_path(File.dirname(__FILE__) + "/torrents/debian-6.0.6-amd64-CD-1.iso.torrent")
24
+ @torrent = Trans::Api::Torrent.add_file file, paused: true
25
+ sleep 1
26
+ end
27
+
28
+ def teardown
29
+ # remove the testing torrent
30
+ id = @torrent.id
31
+ @torrent.delete! delete_local_data: true
32
+ self.signal_wait_until(lambda{|t| t.nil?}) do
33
+ Trans::Api::Torrent.find id
34
+ end
35
+ end
36
+
37
+
38
+ def test_torrent_list_inspect_methods
39
+ # session object
40
+ torrents = Trans::Api::Torrent.all
41
+
42
+ #NOTE: only :id, :status, :name are preloaded!!
43
+
44
+ unless torrents.empty?
45
+ # loop all available files
46
+ torrents.each do |torrent|
47
+ assert torrent.name.class == String
48
+ assert torrent.status_name.class == Symbol
49
+ assert torrent.activityDate.class == Fixnum
50
+ assert torrent.addedDate.class == Fixnum
51
+ assert torrent.bandwidthPriority.class == Fixnum
52
+ assert torrent.comment.class == String
53
+ assert torrent.corruptEver.class == Fixnum
54
+ assert torrent.creator.class == String
55
+ assert torrent.dateCreated.class == Fixnum
56
+ assert torrent.desiredAvailable.class == Fixnum
57
+ assert torrent.doneDate.class == Fixnum
58
+ assert torrent.downloadDir.class == String
59
+ assert torrent.downloadedEver.class == Fixnum
60
+ assert torrent.downloadLimit.class == Fixnum
61
+ assert torrent.downloadLimited.kind_of? Object
62
+ assert torrent.error.class == Fixnum
63
+ assert torrent.eta.class == Fixnum
64
+ assert torrent.files.class == Array
65
+ assert torrent.fileStats.class == Array
66
+ assert torrent.hashString.class == String
67
+ assert torrent.haveUnchecked.class == Fixnum
68
+ assert torrent.haveValid.class == Fixnum
69
+ assert torrent.honorsSessionLimits.kind_of? Object
70
+ assert torrent.id.class == Fixnum
71
+ assert torrent.isFinished.kind_of? Object
72
+ assert torrent.isPrivate.kind_of? Object
73
+ assert torrent.isStalled.kind_of? Object
74
+ assert torrent.leftUntilDone.class == Fixnum
75
+ assert torrent.magnetLink.class == String
76
+ assert torrent.manualAnnounceTime.class == Fixnum
77
+ assert torrent.maxConnectedPeers.class == Fixnum
78
+ assert torrent.metadataPercentComplete.class == Fixnum
79
+ assert torrent.name.class == String
80
+ # puts torrent.peer_limit.class #?? returns nilclass BROKEN!!
81
+ assert torrent.peers.class == Array
82
+ assert torrent.peersConnected.class == Fixnum
83
+ assert torrent.peersFrom.class == Hash
84
+ assert torrent.peersGettingFromUs.class == Fixnum
85
+ assert torrent.peersSendingToUs.class == Fixnum
86
+ assert torrent.percentDone.class == Fixnum
87
+ assert torrent.pieces.class == String
88
+ assert torrent.pieceCount.class == Fixnum
89
+ assert torrent.pieceSize.class == Fixnum
90
+ assert torrent.priorities.class == Array
91
+ assert torrent.queuePosition.class == Fixnum
92
+ assert torrent.rateDownload.class == Fixnum
93
+ assert torrent.rateUpload.class == Fixnum
94
+ # assert torrent.recheckProgress.class == Fixnum
95
+ assert torrent.secondsDownloading.class == Fixnum
96
+ assert torrent.secondsSeeding.class == Fixnum
97
+ assert torrent.seedIdleLimit.class == Fixnum
98
+ assert torrent.seedIdleMode.class == Fixnum
99
+ assert torrent.seedRatioLimit.class == Fixnum
100
+ assert torrent.seedRatioMode.class == Fixnum
101
+ assert torrent.sizeWhenDone.class == Fixnum
102
+ assert torrent.startDate.class == Fixnum
103
+ assert torrent.status.class == Fixnum
104
+ assert torrent.trackers.class == Array
105
+ assert torrent.trackerStats.class == Array
106
+ assert torrent.totalSize.class == Fixnum
107
+ assert torrent.torrentFile.class == String
108
+ assert torrent.uploadedEver.class == Fixnum
109
+ assert torrent.uploadLimit.class == Fixnum
110
+ assert torrent.uploadLimited.kind_of? Object
111
+ # assert torrent.uploadRatio.class == Float broken!!!
112
+ assert torrent.wanted.class == Array
113
+ assert torrent.webseeds.class == Array
114
+ assert torrent.webseedsSendingToUs.class == Fixnum
115
+ end
116
+ else
117
+ assert false, "no torrent files available!"
118
+ end
119
+
120
+
121
+ end
122
+
123
+ def test_torrent_get_set_methods
124
+ # get all available objects
125
+ torrents = Trans::Api::Torrent.all
126
+
127
+ unless torrents.empty?
128
+ torrents.each do |torrent|
129
+ # get current value
130
+ oldpos = torrent.uploadLimit
131
+ newpos = oldpos + 1
132
+
133
+ # set new value
134
+ torrent.uploadLimit = newpos
135
+ torrent.save!
136
+
137
+ # sync file to its resource
138
+ torrent.reset!
139
+ assert torrent.uploadLimit == newpos, "no new value set #{oldpos} -> #{newpos} for: #{torrent.name}"
140
+
141
+ # save old value
142
+ torrent.uploadLimit = oldpos
143
+ torrent.save!
144
+
145
+ # sync file to its resource
146
+ torrent.reset!
147
+ assert torrent.uploadLimit == oldpos, "no new value set #{oldpos} -> #{newpos} for: #{torrent.name}"
148
+ end
149
+ else
150
+ assert false, "no torrent files available!"
151
+ end
152
+ end
153
+
154
+
155
+ def test_torrent_get_single
156
+ torrent_ref = Trans::Api::Torrent.all.first
157
+
158
+ torrent = Trans::Api::Torrent.find torrent_ref.id
159
+ assert !torrent.nil?, "no torrent with id #{torrent_ref.id}"
160
+ end
161
+
162
+ def test_torrent_start_stop_single
163
+ torrent_ref = Trans::Api::Torrent.all.first
164
+
165
+ torrent = Trans::Api::Torrent.find torrent_ref.id
166
+ assert !torrent.nil?, "no torrent with id #{torrent_ref.id}"
167
+
168
+ #TODO: wait until status indicates start!
169
+
170
+ torrent.start!
171
+ torrent.stop!
172
+ end
173
+
174
+
175
+ def test_torrent_start_stop_multiple
176
+ #TODO: wait until status indicates start!
177
+ torrents = Trans::Api::Torrent.start_all
178
+ torrents = Trans::Api::Torrent.stop_all
179
+ end
180
+
181
+ def test_torrent_add_remove_single
182
+ # add file
183
+ file = File.expand_path(File.dirname(__FILE__) + "/torrents/debian-6.0.6-amd64-CD-2.iso.torrent")
184
+ torrent = Trans::Api::Torrent.add_file file, paused: true
185
+ assert torrent.id
186
+
187
+ # remove the testing torrent
188
+ id = torrent.id
189
+ torrent.delete! delete_local_data: true
190
+ self.signal_wait_until(lambda{|t| t.nil?}) do
191
+ Trans::Api::Torrent.find id
192
+ end
193
+ end
194
+
195
+
196
+ def test_torrent_add_remove_multiple
197
+ file = File.expand_path(File.dirname(__FILE__) + "/torrents/debian-6.0.6-amd64-CD-2.iso.torrent")
198
+ torrent = Trans::Api::Torrent.add_file file, paused: true
199
+
200
+ torrents = Trans::Api::Torrent.all
201
+
202
+ assert torrents.size > 0, "no loaded torrents found"
203
+ torrent = Trans::Api::Torrent.delete_all torrents, delete_local_data: true
204
+
205
+ #TODO: add assert here!!
206
+
207
+ end
208
+
209
+
210
+ def test_torrent_select_files_for_download
211
+ torrent = Trans::Api::Torrent.all.first
212
+
213
+ # mark files, unwant
214
+ torrent.files_objects.each do |file|
215
+ file.unwant
216
+ assert !file.wanted?
217
+ end
218
+
219
+ torrent.save!
220
+ torrent.reset!
221
+
222
+ torrent.files_objects.each do |file|
223
+ assert !file.wanted?
224
+ end
225
+
226
+ end
227
+
228
+ def test_torrent_verify
229
+ torrent = Trans::Api::Torrent.all.first
230
+ torrent.verify!
231
+ assert torrent.recheckProgress > 0
232
+ end
233
+
234
+
235
+ def test_torrent_reannounce
236
+ torrent = Trans::Api::Torrent.all.first
237
+ torrent.reannounce!
238
+
239
+ #TODO: check peers here!
240
+
241
+ end
242
+
243
+
244
+ def test_torrent_set_location
245
+ file = File.expand_path(File.dirname(__FILE__) + "/torrents/tmp/download_tmp/")
246
+ torrent = Trans::Api::Torrent.all.first
247
+ torrent.set_location! file, true
248
+
249
+ torrent.reset!
250
+ assert torrent.downloadDir == file
251
+ end
252
+
253
+
254
+ def test_queue_movement
255
+ # get queueposition as well
256
+ Trans::Api::Torrent.default_fields = [ :id, :status, :name, :queuePosition ]
257
+
258
+ # add test torrents
259
+ torrents = []
260
+ file = File.expand_path(File.dirname(__FILE__) + "/torrents/debian-6.0.6-amd64-CD-2.iso.torrent")
261
+ torrents << Trans::Api::Torrent.add_file(file, paused: true)
262
+ file = File.expand_path(File.dirname(__FILE__) + "/torrents/debian-6.0.6-amd64-CD-3.iso.torrent")
263
+ torrents << Trans::Api::Torrent.add_file(file, paused: true)
264
+ file = File.expand_path(File.dirname(__FILE__) + "/torrents/debian-6.0.6-amd64-CD-4.iso.torrent")
265
+ torrents << Trans::Api::Torrent.add_file(file, paused: true)
266
+ file = File.expand_path(File.dirname(__FILE__) + "/torrents/debian-6.0.6-amd64-CD-5.iso.torrent")
267
+ torrents << Trans::Api::Torrent.add_file(file, paused: true)
268
+ file = File.expand_path(File.dirname(__FILE__) + "/torrents/debian-6.0.6-amd64-CD-6.iso.torrent")
269
+ torrents << Trans::Api::Torrent.add_file(file, paused: true)
270
+ file = File.expand_path(File.dirname(__FILE__) + "/torrents/debian-6.0.6-amd64-CD-7.iso.torrent")
271
+ torrents << Trans::Api::Torrent.add_file(file, paused: true)
272
+
273
+ # collect first and last
274
+ all = Trans::Api::Torrent.all
275
+ torrent_first = all.first
276
+ torrent_last = all.last
277
+
278
+ # move to bottom
279
+ torrent_first.queue_bottom!
280
+ torrent_first.reset!
281
+ assert torrent_first.queuePosition == all.size - 1
282
+
283
+ # move to top
284
+ torrent_last.queue_top!
285
+ torrent_last.reset!
286
+ assert torrent_last.queuePosition == 0
287
+
288
+ ref = Trans::Api::Torrent.find_by_field_value :queuePosition, 0
289
+ assert ref.class == Trans::Api::Torrent
290
+
291
+
292
+ # move down the queue list
293
+ all = Trans::Api::Torrent.all
294
+ i = 0
295
+ while i < all.size
296
+ all = Trans::Api::Torrent.all
297
+ all.each do |t|
298
+ if t.queuePosition == i
299
+ assert ref.name == t.name
300
+ t.queue_down!
301
+ end
302
+ end
303
+ i += 1
304
+ end
305
+
306
+ # move up the queue list
307
+ while i >= 0
308
+ all = Trans::Api::Torrent.all
309
+ all.each do |t|
310
+ if t.queuePosition == i
311
+ assert ref.name == t.name
312
+ t.queue_up!
313
+ end
314
+ end
315
+ i -= 1
316
+ end
317
+
318
+ sleep(1) # don't crash the rpc daemon!
319
+ # cleanup
320
+ Trans::Api::Torrent.delete_all torrents, delete_local_data: true
321
+
322
+ end
323
+
324
+ def test_torrent_waitfor_status
325
+
326
+ assert @torrent.status_name == :stopped || @torrent.status_name == :checkFiles
327
+
328
+ # mark start
329
+ @torrent.waitfor( lambda{|t| t.status_name != :stopped} ).start!
330
+ @torrent.reset!
331
+ assert @torrent.status_name != :stopped
332
+
333
+ # mark stop
334
+ @torrent.waitfor( lambda{|t| t.status_name == :stopped} ).stop!
335
+ @torrent.reset!
336
+ assert @torrent.status_name == :stopped
337
+
338
+ end
339
+
340
+ protected
341
+
342
+
343
+ # UTILS, probe block as long as pr callback returns false
344
+
345
+ def signal_wait_until(pr, &block)
346
+ #NOTE: busy waiting!!!
347
+ while true do
348
+ torrent = yield
349
+ break if pr.call torrent
350
+ end
351
+ end
352
+
353
+
354
+ end
data/trans-api.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'trans-api/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "trans-api"
8
+ gem.version = Trans::Api::VERSION
9
+ gem.authors = ["Dennis Blommesteijn"]
10
+ gem.email = ["dennis@blommesteijn.com"]
11
+ gem.description = %q{transmission torrent client rpc interface library}
12
+ gem.summary = %q{transmission torrent client rpc interface library}
13
+ gem.homepage = ""
14
+
15
+ # dependencies
16
+ gem.add_dependency 'nokogiri'
17
+ gem.add_dependency 'json'
18
+ gem.add_development_dependency "test-unit"
19
+
20
+ gem.files = `git ls-files`.split($/)
21
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
22
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
23
+ gem.require_paths = ["lib"]
24
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trans-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dennis Blommesteijn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: transmission torrent client rpc interface library
56
+ email:
57
+ - dennis@blommesteijn.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - examples/basic_setup.rb
69
+ - examples/torrents/debian-6.0.6-amd64-CD-10.iso.torrent
70
+ - lib/trans-api.rb
71
+ - lib/trans-api/client.rb
72
+ - lib/trans-api/connect.rb
73
+ - lib/trans-api/file.rb
74
+ - lib/trans-api/session.rb
75
+ - lib/trans-api/torrent.rb
76
+ - lib/trans-api/version.rb
77
+ - test/test_helper.rb
78
+ - test/unit/torrents/debian-6.0.6-amd64-CD-1.iso.torrent
79
+ - test/unit/torrents/debian-6.0.6-amd64-CD-2.iso.torrent
80
+ - test/unit/torrents/debian-6.0.6-amd64-CD-3.iso.torrent
81
+ - test/unit/torrents/debian-6.0.6-amd64-CD-4.iso.torrent
82
+ - test/unit/torrents/debian-6.0.6-amd64-CD-5.iso.torrent
83
+ - test/unit/torrents/debian-6.0.6-amd64-CD-6.iso.torrent
84
+ - test/unit/torrents/debian-6.0.6-amd64-CD-7.iso.torrent
85
+ - test/unit/trans_connect.rb
86
+ - test/unit/trans_session_object.rb
87
+ - test/unit/trans_torrent_object.rb
88
+ - trans-api.gemspec
89
+ homepage: ''
90
+ licenses: []
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.0.0
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: transmission torrent client rpc interface library
112
+ test_files:
113
+ - test/test_helper.rb
114
+ - test/unit/torrents/debian-6.0.6-amd64-CD-1.iso.torrent
115
+ - test/unit/torrents/debian-6.0.6-amd64-CD-2.iso.torrent
116
+ - test/unit/torrents/debian-6.0.6-amd64-CD-3.iso.torrent
117
+ - test/unit/torrents/debian-6.0.6-amd64-CD-4.iso.torrent
118
+ - test/unit/torrents/debian-6.0.6-amd64-CD-5.iso.torrent
119
+ - test/unit/torrents/debian-6.0.6-amd64-CD-6.iso.torrent
120
+ - test/unit/torrents/debian-6.0.6-amd64-CD-7.iso.torrent
121
+ - test/unit/trans_connect.rb
122
+ - test/unit/trans_session_object.rb
123
+ - test/unit/trans_torrent_object.rb
124
+ has_rdoc: