aria2_driver 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.yardopts +4 -0
- data/{LICENSE → LICENSE.md} +0 -0
- data/README.md +68 -2
- data/lib/aria2_driver/json_rpc/client.rb +15 -14
- data/lib/aria2_driver/json_rpc/response_exception.rb +9 -0
- data/lib/aria2_driver/version.rb +1 -1
- data/spec/aria2_driver/json_rpc/client_spec.rb +94 -82
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
M2ZlMzFiMDg2MWYxNzA1MGMwMThiZDY5YjdhZDkxYjRlMjVhOGY2MQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NjhkZWM1OTkwZmJkMGMwMDE1OTJhOGFlNTgwZGJmODMzNDBiODUyZA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
Yjc1MjRkOWM3MGZkNDBhYTZjZDRhODdiMGU0MGY2NTNmZmFlNDZiNzdmMDBk
|
10
|
+
Mjk0YmQ4ZTJiYTRhMThmOTc4NGUwOTk2YWVjYjJhOGY4NDg3OWUzMmM4YjFh
|
11
|
+
MWI3MGJlMzEyOTczZmUzNjRmMzNkOTI4NmI0NTRlNTc3YTM3MTQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NmU0Njk2NTkzYTEyZTVkNTFiNDMzMDY5ZjVmOGVkMjI5ODkzMGMyMDk1YWY5
|
14
|
+
YmUwM2U5MTAzZTU3NTkzNGE5ZDIwNGJmMGJkMDgwNjBkZmI1MzNlODA1MzJj
|
15
|
+
ODk1OTUxY2NhYzI4YmViMjA3Nzk2ZDJiOGJlMzI3YjM1NDNkODI=
|
data/.yardopts
ADDED
data/{LICENSE → LICENSE.md}
RENAMED
File without changes
|
data/README.md
CHANGED
@@ -11,7 +11,7 @@ Simple api to manage aria2c via api
|
|
11
11
|
|
12
12
|
Add this line to your application's Gemfile:
|
13
13
|
|
14
|
-
```ruby
|
14
|
+
``` ruby
|
15
15
|
gem 'aria2_driver'
|
16
16
|
```
|
17
17
|
|
@@ -25,7 +25,73 @@ Or install it yourself as:
|
|
25
25
|
|
26
26
|
## Usage
|
27
27
|
|
28
|
-
|
28
|
+
|
29
|
+
### Simple aria2 client initialization
|
30
|
+
|
31
|
+
|
32
|
+
``` ruby
|
33
|
+
aria2 = Aria2Driver::JsonRpc::Client.new 'hostname'
|
34
|
+
```
|
35
|
+
|
36
|
+
With the previous line of code you can create a client that will make requests to `aria2c` downloader
|
37
|
+
running on **hostname** server accepting connections to port **80** with default **http** scheme.
|
38
|
+
The client created as is will have a self generated client id (using SecureRandom).
|
39
|
+
|
40
|
+
``` ruby
|
41
|
+
aria2 = Aria2Driver::JsonRpc::Client.new 'hostname',
|
42
|
+
{port: 9090, scheme: 'https', id: 'my', token: 'abcd-1234'}
|
43
|
+
```
|
44
|
+
|
45
|
+
With the code described above, you can override default connection parameters specifying also a
|
46
|
+
custom **client_id** and a **secret token**. The latter will be used in any of the following requests and
|
47
|
+
must match the **--rpc-secret** parameter specified in the aria2c configuration.
|
48
|
+
|
49
|
+
|
50
|
+
### Making requests
|
51
|
+
|
52
|
+
Aria2Driver allows you to make a generic request, via jsonrpc, in which you have to put the aria2 **method** and
|
53
|
+
request parameters, as described below
|
54
|
+
|
55
|
+
``` ruby
|
56
|
+
response = aria2.request Aria2Driver::JsonRpc::Request.new 'aria2.addUri',
|
57
|
+
{params: [['http://www.example.com/a.jpg'], {"dir" => "/tmp/"}]}
|
58
|
+
```
|
59
|
+
|
60
|
+
it's also possible to make the same request as follow
|
61
|
+
|
62
|
+
``` ruby
|
63
|
+
response = aria2.add_uri {params: [['http://www.example.com/a.jpg'], {"dir" => "/tmp/"}]}
|
64
|
+
```
|
65
|
+
|
66
|
+
### Response
|
67
|
+
|
68
|
+
Aria2Driver request can lead to a valid aria2c response or to an exception (*ResponseException*) in case of
|
69
|
+
a connection problem or any other kind of problem occurred in the communication with aria2c.
|
70
|
+
You can check if a valid response is an 'error' one or a 'result' one checking the **error?** method as below
|
71
|
+
|
72
|
+
``` ruby
|
73
|
+
response = aria2.get_version
|
74
|
+
response.error?
|
75
|
+
```
|
76
|
+
|
77
|
+
If a response is an error one, the **error** getter returns you a simple ruby wrapper class of the jsonrpc error
|
78
|
+
block, with **code**, **message** and the optional **data**
|
79
|
+
|
80
|
+
``` ruby
|
81
|
+
|
82
|
+
error = response.error
|
83
|
+
|
84
|
+
error.code # => -32700
|
85
|
+
error.message # => "Parse error"
|
86
|
+
```
|
87
|
+
|
88
|
+
If you get a non-error response, you can access the **result** in form of an hash obtained parsing the json result block
|
89
|
+
in the aria2c response
|
90
|
+
|
91
|
+
``` ruby
|
92
|
+
response.result # => {"gid" => "2089b05ecca3d829", "status" => "active"}
|
93
|
+
```
|
94
|
+
|
29
95
|
|
30
96
|
## Contributing
|
31
97
|
|
@@ -4,6 +4,7 @@ require 'json'
|
|
4
4
|
require 'aria2_driver/json_rpc/connection'
|
5
5
|
require 'aria2_driver/json_rpc/request'
|
6
6
|
require 'aria2_driver/json_rpc/response'
|
7
|
+
require 'aria2_driver/json_rpc/response_exception'
|
7
8
|
|
8
9
|
|
9
10
|
module Aria2Driver
|
@@ -23,21 +24,21 @@ module Aria2Driver
|
|
23
24
|
def request(request)
|
24
25
|
req_hash = request_to_hash(request)
|
25
26
|
http = Net::HTTP.new(connection.host, connection.port)
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
27
|
+
begin
|
28
|
+
http_response = http.request_post(
|
29
|
+
request.path,
|
30
|
+
JSON.generate(req_hash),
|
31
|
+
{
|
32
|
+
'Accept' => 'application/json',
|
33
|
+
'Content-Type' => 'application/json'
|
34
|
+
}
|
35
|
+
)
|
36
|
+
Aria2Driver::JsonRpc::Response.new(JSON.parse(http_response.body))
|
37
|
+
rescue Exception => ex
|
38
|
+
raise Aria2Driver::JsonRpc::ResponseException.new "Connection Error"
|
39
|
+
end
|
35
40
|
end
|
36
41
|
|
37
|
-
def self.from_url(url, options={})
|
38
|
-
uri = URI.parse(url)
|
39
|
-
new uri.host, options.merge({scheme: uri.scheme, port: uri.port, path: uri.path})
|
40
|
-
end
|
41
42
|
|
42
43
|
def method_missing(method, *args)
|
43
44
|
if supported_request?(method)
|
@@ -61,7 +62,7 @@ module Aria2Driver
|
|
61
62
|
end
|
62
63
|
|
63
64
|
def snake_lower_camel(snake)
|
64
|
-
snake.gsub(/(_.)/){ $1.upcase[-1] }
|
65
|
+
snake.gsub(/(_.)/) { $1.upcase[-1] }
|
65
66
|
end
|
66
67
|
|
67
68
|
def generate_uuid
|
data/lib/aria2_driver/version.rb
CHANGED
@@ -21,8 +21,12 @@ module Aria2Driver
|
|
21
21
|
end
|
22
22
|
|
23
23
|
it 'should create from url' do
|
24
|
-
client = Aria2Driver::JsonRpc::Client.
|
25
|
-
|
24
|
+
client = Aria2Driver::JsonRpc::Client.new 'localhost', {
|
25
|
+
id: 'my',
|
26
|
+
scheme: 'https',
|
27
|
+
port: 9090,
|
28
|
+
token: 'abcd-1234'
|
29
|
+
}
|
26
30
|
expect(client.id).to eq('my')
|
27
31
|
expect(client.token).to eq('abcd-1234')
|
28
32
|
expect(client.connection).to have_attributes({
|
@@ -32,15 +36,26 @@ module Aria2Driver
|
|
32
36
|
})
|
33
37
|
end
|
34
38
|
|
39
|
+
|
35
40
|
describe 'requests' do
|
41
|
+
|
42
|
+
let :aria2 do
|
43
|
+
Aria2Driver::JsonRpc::Client.new 'localhost', {
|
44
|
+
id: 'local_client',
|
45
|
+
token: 'abcd-1234'
|
46
|
+
}
|
47
|
+
end
|
48
|
+
|
36
49
|
it 'simple successful generic request' do
|
37
|
-
stubbed_request = Mocks::JsonRpc::GetVersionRequest.new('localhost',
|
50
|
+
stubbed_request = Mocks::JsonRpc::GetVersionRequest.new('localhost',
|
51
|
+
{
|
52
|
+
port: 80,
|
53
|
+
params: ["token:abcd-1234"]
|
54
|
+
})
|
38
55
|
mock_response = Mocks::JsonRpc::GetVersionSuccessfulResponse.new
|
39
56
|
stubbed_request.with_response(mock_response)
|
40
57
|
|
41
|
-
|
42
|
-
'http://localhost:80/jsonrpc', {id: 'local_client', token: 'abcd-1234'})
|
43
|
-
response = client.request(Aria2Driver::JsonRpc::Request.new 'aria2.getVersion')
|
58
|
+
response = aria2.request(Aria2Driver::JsonRpc::Request.new 'aria2.getVersion')
|
44
59
|
|
45
60
|
expect(response.error?).to be_falsey
|
46
61
|
expect(response.result['version']).to eq(mock_response.result['version'])
|
@@ -52,9 +67,7 @@ module Aria2Driver
|
|
52
67
|
mock_error_response = Mocks::JsonRpc::ErrorResponse.new({code: -32700, message: 'Parse error'})
|
53
68
|
stubbed_request.with_response(mock_error_response)
|
54
69
|
|
55
|
-
|
56
|
-
'http://localhost:80/jsonrpc', {id: 'local_client', token: 'abcd-1234'})
|
57
|
-
response = client.request(Aria2Driver::JsonRpc::Request.new 'aria2.getVersion')
|
70
|
+
response = aria2.request(Aria2Driver::JsonRpc::Request.new 'aria2.getVersion')
|
58
71
|
|
59
72
|
expect(response.error?).to be_truthy
|
60
73
|
expect(response.error).to have_attributes({
|
@@ -62,92 +75,91 @@ module Aria2Driver
|
|
62
75
|
message: mock_error_response.message
|
63
76
|
})
|
64
77
|
end
|
65
|
-
end
|
66
78
|
|
67
|
-
|
68
|
-
|
69
|
-
stubbed_request = Mocks::JsonRpc::GetVersionRequest.new('localhost', {port: 80, params: ["token:abcd-1234"]})
|
70
|
-
mock_response = Mocks::JsonRpc::GetVersionSuccessfulResponse.new
|
71
|
-
stubbed_request.with_response(mock_response)
|
72
|
-
|
73
|
-
aria2 = Aria2Driver::JsonRpc::Client.from_url(
|
74
|
-
'http://localhost:80/jsonrpc', {id: 'local_client', token: 'abcd-1234'})
|
75
|
-
|
76
|
-
expect(aria2.respond_to?(:get_version)).to be true
|
79
|
+
it 'handle connection refused on request' do
|
80
|
+
stub_request(:any, 'http://localhost:80/jsonrpc').to_raise(Errno::ECONNREFUSED)
|
77
81
|
|
78
|
-
|
79
|
-
|
80
|
-
expect(response.error?).to be_falsey
|
81
|
-
expect(response.result['version']).to eq(mock_response.result['version'])
|
82
|
-
expect(response.result['enabledFeatures']).to eq(mock_response.result['enabledFeatures'])
|
82
|
+
expect{aria2.request(Aria2Driver::JsonRpc::Request.new 'aria2.getVersion')}
|
83
|
+
.to raise_error(Aria2Driver::JsonRpc::ResponseException)
|
83
84
|
end
|
84
|
-
end
|
85
85
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
params: [
|
92
|
-
"token:abcd-1234",
|
93
|
-
['http://www.example.com/a.jpg'],
|
94
|
-
{"dir" => "/tmp/"}
|
95
|
-
]
|
96
|
-
})
|
97
|
-
mock_response = Mocks::JsonRpc::AddUriSuccessfulResponse.new
|
98
|
-
stubbed_request.with_response(mock_response)
|
86
|
+
describe 'version' do
|
87
|
+
it 'get_version request' do
|
88
|
+
stubbed_request = Mocks::JsonRpc::GetVersionRequest.new('localhost', {port: 80, params: ["token:abcd-1234"]})
|
89
|
+
mock_response = Mocks::JsonRpc::GetVersionSuccessfulResponse.new
|
90
|
+
stubbed_request.with_response(mock_response)
|
99
91
|
|
100
|
-
|
101
|
-
'http://localhost:80/jsonrpc', {id: 'local_client', token: 'abcd-1234'})
|
92
|
+
expect(aria2.respond_to?(:get_version)).to be true
|
102
93
|
|
103
|
-
|
94
|
+
response = aria2.get_version
|
104
95
|
|
105
|
-
|
106
|
-
|
96
|
+
expect(response.error?).to be_falsey
|
97
|
+
expect(response.result['version']).to eq(mock_response.result['version'])
|
98
|
+
expect(response.result['enabledFeatures']).to eq(mock_response.result['enabledFeatures'])
|
99
|
+
end
|
107
100
|
end
|
108
|
-
end
|
109
101
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
102
|
+
describe 'add uri' do
|
103
|
+
it 'add_uri request' do
|
104
|
+
stubbed_request = Mocks::JsonRpc::AddUriRequest.new('localhost',
|
105
|
+
{
|
106
|
+
port: 80,
|
107
|
+
params: [
|
108
|
+
"token:abcd-1234",
|
109
|
+
['http://www.example.com/a.jpg'],
|
110
|
+
{"dir" => "/tmp/"}
|
111
|
+
]
|
112
|
+
})
|
113
|
+
mock_response = Mocks::JsonRpc::AddUriSuccessfulResponse.new
|
114
|
+
stubbed_request.with_response(mock_response)
|
115
|
+
|
116
|
+
response = aria2.add_uri({params: [['http://www.example.com/a.jpg'], {"dir" => "/tmp/"}]})
|
117
|
+
|
118
|
+
expect(response.error?).to be_falsey
|
119
|
+
expect(response.result).to eq(mock_response.result)
|
120
|
+
end
|
128
121
|
end
|
129
122
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
123
|
+
describe 'status' do
|
124
|
+
it 'tell_status simple request' do
|
125
|
+
stubbed_request = Mocks::JsonRpc::TellStatusRequest.new('localhost',
|
126
|
+
{
|
127
|
+
port: 80,
|
128
|
+
params: [
|
129
|
+
"token:abcd-1234",
|
130
|
+
"2089b05ecca3d829"
|
131
|
+
]
|
132
|
+
})
|
133
|
+
mock_response = Mocks::JsonRpc::TellStatusSuccessfulResponse.new
|
134
|
+
stubbed_request.with_response(mock_response)
|
135
|
+
|
136
|
+
response = aria2.tell_status({params: ["2089b05ecca3d829"]})
|
137
|
+
|
138
|
+
expect(response.error?).to be_falsey
|
139
|
+
expect(response.result).to eq(mock_response.result)
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'tell_status request with selected keys' do
|
143
|
+
stubbed_request = Mocks::JsonRpc::TellStatusRequest.new('localhost',
|
144
|
+
{
|
145
|
+
port: 80,
|
146
|
+
params: [
|
147
|
+
"token:abcd-1234",
|
148
|
+
"2089b05ecca3d829",
|
149
|
+
["gid", "status"]
|
150
|
+
]
|
151
|
+
})
|
152
|
+
mock_response = Mocks::JsonRpc::TellStatusSuccessfulResponse.new
|
153
|
+
stubbed_request.with_response(mock_response)
|
154
|
+
|
155
|
+
response = aria2.tell_status({params: ["2089b05ecca3d829", ['gid', 'status']]})
|
156
|
+
|
157
|
+
expect(response.error?).to be_falsey
|
158
|
+
expect(response.result).to eq({"gid" => "2089b05ecca3d829", "status" => "active"})
|
159
|
+
end
|
148
160
|
end
|
149
|
-
|
150
161
|
end
|
151
162
|
end
|
163
|
+
|
152
164
|
end
|
153
165
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aria2_driver
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roberto Ciatti
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-03-
|
12
|
+
date: 2015-03-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -90,8 +90,9 @@ extra_rdoc_files: []
|
|
90
90
|
files:
|
91
91
|
- .gitignore
|
92
92
|
- .travis.yml
|
93
|
+
- .yardopts
|
93
94
|
- Gemfile
|
94
|
-
- LICENSE
|
95
|
+
- LICENSE.md
|
95
96
|
- README.md
|
96
97
|
- Rakefile
|
97
98
|
- aria2_driver.gemspec
|
@@ -101,6 +102,7 @@ files:
|
|
101
102
|
- lib/aria2_driver/json_rpc/error.rb
|
102
103
|
- lib/aria2_driver/json_rpc/request.rb
|
103
104
|
- lib/aria2_driver/json_rpc/response.rb
|
105
|
+
- lib/aria2_driver/json_rpc/response_exception.rb
|
104
106
|
- lib/aria2_driver/version.rb
|
105
107
|
- spec/aria2_driver/json_rpc/client_spec.rb
|
106
108
|
- spec/aria2_driver/json_rpc/connection_spec.rb
|