transmission-client 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -6,7 +6,7 @@ The goal is to support all requests described in the Transmission [RPC Specifica
6
6
  You need to have http://gemcutter.org in you gem sources. To add it you can execute either
7
7
 
8
8
  sudo gem install gemcutter
9
- sudo gem tumble`
9
+ sudo gem tumble
10
10
 
11
11
  or
12
12
 
@@ -15,14 +15,35 @@ or
15
15
  To install transmission-client:
16
16
 
17
17
  sudo gem install transmission-client
18
+
19
+ If you want to use EventMachine (optional) you need to install the eventmachine gem and igrigorik's em-http-request:
18
20
 
21
+ sudo gem install eventmachine
22
+ sudo gem install em-http-request
23
+
19
24
  ## Usage
20
25
  Get a list of torrents and print its file names:
21
26
 
22
27
  require 'transmission-client'
23
- t = Transmission::Client.new('192.168.0.2')
28
+ t = Transmission::Client.new('127.0.0.1', 9091)
24
29
  t.torrents.each do |torrent|
25
- puts torrent.name
30
+ puts torrent.name
26
31
  end
27
-
32
+
33
+ To use the EventMachine driven interface:
34
+
35
+ require 'eventmachine'
36
+ require 'transmission-client'
37
+
38
+ EventMachine.run do
39
+ t = Transmission::Client.new
40
+ EM.add_periodic_timer(1) do
41
+ t.torrents do |torrents|
42
+ torrents.each do |tor|
43
+ puts tor.percentDone
44
+ end
45
+ end
46
+ end
47
+ end
48
+
28
49
  RDoc is still to be written, at the meantime have a look at the code to find out which methods are supported.
data/README.rdoc CHANGED
@@ -1,4 +1,4 @@
1
- # tranmission-client: A Transmission RPC Client
1
+ # transmission-client: A Transmission RPC Client
2
2
 
3
3
  The goal is to support all requests described in the Transmission [RPC Specifications](http://trac.transmissionbt.com/browser/trunk/doc/rpc-spec.txt).
4
4
 
@@ -6,7 +6,7 @@ The goal is to support all requests described in the Transmission [RPC Specifica
6
6
  You need to have http://gemcutter.org in you gem sources. To add it you can execute either
7
7
 
8
8
  sudo gem install gemcutter
9
- sudo gem tumble`
9
+ sudo gem tumble
10
10
 
11
11
  or
12
12
 
@@ -15,14 +15,35 @@ or
15
15
  To install transmission-client:
16
16
 
17
17
  sudo gem install transmission-client
18
+
19
+ If you want to use EventMachine (optional) you need to install the eventmachine gem and igrigorik's em-http-request:
18
20
 
21
+ sudo gem install eventmachine
22
+ sudo gem install em-http-request
23
+
19
24
  ## Usage
20
25
  Get a list of torrents and print its file names:
21
26
 
22
27
  require 'transmission-client'
23
- t = Transmission::Client.new('192.168.0.2')
28
+ t = Transmission::Client.new('127.0.0.1', 9091)
24
29
  t.torrents.each do |torrent|
25
- puts torrent.name
30
+ puts torrent.name
26
31
  end
27
-
32
+
33
+ To use the EventMachine driven interface:
34
+
35
+ require 'eventmachine'
36
+ require 'transmission-client'
37
+
38
+ EventMachine.run do
39
+ t = Transmission::Client.new
40
+ EM.add_periodic_timer(1) do
41
+ t.torrents do |torrents|
42
+ torrents.each do |tor|
43
+ puts tor.percentDone
44
+ end
45
+ end
46
+ end
47
+ end
48
+
28
49
  RDoc is still to be written, at the meantime have a look at the code to find out which methods are supported.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.0.1
@@ -1,11 +1,21 @@
1
1
  require 'net/http'
2
2
  require 'singleton'
3
3
  require 'json'
4
-
4
+ require 'rubygems'
5
5
  $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
6
6
 
7
7
  require 'transmission-client/client'
8
- require 'transmission-client/connection'
8
+
9
+ if defined? EM
10
+ begin
11
+ require 'em-http'
12
+ require 'transmission-client/em-connection'
13
+ rescue LoadError
14
+ require 'transmission-client/connection'
15
+ end
16
+ else
17
+ require 'transmission-client/connection'
18
+ end
9
19
  require 'transmission-client/torrent'
10
20
  require 'transmission-client/session'
11
21
 
@@ -4,55 +4,68 @@ module Transmission
4
4
  Connection.instance.init(host, port)
5
5
  end
6
6
 
7
- def start_all
8
- Connection.instance.send('torrent-start')
7
+ def start_all &cb
8
+ Connection.instance.send('torrent-start', &cb)
9
9
  end
10
10
 
11
- def start(id)
12
- Connection.instance.send('torrent-start', {'ids' => id.class == Array ? id : [id]})
11
+ def start(id, &cb)
12
+ Connection.instance.send('torrent-start', {'ids' => id.class == Array ? id : [id]}, &cb)
13
13
  end
14
14
 
15
- def stop(id)
16
- Connection.instance.send('torrent-stop', {'ids' => id.class == Array ? id : [id]})
15
+ def stop(id, &cb)
16
+ Connection.instance.send('torrent-stop', {'ids' => id.class == Array ? id : [id]}, &cb)
17
17
  end
18
18
 
19
- def stop_all
20
- Connection.instance.send('torrent-stop')
19
+ def stop_all &cb
20
+ Connection.instance.send('torrent-stop', &cb)
21
21
  end
22
22
 
23
- def remove(id, delete_data = false)
24
- Connection.instance.send('torrent-remove', {'ids' => id.class == Array ? id : [id], 'delete-local-data' => delete_data })
23
+ def remove(id, delete_data = false, &cb)
24
+ Connection.instance.send('torrent-remove', {'ids' => id.class == Array ? id : [id], 'delete-local-data' => delete_data }, &cb)
25
25
  end
26
26
 
27
- def remove_all(delete_data = false)
28
- Connection.instance.send('torrent-remove', {'delete-local-data' => delete_data })
27
+ def remove_all(delete_data = false, &cb)
28
+ Connection.instance.send('torrent-remove', {'delete-local-data' => delete_data }, &cb)
29
29
  end
30
30
 
31
- def add_torrent(a)
31
+ def add_torrent(a, &cb)
32
32
  if a['filename'].nil? && a['metainfo'].nil?
33
33
  raise "You need to provide either a 'filename' or 'metainfo'."
34
34
  end
35
- Connection.instance.send('torrent-add', a)
35
+ Connection.instance.send('torrent-add', a, &cb)
36
36
  end
37
37
 
38
- def add_torrent_by_file(filename)
39
- add_torrent({'filename' => filename})
38
+ def add_torrent_by_file(filename, &cb)
39
+ add_torrent({'filename' => filename}, &cb)
40
40
  end
41
41
 
42
- def add_torrent_by_data(data)
43
- add_torrent({'metainfo' => data})
42
+ def add_torrent_by_data(data, &cb)
43
+ add_torrent({'metainfo' => data}, &cb)
44
44
  end
45
45
 
46
- def session
47
- Session.new Connection.instance.request('session-get')
46
+ def session &cb
47
+ if cb
48
+ Connection.instance.request('session-get') { |resp| cb.call Session.new resp }
49
+ else
50
+ Session.new Connection.instance.request('session-get')
51
+ end
48
52
  end
49
53
 
50
- def torrents(fields = nil)
54
+ def torrents(fields = nil, &cb)
51
55
  torrs = []
52
- Connection.instance.request('torrent-get', {'fields' => fields ? fields : Transmission::Torrent::ATTRIBUTES})['torrents'].each do |t|
53
- torrs << Torrent.new(t)
54
- end
55
- torrs
56
+ if cb
57
+ Connection.instance.request('torrent-get', {'fields' => fields ? fields : Transmission::Torrent::ATTRIBUTES}) { |resp|
58
+ resp['torrents'].each do |t|
59
+ torrs << Torrent.new(t)
60
+ end
61
+ cb.call torrs
62
+ }
63
+ else
64
+ Connection.instance.request('torrent-get', {'fields' => fields ? fields : Transmission::Torrent::ATTRIBUTES})['torrents'].each do |t|
65
+ torrs << Torrent.new(t)
66
+ end
67
+ torrs
68
+ end
56
69
  end
57
70
  end
58
71
  end
@@ -0,0 +1,51 @@
1
+ module Transmission
2
+ class Connection
3
+ include Singleton
4
+ #include EM::Deferrable
5
+
6
+ def init(host, port)
7
+ @host = host
8
+ @port = port
9
+ uri = URI.parse("http://#{@host}:#{@port}/transmission/rpc")
10
+ #@conn = Net::HTTP.start(uri.host, uri.port)
11
+ @conn = EventMachine::HttpRequest.new(uri)
12
+ @header = {} #{"Accept-Encoding" => "deflate"} deflate is broken somewhere
13
+ end
14
+
15
+ def request(method, attributes={}, &cb)
16
+ req = @conn.post(:body => build_json(method,attributes), :head => @header )
17
+ req.callback {
18
+ if req.response_header.status == 409 #&& @header['x-transmission-session-id'].nil?
19
+ @header['x-transmission-session-id'] = req.response_header['X_TRANSMISSION_SESSION_ID']
20
+ request(method,attributes, &cb)
21
+ elsif req.response_header.status == 200
22
+ resp = JSON.parse(req.response)
23
+ if resp["result"] == 'success'
24
+ cb.call resp['arguments'] if cb
25
+ else
26
+ cb.call resp if cb
27
+ end
28
+ end
29
+ }
30
+ req.errback {
31
+ puts 'errback'
32
+ pp req
33
+ }
34
+ end
35
+
36
+ def send(method, attributes={}, &cb)
37
+ request(method, attributes) do |resp|
38
+ cb.call resp if cb
39
+ end
40
+ end
41
+
42
+ def build_json(method,attributes = {})
43
+ if attributes.length == 0
44
+ {'method' => method}.to_json
45
+ else
46
+ {'method' => method, 'arguments' => attributes }.to_json
47
+ end
48
+ end
49
+
50
+ end
51
+ end
@@ -29,7 +29,6 @@ module Transmission
29
29
  end
30
30
 
31
31
  def method_missing(m, *args, &block)
32
- pp m
33
32
  if ATTRIBUTES.include? m.to_s
34
33
  return @attributes[m.to_s]
35
34
  elsif ADV_ATTRIBUTES.include? m.to_s
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: transmission-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dominik Sander
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-23 00:00:00 +02:00
12
+ date: 2009-10-27 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -43,6 +43,7 @@ files:
43
43
  - lib/transmission-client.rb
44
44
  - lib/transmission-client/client.rb
45
45
  - lib/transmission-client/connection.rb
46
+ - lib/transmission-client/em-connection.rb
46
47
  - lib/transmission-client/session.rb
47
48
  - lib/transmission-client/torrent.rb
48
49
  - test/helper.rb