Class: TransmissionApi::Client

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

Direct Known Subclasses

ClientFake

Constant Summary collapse

TORRENT_FIELDS =
[
  "id",
  "name",
  "totalSize",
  "addedDate",
  "isFinished",
  "rateDownload",
  "rateUpload",
  "percentDone",
  "files"
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Client

Returns a new instance of Client.



20
21
22
23
24
25
26
# File 'lib/transmission_api/client.rb', line 20

def initialize(opts)
  @url = opts[:url]
  @fields = opts[:fields] || TORRENT_FIELDS
  @basic_auth = { :username => opts[:username], :password => opts[:password] } if opts[:username]
  @session_id = "NOT-INITIALIZED"
  @debug_mode = opts[:debug_mode] || false
end

Instance Attribute Details

#basic_authObject

Returns the value of attribute basic_auth.



4
5
6
# File 'lib/transmission_api/client.rb', line 4

def basic_auth
  @basic_auth
end

#debug_modeObject

Returns the value of attribute debug_mode.



6
7
8
# File 'lib/transmission_api/client.rb', line 6

def debug_mode
  @debug_mode
end

#fieldsObject

Returns the value of attribute fields.



5
6
7
# File 'lib/transmission_api/client.rb', line 5

def fields
  @fields
end

#session_idObject

Returns the value of attribute session_id.



2
3
4
# File 'lib/transmission_api/client.rb', line 2

def session_id
  @session_id
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/transmission_api/client.rb', line 3

def url
  @url
end

Instance Method Details

#allObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/transmission_api/client.rb', line 28

def all
  log "get_torrents"

  response =
    post(
      :method => "torrent-get",
      :arguments => {
        :fields => fields
      }
    )

  response["arguments"]["torrents"]
end

#create(filename) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/transmission_api/client.rb', line 57

def create(filename)
  log "add_torrent: #{filename}"

  response =
    post(
      :method => "torrent-add",
      :arguments => {
        :filename => filename
      }
    )

  response["arguments"]["torrent-added"]
end

#destroy(id) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/transmission_api/client.rb', line 71

def destroy(id)
  log "remove_torrent: #{id}"

  response =
    post(
      :method => "torrent-remove",
      :arguments => {
        :ids => [id],
        :"delete-local-data" => true
      }
    )

  response
end

#find(id) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/transmission_api/client.rb', line 42

def find(id)
  log "get_torrent: #{id}"

  response =
    post(
      :method => "torrent-get",
      :arguments => {
        :fields => fields,
        :ids => [id]
      }
    )

  response["arguments"]["torrents"].first
end

#http_post(opts) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/transmission_api/client.rb', line 96

def http_post(opts)
  post_options = {
    :body => opts.to_json,
    :headers => { "x-transmission-session-id" => session_id }
  }
  post_options.merge!( :basic_auth => basic_auth ) if basic_auth

  log "url: #{url}"
  log "post_body:"
  log JSON.parse(post_options[:body]).to_yaml
  log "------------------"

  response = HTTParty.post( url, post_options )

  log_response response

  # retry connection if session_id incorrect
  if( response.code == 409 )
    log "changing session_id"
    @session_id = response.headers["x-transmission-session-id"]
    response = http_post(opts)
  end

  response
end

#log(message) ⇒ Object



122
123
124
# File 'lib/transmission_api/client.rb', line 122

def log(message)
  Kernel.puts "[TransmissionApi #{Time.now.strftime( "%F %T" )}] #{message}" if debug_mode
end

#log_response(response) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/transmission_api/client.rb', line 126

def log_response(response)
  body = nil
  begin
    body = JSON.parse(response.body).to_yaml
  rescue
    body = response.body
  end

  headers = response.headers.to_yaml

  log "response.code: #{response.code}"
  log "response.message: #{response.message}"

  log "response.body_raw:"
  log response.body
  log "-----------------"

  log "response.body:"
  log body
  log "-----------------"

  log "response.headers:"
  log headers
  log "------------------"
end

#post(opts) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/transmission_api/client.rb', line 86

def post(opts)
  response_parsed = JSON::parse( http_post(opts).body )

  if response_parsed["result"] != "success"
    raise TransmissionApi::Exception, response_parsed["result"]
  end

  response_parsed
end