Class: Transmission::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/transmission-client/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(host = 'localhost', port = 9091, username = nil, password = nil) ⇒ Client

Returns a new instance of Client.



9
10
11
12
# File 'lib/transmission-client/client.rb', line 9

def initialize(host='localhost',port=9091, username = nil, password = nil)
  Connection.init(host, port, username, password)
  @torrents = nil
end

Instance Method Details

#add_torrent(a) ⇒ Object



38
39
40
41
42
43
# File 'lib/transmission-client/client.rb', line 38

def add_torrent(a)
  if a['filename'].nil? && a['metainfo'].nil?
    raise "You need to provide either a 'filename' or 'metainfo'."
  end
  Connection.send('torrent-add', a)
end

#add_torrent_by_data(data) ⇒ Object



49
50
51
# File 'lib/transmission-client/client.rb', line 49

def add_torrent_by_data(data)
  add_torrent({'metainfo' => data})
end

#add_torrent_by_file(filename) ⇒ Object



45
46
47
# File 'lib/transmission-client/client.rb', line 45

def add_torrent_by_file(filename)
  add_torrent({'filename' => filename})
end

#on_download_finished(&blk) ⇒ Object



3
# File 'lib/transmission-client/client.rb', line 3

def on_download_finished(&blk); @on_download_finished = blk; callback_initialized; end

#on_torrent_added(&blk) ⇒ Object



4
# File 'lib/transmission-client/client.rb', line 4

def on_torrent_added(&blk); @on_torrent_added = blk; callback_initialized; end

#on_torrent_removed(&blk) ⇒ Object



7
# File 'lib/transmission-client/client.rb', line 7

def on_torrent_removed(&blk); @on_torrent_removed = blk; callback_initialized; end

#on_torrent_started(&blk) ⇒ Object



6
# File 'lib/transmission-client/client.rb', line 6

def on_torrent_started(&blk); @on_torrent_started = blk; callback_initialized; end

#on_torrent_stopped(&blk) ⇒ Object



5
# File 'lib/transmission-client/client.rb', line 5

def on_torrent_stopped(&blk); @on_torrent_stopped = blk; callback_initialized; end

#remove(id, delete_data = false) ⇒ Object



30
31
32
# File 'lib/transmission-client/client.rb', line 30

def remove(id, delete_data = false)
  Connection.send('torrent-remove', {'ids' => id.class == Array ? id : [id], 'delete-local-data' => delete_data })
end

#remove_all(delete_data = false) ⇒ Object



34
35
36
# File 'lib/transmission-client/client.rb', line 34

def remove_all(delete_data = false)
  Connection.send('torrent-remove', {'delete-local-data' => delete_data })
end

#sessionObject



53
54
55
# File 'lib/transmission-client/client.rb', line 53

def session
  Connection.request('session-get') { |resp| yield Session.new resp }
end

#start(id) ⇒ Object



18
19
20
# File 'lib/transmission-client/client.rb', line 18

def start(id)
  Connection.send('torrent-start', {'ids' => id.class == Array ? id : [id]})
end

#start_all(&cb) ⇒ Object



14
15
16
# File 'lib/transmission-client/client.rb', line 14

def start_all &cb
  Connection.send('torrent-start')
end

#stop(id) ⇒ Object



22
23
24
# File 'lib/transmission-client/client.rb', line 22

def stop(id)
  Connection.send('torrent-stop', {'ids' => id.class == Array ? id : [id]})
end

#stop_all(&cb) ⇒ Object



26
27
28
# File 'lib/transmission-client/client.rb', line 26

def stop_all &cb
  Connection.send('torrent-stop')
end

#torrents(fields = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/transmission-client/client.rb', line 57

def torrents(fields = nil)
 Connection.request('torrent-get', {'fields' => fields ? fields : Transmission::Torrent::ATTRIBUTES}) { |resp| 
   torrs = []
   resp['torrents'].each do |t|
     torrs << Torrent.new(t)
  end
  yield torrs
 }
end