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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NTZiM2QzYWFiODhhNDI3ZjRkYTJhM2FiM2VkNzMwMjUzMmNkNDZhYQ==
4
+ M2ZlMzFiMDg2MWYxNzA1MGMwMThiZDY5YjdhZDkxYjRlMjVhOGY2MQ==
5
5
  data.tar.gz: !binary |-
6
- NzdjMjZlMDY0NjAyNDc3Njc5MjM3MjlmOWZlOWJmYjE5NTllMTMzMg==
6
+ NjhkZWM1OTkwZmJkMGMwMDE1OTJhOGFlNTgwZGJmODMzNDBiODUyZA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YzI0ZDBmZGFlZTQyOTE2MzlmYzA2YWZjZjZkYzM1ZTg5Y2U0ZmZlNjhiYjlm
10
- ZmE5NGZkMjkyOTAxYmEyMzA2NDVjYmI5NDY1OWVjNTIyYWVjOTYzNDZlZmM5
11
- ZDQxNzU1ZDU1MmExYmYxNTM2NjA5YTk2MTViMWQ1NTE0NDY5M2U=
9
+ Yjc1MjRkOWM3MGZkNDBhYTZjZDRhODdiMGU0MGY2NTNmZmFlNDZiNzdmMDBk
10
+ Mjk0YmQ4ZTJiYTRhMThmOTc4NGUwOTk2YWVjYjJhOGY4NDg3OWUzMmM4YjFh
11
+ MWI3MGJlMzEyOTczZmUzNjRmMzNkOTI4NmI0NTRlNTc3YTM3MTQ=
12
12
  data.tar.gz: !binary |-
13
- M2Q0OGMxZjE3NjQ1MDY5Nzc4ZTY1ZjZlOWRiZWQ0ZGZkMjRkN2Q4ZTEzNDM4
14
- ZWVlM2U4NzVkZDYzOGU0OThhYjZhNWY2YWE2YjJhNTg0MTgyYmM0Nzg0MDI2
15
- ZDM0YzdhMWRhZGMwNGVlYjc1NjJlZTUwMWM5ODBjMjVlZTQ3MjQ=
13
+ NmU0Njk2NTkzYTEyZTVkNTFiNDMzMDY5ZjVmOGVkMjI5ODkzMGMyMDk1YWY5
14
+ YmUwM2U5MTAzZTU3NTkzNGE5ZDIwNGJmMGJkMDgwNjBkZmI1MzNlODA1MzJj
15
+ ODk1OTUxY2NhYzI4YmViMjA3Nzk2ZDJiOGJlMzI3YjM1NDNkODI=
data/.yardopts ADDED
@@ -0,0 +1,4 @@
1
+ -
2
+ README.md
3
+ LICENSE.md
4
+ lib/**/*.rb
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
- TODO: Write usage instructions here
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
- http_response = http.request_post(
27
- request.path,
28
- JSON.generate(req_hash),
29
- {
30
- 'Accept' => 'application/json',
31
- 'Content-Type' => 'application/json'
32
- }
33
- )
34
- Aria2Driver::JsonRpc::Response.new(JSON.parse(http_response.body))
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
@@ -0,0 +1,9 @@
1
+ module Aria2Driver
2
+ module JsonRpc
3
+ class ResponseException < StandardError
4
+ def initialize(message)
5
+ super(message)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module Aria2Driver
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -21,8 +21,12 @@ module Aria2Driver
21
21
  end
22
22
 
23
23
  it 'should create from url' do
24
- client = Aria2Driver::JsonRpc::Client.from_url(
25
- 'https://localhost:9090/jsonrpc', {id: 'my', token: 'abcd-1234'})
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', {port: 80, params: ["token:abcd-1234"]})
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
- client = Aria2Driver::JsonRpc::Client.from_url(
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
- client = Aria2Driver::JsonRpc::Client.from_url(
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
- describe 'version' do
68
- it 'get_version request' do
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
- response = aria2.get_version
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
- describe 'add uri' do
87
- it 'add_uri request' do
88
- stubbed_request = Mocks::JsonRpc::AddUriRequest.new('localhost',
89
- {
90
- port: 80,
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
- aria2 = Aria2Driver::JsonRpc::Client.from_url(
101
- 'http://localhost:80/jsonrpc', {id: 'local_client', token: 'abcd-1234'})
92
+ expect(aria2.respond_to?(:get_version)).to be true
102
93
 
103
- response = aria2.add_uri({params: [['http://www.example.com/a.jpg'], {"dir" => "/tmp/"}]})
94
+ response = aria2.get_version
104
95
 
105
- expect(response.error?).to be_falsey
106
- expect(response.result).to eq(mock_response.result)
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
- describe 'status' do
111
- it 'tell_status simple request' do
112
- stubbed_request = Mocks::JsonRpc::TellStatusRequest.new('localhost',
113
- {
114
- port: 80,
115
- params: [
116
- "token:abcd-1234",
117
- "2089b05ecca3d829"
118
- ]
119
- })
120
- mock_response = Mocks::JsonRpc::TellStatusSuccessfulResponse.new
121
- stubbed_request.with_response(mock_response)
122
-
123
- aria2 = Aria2Driver::JsonRpc::Client.new 'localhost', {port: 80, id: 'local_client', token: 'abcd-1234'}
124
- response = aria2.tell_status({params: ["2089b05ecca3d829"]})
125
-
126
- expect(response.error?).to be_falsey
127
- expect(response.result).to eq(mock_response.result)
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
- it 'tell_status request with selected keys' do
131
- stubbed_request = Mocks::JsonRpc::TellStatusRequest.new('localhost',
132
- {
133
- port: 80,
134
- params: [
135
- "token:abcd-1234",
136
- "2089b05ecca3d829",
137
- ["gid", "status"]
138
- ]
139
- })
140
- mock_response = Mocks::JsonRpc::TellStatusSuccessfulResponse.new
141
- stubbed_request.with_response(mock_response)
142
-
143
- aria2 = Aria2Driver::JsonRpc::Client.new 'localhost', {port: 80, id: 'local_client', token: 'abcd-1234'}
144
- response = aria2.tell_status({params: ["2089b05ecca3d829", ['gid', 'status']]})
145
-
146
- expect(response.error?).to be_falsey
147
- expect(response.result).to eq({"gid"=>"2089b05ecca3d829", "status"=>"active"})
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
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-05 00:00:00.000000000 Z
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