Class: P3::Transmission::Client
- Inherits:
-
Object
- Object
- P3::Transmission::Client
- Defined in:
- lib/p3-transmission/client.rb
Constant Summary collapse
- TORRENT_FIELDS =
[ "id", "name", "totalSize", "addedDate", "isFinished", "rateDownload", "rateUpload", "percentDone", "files", "comment", "description", "downloadDir", "doneDate", "eta", "hashString", "leechers", "leftUntilDone", "name", "rateDownload", "seeders", "seedRatioLimit", "sizeWhenDone", "status", "torrentFile", "trackers", "uploadLimit", "uploadLimited", "uploadRatio" ]
Instance Attribute Summary collapse
-
#basic_auth ⇒ Object
Returns the value of attribute basic_auth.
-
#debug_mode ⇒ Object
Returns the value of attribute debug_mode.
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#session_id ⇒ Object
Returns the value of attribute session_id.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #all ⇒ Object
- #create(filename) ⇒ Object
-
#destroy(id) ⇒ Object
remove the torrent and delete the file.
- #find(id) ⇒ Object
- #http_post(opts) ⇒ Object
-
#initialize(opts) ⇒ Client
constructor
A new instance of Client.
- #log(message) ⇒ Object
- #log_response(response) ⇒ Object
- #pause(id) ⇒ Object
- #pause_all ⇒ Object
- #post(opts) ⇒ Object
-
#remove(id) ⇒ Object
remove the torrent but keep the file.
- #resume(id) ⇒ Object
- #resume_all ⇒ Object
Constructor Details
#initialize(opts) ⇒ Client
Returns a new instance of Client.
41 42 43 44 45 46 47 |
# File 'lib/p3-transmission/client.rb', line 41 def initialize(opts) @url = opts[:url] || "http://#{opts[:host]}:#{opts[:port]}/transmission/rpc" @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_auth ⇒ Object
Returns the value of attribute basic_auth.
6 7 8 |
# File 'lib/p3-transmission/client.rb', line 6 def basic_auth @basic_auth end |
#debug_mode ⇒ Object
Returns the value of attribute debug_mode.
8 9 10 |
# File 'lib/p3-transmission/client.rb', line 8 def debug_mode @debug_mode end |
#fields ⇒ Object
Returns the value of attribute fields.
7 8 9 |
# File 'lib/p3-transmission/client.rb', line 7 def fields @fields end |
#session_id ⇒ Object
Returns the value of attribute session_id.
4 5 6 |
# File 'lib/p3-transmission/client.rb', line 4 def session_id @session_id end |
#url ⇒ Object
Returns the value of attribute url.
5 6 7 |
# File 'lib/p3-transmission/client.rb', line 5 def url @url end |
Instance Method Details
#all ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/p3-transmission/client.rb', line 49 def all log "get_torrents" response = post( :method => "torrent-get", :arguments => { :fields => fields } ) response["arguments"]["torrents"] end |
#create(filename) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/p3-transmission/client.rb', line 120 def create(filename) log "add_torrent: #{filename}" response = post( :method => "torrent-add", :arguments => { :filename => filename } ) response["arguments"]["torrent-added"] end |
#destroy(id) ⇒ Object
remove the torrent and delete the file
151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/p3-transmission/client.rb', line 151 def destroy(id) log "remove_torrent: #{id}" response = post( :method => "torrent-remove", :arguments => { :ids => [id], :"delete-local-data" => true } ) response end |
#find(id) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/p3-transmission/client.rb', line 63 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
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/p3-transmission/client.rb', line 176 def http_post(opts) = { :body => opts.to_json, :headers => { "x-transmission-session-id" => session_id } } .merge!( :basic_auth => basic_auth ) if basic_auth log "url: #{url}" log "post_body:" log JSON.parse([:body]).to_yaml log "------------------" response = HTTParty.post( url, ) 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
202 203 204 |
# File 'lib/p3-transmission/client.rb', line 202 def log() Kernel.puts "[P3::Transmission #{Time.now.strftime( "%F %T" )}] #{}" if debug_mode end |
#log_response(response) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/p3-transmission/client.rb', line 206 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.}" log "response.body_raw:" log response.body log "-----------------" log "response.body:" log body log "-----------------" log "response.headers:" log headers log "------------------" end |
#pause(id) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/p3-transmission/client.rb', line 99 def pause(id) log "pause_torrent: #{id}" response = post( :method => "torrent-stop", :arguments => { :ids => [id] } ) end |
#pause_all ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/p3-transmission/client.rb', line 111 def pause_all() log "pause_all_torrents" response = post( :method => "torrent-stop", ) end |
#post(opts) ⇒ Object
166 167 168 169 170 171 172 173 174 |
# File 'lib/p3-transmission/client.rb', line 166 def post(opts) response_parsed = JSON::parse( http_post(opts).body ) if response_parsed["result"] != "success" raise Transmission::Exception, response_parsed["result"] end response_parsed end |
#remove(id) ⇒ Object
remove the torrent but keep the file
135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/p3-transmission/client.rb', line 135 def remove(id) log "remove_torrent: #{id}" response = post( :method => "torrent-remove", :arguments => { :ids => [id], :"delete-local-data" => false } ) response end |
#resume(id) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/p3-transmission/client.rb', line 78 def resume(id) log "resume_torrent: #{id}" response = post( :method => "torrent-start", :arguments => { :ids => [id] } ) end |
#resume_all ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/p3-transmission/client.rb', line 90 def resume_all() log "resume_all_torrents" response = post( :method => "torrent-start", ) end |