transmission-rpc-ruby 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: efdf44a2260c3452e6cb831d4500909d58de5065
4
- data.tar.gz: d13509c0535e64e4de343e417d393dcf31b4e799
3
+ metadata.gz: 15491a3e93e2a8a22302abb1d9921ad6a860a080
4
+ data.tar.gz: db5be0864ccb78101bf44138a992265260cbc5b2
5
5
  SHA512:
6
- metadata.gz: 31ba84133758e97c74d5e25ab7c08ac0b1457ab7fe3e0272c7f440dba1eeab2ea91d6409649a904d9f8dcf60bbdcec665c6700ade82609c8b62577707aa79f00
7
- data.tar.gz: d5482d480e85a4b47fd87486a8c8ac8bfb3e8a2c02a443f24bb4e6e20cba13d30c26e5fcb5d1e80980f92834c55c40489cef80f143dbef0f8fbb59c06d8cbd6c
6
+ metadata.gz: 76c299668f5f310ebd87c07f8d98f889e49091cc5b8469fbd3ed91f3e430c051f602a45497f2a11e097e04a3eb345b47f705b759606c3184cf2182bca0c13520
7
+ data.tar.gz: f31884514bc2afa898b2e190445dc900f242385dc74c45747b5d5df9369add7b9a16fcfe144644849155469c44339021328746aa8b8d0d2fc833fbcb4ea56d90
data/CHANGELOG.md ADDED
@@ -0,0 +1,27 @@
1
+ ## v0.3.0 (2015-04-02)
2
+
3
+ Features:
4
+
5
+ - ability to handle multiple torrents in one instance
6
+ - `start_all!` & `stop_all!` static class methods for torrents
7
+ - `reload!`, `to_json`, `is_multi?`, `is_finished` instance method for torrents
8
+ - `to_json` instance method for session & session stats
9
+
10
+ ## v0.2.1 (2015-04-01)
11
+
12
+ Bugfixes:
13
+
14
+ - when adding torrents the returned torrent instance will use same options for finding added torrent
15
+
16
+ ## v0.2.0 (2015-03-31)
17
+
18
+ Features:
19
+
20
+ - all basic torrent actions (start, stop, move up queue, etc)
21
+ - session model
22
+ - session stats model
23
+ - adding torrents
24
+
25
+ ## v0.1.0 (2015-03-12)
26
+
27
+ - Initial project import
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Transmission RPC Ruby
2
2
 
3
- [![Build Status](https://travis-ci.org/transmission-rails/transmission-rpc-ruby.svg?branch=master)](https://travis-ci.org/transmission-rails/transmission-rpc-ruby) [![Code Climate](https://codeclimate.com/github/transmission-rails/transmission-rpc-ruby/badges/gpa.svg)](https://codeclimate.com/github/transmission-rails/transmission-rpc-ruby) [![Dependency Status](https://gemnasium.com/transmission-rails/transmission-rpc-ruby.svg)](https://gemnasium.com/transmission-rails/transmission-rpc-ruby) [![Coverage Status](https://coveralls.io/repos/transmission-rails/transmission-rpc-ruby/badge.svg?branch=master)](https://coveralls.io/r/transmission-rails/transmission-rpc-ruby?branch=master)
3
+ [![Build Status](https://travis-ci.org/transmission-rails/transmission-rpc-ruby.svg?branch=master)](https://travis-ci.org/transmission-rails/transmission-rpc-ruby) [![Code Climate](https://codeclimate.com/github/transmission-rails/transmission-rpc-ruby/badges/gpa.svg)](https://codeclimate.com/github/transmission-rails/transmission-rpc-ruby) [![Dependency Status](https://gemnasium.com/transmission-rails/transmission-rpc-ruby.svg)](https://gemnasium.com/transmission-rails/transmission-rpc-ruby) [![Coverage Status](https://coveralls.io/repos/transmission-rails/transmission-rpc-ruby/badge.svg?branch=master)](https://coveralls.io/r/transmission-rails/transmission-rpc-ruby?branch=master) [![Gem Version](https://badge.fury.io/rb/transmission-rpc-ruby.svg)](http://badge.fury.io/rb/transmission-rpc-ruby)
4
4
 
5
5
  Transmission RPC Ruby is a Ruby library to communicate with Transmission RPC (bittorrent client).
6
6
  This library is based on this [spec](https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt) and currently supports RPC versions >= 14
@@ -105,6 +105,12 @@ Or use an RPC connector instance
105
105
  torrent.move_top!
106
106
  torrent.move_bottom!
107
107
 
108
+ torrent.finished?
109
+ # => true
110
+
111
+ torrent.to_json
112
+ # => {"id"=>132, "name"=>"Torrent Name", ....}
113
+
108
114
  You can access the torrent accessors & mutators via instance methods too
109
115
 
110
116
  # uploadLimited
@@ -117,6 +123,43 @@ The `save!` method will update the torrent on your remote transmission daemon.
117
123
 
118
124
  To find all the torrent [accessors](https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt#L127) & [mutators](https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt#L90) visit [spec](https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt)
119
125
 
126
+ #### Start & Stop all torrents
127
+
128
+ You can also start and stop all torrents
129
+
130
+ Transmission::Model::Torrent.start_all!
131
+
132
+ Transmission::Model::Torrent.stop_all!
133
+
134
+ #### Dealing with multiple torrents
135
+
136
+ If you want to change multiple torrents at once:
137
+
138
+ ids = [1, 2, 3]
139
+ torrents = Transmission::Model::Torrent.find ids
140
+
141
+ This will return a `Transmission::Model::Torrent` instance which takes the same methods as described before.
142
+
143
+ torrents.start!
144
+ torrents.stop!
145
+ # ...
146
+
147
+ torrents.to_json
148
+ # => [{"id"=>132, "name"=>"Torrent Name", ....}, {...}]
149
+
150
+ # uploadLimited
151
+ torrents.upload_limited = false
152
+ torrents.save!
153
+
154
+ This will change `uploadLimited` for all torrents with ids 1, 2 & 3.
155
+
156
+ __NOTE:__ If using `Transmission::Model::Torrent` you will only be able to modify their mutators.
157
+
158
+ To find out if a torrent instance contains multiple torrents
159
+
160
+ torrents.is_multi?
161
+ # => true
162
+
120
163
  ### Session
121
164
 
122
165
  To find out more about the current session use the `Transmission::Model::Session` class.
@@ -178,6 +221,42 @@ If it is not desired to use any of the `Transmission::Model` classes you can use
178
221
 
179
222
  For more methods check out `lib/transmission/rpc.rb`
180
223
 
224
+ ## Changelog
225
+
226
+ ### v0.3.0 (2015-04-02)
227
+
228
+ Features:
229
+
230
+ - ability to handle multiple torrents in one instance
231
+ - `start_all!` & `stop_all!` static class methods for torrents
232
+ - `reload!`, `to_json`, `is_multi?`, `is_finished` instance method for torrents
233
+ - `to_json` instance method for session & session stats
234
+
235
+ ### v0.2.1 (2015-04-01)
236
+
237
+ Bugfixes:
238
+
239
+ - when adding torrents the returned torrent instance will use same options for finding added torrent
240
+
241
+ ### v0.2.0 (2015-03-31)
242
+
243
+ Features:
244
+
245
+ - all basic torrent actions (start, stop, move up queue, etc)
246
+ - session model
247
+ - session stats model
248
+ - adding torrents
249
+
250
+ ### v0.1.0 (2015-03-12)
251
+
252
+ - Initial project import
253
+
254
+ ## Roadmap
255
+
256
+ - Add support for all versions of RPC
257
+ - More documentation
258
+ - Add 'torrent-rename-path' & 'torrent-set-location' & 'port-test' & 'free-space' & 'session-close' RPC methods
259
+
181
260
  ## Contribute
182
261
 
183
262
  Please help make this gem awesome! If you have any suggestions or feedback either create an issue or PR.
data/lib/transmission.rb CHANGED
@@ -6,5 +6,5 @@ require File.join(File.dirname(__FILE__), 'transmission', 'arguments')
6
6
  require File.join(File.dirname(__FILE__), 'transmission', 'utils')
7
7
 
8
8
  module Transmission
9
- VERSION = '0.2.1'
9
+ VERSION = '0.3.0'
10
10
  end
@@ -15,6 +15,10 @@ module Transmission
15
15
  connector.set_session filtered
16
16
  end
17
17
 
18
+ def to_json
19
+ @attributes
20
+ end
21
+
18
22
  def method_missing(symbol, *args)
19
23
  string = symbol.to_s
20
24
  if string[-1] == '='
@@ -9,6 +9,10 @@ module Transmission
9
9
  @connector = connector
10
10
  end
11
11
 
12
+ def to_json
13
+ @attributes
14
+ end
15
+
12
16
  class << self
13
17
  def get(options = {})
14
18
  rpc = options[:connector] || connector
@@ -6,65 +6,90 @@ module Transmission
6
6
  class MissingAttributesError < StandardError; end
7
7
  class DuplicateTorrentError < StandardError; end
8
8
 
9
- attr_accessor :attributes, :deleted, :connector
9
+ attr_accessor :attributes, :deleted, :connector, :torrents, :ids
10
10
 
11
11
  def initialize(torrent_object, connector)
12
- @attributes = torrent_object
12
+ if torrent_object.is_a? Array
13
+ is_single = torrent_object.size == 1
14
+ @attributes = is_single ? torrent_object.first : {}
15
+ @ids = is_single ? [@attributes['id'].to_i] : []
16
+ @torrents = torrent_object.inject([]) do |torrents, torrent|
17
+ @ids << torrent['id'].to_i
18
+ torrents << Torrent.new([torrent], connector)
19
+ end unless is_single
20
+ end
13
21
  @connector = connector
14
22
  end
15
23
 
16
24
  def delete!(remove_local_data = false)
17
- connector.remove_torrent [self.id], remove_local_data
25
+ connector.remove_torrent @ids, remove_local_data
18
26
  @deleted = true
19
27
  end
20
28
 
21
29
  def save!
22
30
  filtered = Transmission::Arguments::TorrentSet.filter @attributes
23
- connector.set_torrent [self.id], filtered
31
+ connector.set_torrent @ids, filtered
24
32
  end
25
33
 
26
34
  def move_up!
27
- connector.move_up_torrent [self.id]
35
+ connector.move_up_torrent @ids
28
36
  end
29
37
 
30
38
  def move_down!
31
- connector.move_down_torrent [self.id]
39
+ connector.move_down_torrent @ids
32
40
  end
33
41
 
34
42
  def move_top!
35
- connector.move_top_torrent [self.id]
43
+ connector.move_top_torrent @ids
36
44
  end
37
45
 
38
46
  def move_bottom!
39
- connector.move_bottom_torrent [self.id]
47
+ connector.move_bottom_torrent @ids
40
48
  end
41
49
 
42
50
  def start!
43
- connector.start_torrent [self.id]
51
+ connector.start_torrent @ids
44
52
  end
45
53
 
46
54
  def start_now!
47
- connector.start_torrent_now [self.id]
55
+ connector.start_torrent_now @ids
48
56
  end
49
57
 
50
58
  def stop!
51
- connector.stop_torrent [self.id]
59
+ connector.stop_torrent @ids
52
60
  end
53
61
 
54
62
  def verify!
55
- connector.verify_torrent [self.id]
63
+ connector.verify_torrent @ids
56
64
  end
57
65
 
58
66
  def re_announce!
59
- connector.re_announce_torrent [self.id]
67
+ connector.re_announce_torrent @ids
68
+ end
69
+
70
+ def is_multi?
71
+ @ids.size > 1
60
72
  end
61
73
 
62
74
  def finished?
75
+ self.percent_done == 1
76
+ end
63
77
 
78
+ def reload!
79
+ torrents = Torrent.find @ids, connector: @connector
80
+ @ids = torrents.ids
81
+ @attributes = torrents.attributes
82
+ @torrents = torrents.torrents
64
83
  end
65
84
 
66
85
  def to_json
67
-
86
+ if is_multi?
87
+ @torrents.inject([]) do |torrents, torrent|
88
+ torrents << torrent.to_json
89
+ end
90
+ else
91
+ @attributes
92
+ end
68
93
  end
69
94
 
70
95
  def method_missing(symbol, *args)
@@ -84,16 +109,15 @@ module Transmission
84
109
  def all(options = {})
85
110
  rpc = options[:connector] || connector
86
111
  body = rpc.get_torrent nil, options[:fields]
87
- body['torrents'].inject([]) do |torrents, torrent|
88
- torrents << Torrent.new(torrent, rpc)
89
- end
112
+ Torrent.new body['torrents'], rpc
90
113
  end
91
114
 
92
115
  def find(id, options = {})
93
116
  rpc = options[:connector] || connector
94
- body = rpc.get_torrent [id], options[:fields]
117
+ ids = id.is_a?(Array) ? id : [id]
118
+ body = rpc.get_torrent ids, options[:fields]
95
119
  raise TorrentNotFoundError if body['torrents'].size == 0
96
- Torrent.new body['torrents'].first, rpc
120
+ Torrent.new body['torrents'], rpc
97
121
  end
98
122
 
99
123
  def add(options = {})
@@ -103,6 +127,16 @@ module Transmission
103
127
  find body['torrent-added']['id'], options
104
128
  end
105
129
 
130
+ def start_all!(options = {})
131
+ rpc = options[:connector] || connector
132
+ rpc.start_torrent nil
133
+ end
134
+
135
+ def stop_all!(options = {})
136
+ rpc = options[:connector] || connector
137
+ rpc.stop_torrent nil
138
+ end
139
+
106
140
  def connector
107
141
  Transmission::Config.get_connector
108
142
  end
@@ -45,6 +45,14 @@ describe Transmission::Model::Session do
45
45
  end
46
46
  end
47
47
 
48
+ describe '#to_json' do
49
+ let(:session) { Transmission::Model::Session.new({'id' => 1}, nil) }
50
+
51
+ it 'should return its attributes' do
52
+ expect(session.to_json).to eq({'id' => 1})
53
+ end
54
+ end
55
+
48
56
  describe '#method_missing' do
49
57
  let(:session) { Transmission::Model::Session.new({'id' => 1, 'name' => 'some name', 'some-key' => 'some-value'}, nil) }
50
58
 
@@ -8,10 +8,9 @@ describe Transmission::Model::Torrent do
8
8
  stub_get_torrent({}, [{id: 1}])
9
9
  end
10
10
 
11
- it 'should return an array of Torrent models' do
11
+ it 'should return a Torrent model instance' do
12
12
  torrents = Transmission::Model::Torrent.all
13
- expect(torrents).to be_an(Array)
14
- expect(torrents.first).to be_a(Transmission::Model::Torrent)
13
+ expect(torrents).to be_a(Transmission::Model::Torrent)
15
14
  end
16
15
  end
17
16
 
@@ -23,10 +22,9 @@ describe Transmission::Model::Torrent do
23
22
  .to_return(successful_response({arguments: {torrents: [{id: 1}]}}))
24
23
  end
25
24
 
26
- it 'should return an array of Torrent models' do
25
+ it 'should return a Torrent model instance' do
27
26
  torrents = Transmission::Model::Torrent.all connector: @rpc
28
- expect(torrents).to be_an(Array)
29
- expect(torrents.first).to be_a(Transmission::Model::Torrent)
27
+ expect(torrents).to be_a(Transmission::Model::Torrent)
30
28
  end
31
29
  end
32
30
 
@@ -71,6 +69,23 @@ describe Transmission::Model::Torrent do
71
69
  end
72
70
  end
73
71
 
72
+ describe 'with multiple ids' do
73
+ before :each do
74
+ @rpc = Transmission::RPC.new
75
+ stub_get_torrent({ids: [1, 2]}, [{id: 1}, {id: 2}])
76
+ end
77
+
78
+ it 'should return a Torrent model instance' do
79
+ torrent = Transmission::Model::Torrent.find [1, 2], connector: @rpc
80
+ expect(torrent).to be_a(Transmission::Model::Torrent)
81
+ end
82
+
83
+ it 'should remember all ids' do
84
+ torrent = Transmission::Model::Torrent.find [1, 2], connector: @rpc
85
+ expect(torrent.ids).to eq([1, 2])
86
+ end
87
+ end
88
+
74
89
  end
75
90
 
76
91
  describe '.add' do
@@ -107,6 +122,32 @@ describe Transmission::Model::Torrent do
107
122
 
108
123
  end
109
124
 
125
+ describe '.start_all!' do
126
+ before :each do
127
+ Transmission::Config.set
128
+ stub_rpc_request
129
+ .with(body: torrent_method_body('torrent-start', {}))
130
+ .to_return(successful_response)
131
+ end
132
+
133
+ it 'should start all torrents' do
134
+ Transmission::Model::Torrent.start_all!
135
+ end
136
+ end
137
+
138
+ describe '.stop_all!' do
139
+ before :each do
140
+ Transmission::Config.set
141
+ stub_rpc_request
142
+ .with(body: torrent_method_body('torrent-stop', {}))
143
+ .to_return(successful_response)
144
+ end
145
+
146
+ it 'should start all torrents' do
147
+ Transmission::Model::Torrent.stop_all!
148
+ end
149
+ end
150
+
110
151
  describe '#delete!' do
111
152
 
112
153
  describe 'with configuration' do
@@ -141,10 +182,77 @@ describe Transmission::Model::Torrent do
141
182
  end
142
183
  end
143
184
 
185
+ describe 'with multiple ids' do
186
+ before :each do
187
+ @rpc = Transmission::RPC.new
188
+ stub_get_torrent({ids: [1, 2]}, [{id: 1}, {id: 2}])
189
+ stub_rpc_request
190
+ .with(body: torrent_remove_body({:ids => [1, 2], 'delete-local-data' => false}))
191
+ .to_return(successful_response({arguments: {torrents: [{id: 1}, {id: 2}]}}))
192
+ end
193
+
194
+ it 'should remove the torrent files' do
195
+ torrent = Transmission::Model::Torrent.find [1, 2], :connector => @rpc
196
+ torrent.delete!
197
+ expect(torrent.deleted).to eq(true)
198
+ end
199
+ end
200
+
201
+ end
202
+
203
+ describe '#is_multi?' do
204
+ let(:torrents) { Transmission::Model::Torrent.new([{'id' => 1}, {'id' => 2}], nil) }
205
+ let(:torrent) { Transmission::Model::Torrent.new([{'id' => 1}], nil) }
206
+
207
+ describe 'with multiple torrents' do
208
+ it 'should return true' do
209
+ expect(torrents.is_multi?).to eq(true)
210
+ end
211
+ end
212
+
213
+ describe 'with single torrent' do
214
+ it 'should return false' do
215
+ expect(torrent.is_multi?).to eq(false)
216
+ end
217
+ end
218
+ end
219
+
220
+ describe '#to_json' do
221
+ let(:torrents) { Transmission::Model::Torrent.new([{'id' => 1}, {'id' => 2}], nil) }
222
+ let(:torrent) { Transmission::Model::Torrent.new([{'id' => 1}], nil) }
223
+
224
+ describe 'with multiple torrents' do
225
+ it 'should return true' do
226
+ expect(torrents.to_json).to eq([{'id' => 1}, {'id' => 2}])
227
+ end
228
+ end
229
+
230
+ describe 'with single torrent' do
231
+ it 'should return false' do
232
+ expect(torrent.to_json).to eq({'id' => 1})
233
+ end
234
+ end
235
+ end
236
+
237
+ describe '#finished?' do
238
+ let(:unfinished_torrent) { Transmission::Model::Torrent.new([{'id' => 1, 'percentDone' => 0.5}], nil) }
239
+ let(:finished_torrent) { Transmission::Model::Torrent.new([{'id' => 1, 'percentDone' => 1}], nil) }
240
+
241
+ describe 'with finished torrent' do
242
+ it 'should return true' do
243
+ expect(finished_torrent.finished?).to eq(true)
244
+ end
245
+ end
246
+
247
+ describe 'with unfinished torrent' do
248
+ it 'should return false' do
249
+ expect(unfinished_torrent.finished?).to eq(false)
250
+ end
251
+ end
144
252
  end
145
253
 
146
254
  describe '#method_missing' do
147
- let(:torrent) { Transmission::Model::Torrent.new({'id' => 1, 'name' => 'some name', 'some-key' => 'some-value'}, nil) }
255
+ let(:torrent) { Transmission::Model::Torrent.new([{'id' => 1, 'name' => 'some name', 'some-key' => 'some-value'}], nil) }
148
256
 
149
257
  before :each do
150
258
  stub_const("Transmission::Fields::TorrentGet::ATTRIBUTES", [{field: 'id'}, {field: 'name'}, {field: 'some-key'}])
@@ -211,16 +319,21 @@ describe Transmission::Model::Torrent do
211
319
  describe "##{object[:method]}" do
212
320
  let(:rpc) {Transmission::RPC.new}
213
321
 
214
- before :each do
215
- stub_get_torrent({ids: [1]}, [{id: 1}])
216
- stub_rpc_request
217
- .with(body: torrent_method_body(object[:rpc_method], {ids: [1]}))
218
- .to_return(successful_response)
219
- end
220
-
221
- it "should #{object[:method]} torrent" do
222
- torrent = Transmission::Model::Torrent.find 1, connector: rpc
223
- torrent.send object[:method].to_sym
322
+ [
323
+ {ids: [1], response: [{id: 1}], text: ''},
324
+ {ids: [1, 2], response: [{id: 1}, {id: 2}], text: 'with multiple ids'}
325
+ ].each do |args|
326
+ before :each do
327
+ stub_get_torrent({ids: args[:ids]}, args[:response])
328
+ stub_rpc_request
329
+ .with(body: torrent_method_body(object[:rpc_method], {ids: args[:ids]}))
330
+ .to_return(successful_response)
331
+ end
332
+
333
+ it "should #{object[:method]} torrent #{args[:text]}" do
334
+ torrent = Transmission::Model::Torrent.find args[:ids], connector: rpc
335
+ torrent.send object[:method].to_sym
336
+ end
224
337
  end
225
338
  end
226
339
  end
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'transmission-rpc-ruby'
3
- s.version = '0.2.1'
4
- s.date = '2015-03-27'
3
+ s.version = '0.3.0'
4
+ s.date = '2015-04-02'
5
5
  s.summary = "Transmission RPC wrapper in Ruby"
6
6
  s.description = "A new transmission RPC wrapper for Ruby. All object oriented for controlling remote transmission daemons."
7
7
  s.authors = ["Max Hoffmann"]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: transmission-rpc-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Hoffmann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-27 00:00:00.000000000 Z
11
+ date: 2015-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -76,6 +76,7 @@ files:
76
76
  - ".gitignore"
77
77
  - ".rspec"
78
78
  - ".travis.yml"
79
+ - CHANGELOG.md
79
80
  - Gemfile
80
81
  - Gemfile.lock
81
82
  - LICENSE