Class: TransmissionRSS::Client

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

Overview

Class for communication with transmission utilizing the RPC web interface.

Defined Under Namespace

Classes: Unauthorized

Constant Summary collapse

OPTIONS =
[:paused, :download_dir]

Instance Method Summary collapse

Constructor Details

#initialize(server = {}, login = nil, options = {}) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/transmission-rss/client.rb', line 15

def initialize(server = {},  = nil, options = {})
  options ||= {}

  @host     = server.host || 'localhost'
  @port     = server.port || 9091
  @tls      = !!server.tls
  @rpc_path = server.rpc_path || '/transmission/rpc'
  @login    = 

  @timeout  = options.timeout || 5
  @log      = TransmissionRSS::Log.instance
end

Instance Method Details

#add_torrent(file, type = :url, options = {}) ⇒ Object

POST json packed torrent add command.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/transmission-rss/client.rb', line 46

def add_torrent(file, type = :url, options = {})
  arguments = set_arguments_from_options(options)

  case type
    when :url
      file = URI.escape(file) if URI.unescape(file) == file
      arguments.filename = file
    when :file
      arguments.metainfo = Base64.encode64(File.read(file))
    else
      raise ArgumentError.new('type has to be :url or :file.')
  end

  response = rpc('torrent-add', arguments)
  id = get_id_from_response(response)

  log_message = 'torrent-add result: ' + response.result
  log_message << ' (id ' + id.to_s + ')' if id
  @log.debug(log_message)

  if id && options[:seed_ratio_limit]
    if options[:seed_ratio_limit].to_f < 0
      set_torrent(id, {
        'seedRatioMode' => 2
      })
    else
      set_torrent(id, {
        'seedRatioLimit' => options[:seed_ratio_limit].to_f,
        'seedRatioMode' => 1
      })
    end
  end

  response
end

#get_session_idObject

Get transmission session id.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/transmission-rss/client.rb', line 91

def get_session_id
  get = Net::HTTP::Get.new(@rpc_path)

  add_basic_auth(get)

  response = request(get)

  id = response.header['x-transmission-session-id']

  if id.nil?
    @log.debug("could not obtain session id (#{response.code}, " +
      "#{response.class})")
  else
    @log.debug('got session id ' + id)
  end

  id
end

#rpc(method, arguments) ⇒ Object

Raises:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/transmission-rss/client.rb', line 28

def rpc(method, arguments)
  sid = get_session_id
  raise Unauthorized unless sid

  post = Net::HTTP::Post.new \
    @rpc_path,
    {
      'Content-Type' => 'application/json',
      'X-Transmission-Session-Id' => sid
    }

  add_basic_auth(post)
  post.body = {method: method, arguments: arguments}.to_json

  JSON.parse(request(post).body)
end

#set_torrent(id, arguments = {}) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/transmission-rss/client.rb', line 82

def set_torrent(id, arguments = {})
  arguments.ids = [id]
  response = rpc('torrent-set', arguments)
  @log.debug('torrent-set result: ' + response.result)

  response
end