Class: Crankshaft::Session

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

Instance Method Summary collapse

Constructor Details

#initialize(url, timeout = 2) ⇒ Session

Returns a new instance of Session.



11
12
13
14
# File 'lib/crankshaft/session.rb', line 11

def initialize(url, timeout = 2)
  @uri = URI.parse(url)
  @timeout = timeout
end

Instance Method Details

#add(file, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/crankshaft/session.rb', line 32

def add(file, options = {})
  arguments = { :metainfo => Base64.encode64(file) }.merge(options)
  response = execute('torrent-add', arguments)
  if response['result'] == 'success'
    attrs = response['arguments']['torrent-added']
    if attrs
      return Crankshaft::Torrent.new(self, attrs)
    end
  end
end

#execute(method, arguments = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/crankshaft/session.rb', line 16

def execute(method, arguments = {})
  begin
    request = { :method => method, :arguments => arguments }
    response = connection.post(@uri.path, request.to_json, @headers)
    if response.class == Net::HTTPConflict
      @headers = { 'X-Transmission-Session-Id' => response['X-Transmission-Session-Id'] }
      return execute(method, arguments)
    end

    return JSON.parse(response.body)
  rescue Exception => e
    @connection = nil
    throw "Failed to execute request: #{e.message}"
  end
end

#torrent(hash, *attrs) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/crankshaft/session.rb', line 52

def torrent(hash, *attrs)
  arguments = { :ids => [ hash ], :fields => Crankshaft::Torrent::ATTRS + attrs }
  response = execute('torrent-get', arguments)
  if response['result'] == 'success'
    attrs = response['arguments']['torrents'][0]
    if attrs
      return Crankshaft::Torrent.new(self, attrs)
    end
  end
end

#torrents(*attrs) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/crankshaft/session.rb', line 43

def torrents(*attrs)
  arguments = { :fields => Crankshaft::Torrent::ATTRS }
  response = execute('torrent-get', arguments)

  return response['arguments']['torrents'].map do |attrs|
    Crankshaft::Torrent.new(self, attrs)
  end
end