Class: Transmission

Inherits:
Object
  • Object
show all
Defined in:
lib/torrentsync.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port, user = nil, pass = nil) ⇒ Transmission

Returns a new instance of Transmission.



11
12
13
14
15
16
# File 'lib/torrentsync.rb', line 11

def initialize(host, port, user = nil, pass = nil)
  @host = host
  @port = port
  @user = user
  @pass = pass
end

Instance Method Details

#add(torrent) ⇒ Object

TODO DRY



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/torrentsync.rb', line 39

def add(torrent)
  sessionid = Net::HTTP.start(@host, @port) do |http|
    res = http.get('/transmission/rpc')
    h = Nokogiri::HTML.parse(res.body)
    h.css('code').text.split.last
  end
  header = {
    'Content-Type' => 'application/json',
  }
  header['X-Transmission-Session-Id'] = sessionid unless sessionid.nil?
  Net::HTTP.start(@host, @port) do |http|
    json = {
      :method => 'torrent-add',
      :arguments => { :metainfo => Base64::encode64(torrent) }
    }
    http.post('/transmission/rpc', json.to_json, header)
  end
end

#listObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/torrentsync.rb', line 18

def list
  sessionid = Net::HTTP.start(@host, @port) do |http|
    res = http.get('/transmission/rpc')
    h = Nokogiri::HTML.parse(res.body)
    h.css('code').text.split.last
  end
  header = {
    'Content-Type' => 'application/json',
  }
  header['X-Transmission-Session-Id'] = sessionid unless sessionid.nil?
  sessionid = Net::HTTP.start(@host, @port) do |http|
    json = {
      :method => 'torrent-get',
      :arguments => { :fields => [ :hashString, :id, :name ] }
    }
    res = http.post('/transmission/rpc', json.to_json, header)
    JSON.parse(res.body)
  end
end