transmission-connect 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem 'json'
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :development do
11
+ gem "shoulda", ">= 0"
12
+ gem "bundler", "~> 1.0.0"
13
+ gem "jeweler", "~> 1.5.2"
14
+ gem "rcov", ">= 0"
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ json (1.5.1)
10
+ rake (0.8.7)
11
+ rcov (0.9.9)
12
+ shoulda (2.11.3)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ bundler (~> 1.0.0)
19
+ jeweler (~> 1.5.2)
20
+ json
21
+ rcov
22
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Dmitry Brazhkin
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = transmission-connect
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to transmission-connect
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Dmitry Brazhkin. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "transmission-connect"
16
+ gem.homepage = "http://github.com/Daem0n/transmission-connect"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Simple connector to transmission bittorrent client}
19
+ gem.description = %Q{Simple connector to transmission bittorrent client by HTTP-RPC}
20
+ gem.email = "dbrazhkin@gmail.com"
21
+ gem.authors = ["Dmitry Brazhkin"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "transmission-connect #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.1
@@ -0,0 +1,77 @@
1
+ module Transmission
2
+ RPC_PATH = 'transmission/rpc'
3
+
4
+ class Client
5
+ SESSION_STAT_ARGS = %w(activeTorrentCount downloadSpeed pausedTorrentCount torrentCount uploadSpeed cumulative-stats current-stats)
6
+ TORRENT_ARGS = %w(downloadDir hashString id isFinished name percentDone status totalSize error errorString rateDownload rateUpload)
7
+ CHECK_WAIT = 1
8
+ CHECK = 2
9
+ DOWNLOAD = 4
10
+ SEED = 8
11
+ STOPPED = 16
12
+ STATUS = {
13
+ 1 => :check_wait,
14
+ 2 => :check,
15
+ 4 => :download,
16
+ 8 => :seed,
17
+ 16 => :stopped
18
+ }
19
+
20
+ def initialize(host = '127.0.0.1', port = 9091, username = nil, password = nil)
21
+ @header = username.nil? ? {} : {'Authorization' => Base64.encode64("#{username}:#{password}")}
22
+ @uri = URI.parse("http://#{host}:#{port}/#{RPC_PATH}")
23
+ @connection = Net::HTTP.start(@uri.host, @uri.port)
24
+ end
25
+
26
+ def session_stats(args = {})
27
+ request('session-stats', args)
28
+ end
29
+
30
+ def add_bt_magnet(hash)
31
+ hash = "magnet:?xt=urn:btih:#{hash}"
32
+ request('torrent-add', {:filename => hash})
33
+ end
34
+
35
+ def rem_bt_magnet(hash)
36
+ request('torrent-remove', {:ids => hash.to_a, 'delete-local-data' => true})
37
+ end
38
+
39
+ def pause_magnet(hash)
40
+ request('torrent-stop', {:ids => hash.to_a})
41
+ end
42
+
43
+ def unpause_magnet(hash)
44
+ request('torrent-start', {:ids => hash.to_a})
45
+ end
46
+
47
+ def get_info(hash = nil)
48
+ args = hash.nil? ? {} : {:ids => hash.to_a}
49
+ args = {:fields => TORRENT_ARGS}.merge(args)
50
+ request('torrent-get', args)
51
+ end
52
+
53
+ private
54
+ def request(method, args)
55
+ post_data = build_json method, args
56
+ result = @connection.post2(@uri.path, post_data, @header)
57
+ case result
58
+ when Net::HTTPSuccess
59
+ JSON.parse(result.read_body)['arguments']
60
+ when Net::HTTPConflict
61
+ @header = @header.merge("x-transmission-session-id" => result.header["x-transmission-session-id"])
62
+ request(method, args)
63
+ else
64
+ raise Exception
65
+ end
66
+ end
67
+
68
+ def build_json(method,attributes = {})
69
+ if attributes.length == 0
70
+ {'method' => method}.to_json
71
+ else
72
+ {'method' => method, 'arguments' => attributes }.to_json
73
+ end
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,4 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'base64'
4
+ require 'transmission-connect/client'
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'transmission-connect'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,90 @@
1
+ require 'helper'
2
+
3
+ class TestClient < Test::Unit::TestCase
4
+ context "a transmission client" do
5
+ setup do
6
+ @uri = URI.parse("http://127.0.0.1:9091/transmission/rpc")
7
+ @connect = Transmission::Client.new
8
+ @hash = '562e7e6eb0852a652f329f5e8518385c5762fa84'
9
+ @hash = @hash.upcase
10
+ end
11
+
12
+ should "have connection" do
13
+ assert_instance_of Transmission::Client, @connect
14
+ end
15
+
16
+ should "get common info" do
17
+ assert_instance_of Hash, @connect.session_stats
18
+ end
19
+
20
+ should "get all torrents info" do
21
+ result = @connect.get_info
22
+ assert_instance_of Hash, result
23
+ assert_not_nil result['torrents']
24
+ assert_instance_of Array, result['torrents']
25
+ end
26
+
27
+ context 'with torrent' do
28
+ setup do
29
+ @arg_count = Transmission::Client::TORRENT_ARGS.count
30
+ end
31
+
32
+ should "add magnet link to downloads" do
33
+ @count = @connect.session_stats["torrentCount"]
34
+ result = @connect.add_bt_magnet(@hash)
35
+ count = @connect.session_stats["torrentCount"]
36
+ assert_equal @count + 1, count
37
+ assert_equal @hash.upcase, result['torrent-added']['hashString'].upcase
38
+ @connect.rem_bt_magnet(@hash)
39
+ end
40
+
41
+ should 'get info about all' do
42
+ @count = @connect.session_stats["torrentCount"]
43
+ result = @connect.get_info
44
+ assert_instance_of Hash, result
45
+ assert_equal @count, result['torrents'].count
46
+ result['torrents'].each do |torrent|
47
+ assert_equal @arg_count, torrent.keys.count
48
+ end
49
+ end
50
+
51
+ should "remove by magnet link" do
52
+ @connect.add_bt_magnet(@hash)
53
+ @count = @connect.session_stats["torrentCount"]
54
+ result = @connect.rem_bt_magnet(@hash)
55
+ assert_equal @count - 1, @connect.session_stats["torrentCount"]
56
+ end
57
+
58
+ should 'get info by hash' do
59
+ @connect.add_bt_magnet(@hash)
60
+ result = @connect.get_info(@hash)
61
+ assert_instance_of Hash, result
62
+ assert_equal 1, result['torrents'].count
63
+ assert_equal @arg_count, result['torrents'].first.keys.count
64
+ @connect.rem_bt_magnet(@hash)
65
+ end
66
+
67
+ should 'pause torrent' do
68
+ @connect.add_bt_magnet(@hash)
69
+ @connect.pause_magnet(@hash)
70
+ sleep 1
71
+ result = @connect.get_info(@hash)
72
+ assert_equal Transmission::Client::STOPPED, result['torrents'].first['status']
73
+ @connect.rem_bt_magnet(@hash)
74
+ end
75
+
76
+ should 'unpause torrent' do
77
+ @connect.add_bt_magnet(@hash)
78
+ @connect.pause_magnet(@hash)
79
+ sleep 1
80
+ @connect.unpause_magnet(@hash)
81
+ sleep 4
82
+ result = @connect.get_info(@hash)
83
+ assert_not_equal Transmission::Client::STOPPED, result['torrents'].first['status']
84
+ @connect.rem_bt_magnet(@hash)
85
+ end
86
+
87
+ end
88
+
89
+ end
90
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestTransmissionConnect < Test::Unit::TestCase
4
+ should "exist module Transmission" do
5
+ assert_not_nil Transmission
6
+ end
7
+ end
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{transmission-connect}
8
+ s.version = "0.2.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Dmitry Brazhkin"]
12
+ s.date = %q{2011-04-11}
13
+ s.description = %q{Simple connector to transmission bittorrent client by HTTP-RPC}
14
+ s.email = %q{dbrazhkin@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/transmission-connect.rb",
28
+ "lib/transmission-connect/client.rb",
29
+ "test/helper.rb",
30
+ "test/test_client.rb",
31
+ "test/test_transmission-connect.rb",
32
+ "transmission-connect.gemspec"
33
+ ]
34
+ s.homepage = %q{http://github.com/Daem0n/transmission-connect}
35
+ s.licenses = ["MIT"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.7.1}
38
+ s.summary = %q{Simple connector to transmission bittorrent client}
39
+ s.test_files = [
40
+ "test/helper.rb",
41
+ "test/test_client.rb",
42
+ "test/test_transmission-connect.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<json>, [">= 0"])
50
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
51
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
52
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
53
+ s.add_development_dependency(%q<rcov>, [">= 0"])
54
+ else
55
+ s.add_dependency(%q<json>, [">= 0"])
56
+ s.add_dependency(%q<shoulda>, [">= 0"])
57
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
58
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
59
+ s.add_dependency(%q<rcov>, [">= 0"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<json>, [">= 0"])
63
+ s.add_dependency(%q<shoulda>, [">= 0"])
64
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
65
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
66
+ s.add_dependency(%q<rcov>, [">= 0"])
67
+ end
68
+ end
69
+
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: transmission-connect
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 1
10
+ version: 0.2.1
11
+ platform: ruby
12
+ authors:
13
+ - Dmitry Brazhkin
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-11 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ version_requirements: &id001 !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ hash: 3
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ name: json
31
+ prerelease: false
32
+ type: :runtime
33
+ requirement: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ version_requirements: &id002 !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ hash: 3
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ name: shoulda
45
+ prerelease: false
46
+ type: :development
47
+ requirement: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ version_requirements: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ hash: 23
55
+ segments:
56
+ - 1
57
+ - 0
58
+ - 0
59
+ version: 1.0.0
60
+ name: bundler
61
+ prerelease: false
62
+ type: :development
63
+ requirement: *id003
64
+ - !ruby/object:Gem::Dependency
65
+ version_requirements: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ hash: 7
71
+ segments:
72
+ - 1
73
+ - 5
74
+ - 2
75
+ version: 1.5.2
76
+ name: jeweler
77
+ prerelease: false
78
+ type: :development
79
+ requirement: *id004
80
+ - !ruby/object:Gem::Dependency
81
+ version_requirements: &id005 !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ name: rcov
91
+ prerelease: false
92
+ type: :development
93
+ requirement: *id005
94
+ description: Simple connector to transmission bittorrent client by HTTP-RPC
95
+ email: dbrazhkin@gmail.com
96
+ executables: []
97
+
98
+ extensions: []
99
+
100
+ extra_rdoc_files:
101
+ - LICENSE.txt
102
+ - README.rdoc
103
+ files:
104
+ - .document
105
+ - Gemfile
106
+ - Gemfile.lock
107
+ - LICENSE.txt
108
+ - README.rdoc
109
+ - Rakefile
110
+ - VERSION
111
+ - lib/transmission-connect.rb
112
+ - lib/transmission-connect/client.rb
113
+ - test/helper.rb
114
+ - test/test_client.rb
115
+ - test/test_transmission-connect.rb
116
+ - transmission-connect.gemspec
117
+ homepage: http://github.com/Daem0n/transmission-connect
118
+ licenses:
119
+ - MIT
120
+ post_install_message:
121
+ rdoc_options: []
122
+
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ hash: 3
131
+ segments:
132
+ - 0
133
+ version: "0"
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ hash: 3
140
+ segments:
141
+ - 0
142
+ version: "0"
143
+ requirements: []
144
+
145
+ rubyforge_project:
146
+ rubygems_version: 1.7.1
147
+ signing_key:
148
+ specification_version: 3
149
+ summary: Simple connector to transmission bittorrent client
150
+ test_files:
151
+ - test/helper.rb
152
+ - test/test_client.rb
153
+ - test/test_transmission-connect.rb