aria2_driver 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: de5dcdac31c34a26de91ab9d1a937b4071b887cc
4
+ data.tar.gz: 5182d13796e0e10255f1a415535eb84e48cef84f
5
+ SHA512:
6
+ metadata.gz: 50376891c256b04f70b59d72846604e32b5c9b1cd1cd7f490ff3f6ea8447c889e847d54a0ee346482c53239250dc54f98c2e8b845c51f9d4bc61afecf8c5a828
7
+ data.tar.gz: 91fce3fbe30c07057c646285cd1488277c4e114271860bf1a57d04e5b565baa7ed7e377602430ff6bd150c9cf8b3779b88c7725faa8d606b2af8a6c3f317994e
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .DS_Store
11
+ .idea
12
+ *.bundle
13
+ *.so
14
+ *.o
15
+ *.a
16
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,29 @@
1
+ language: ruby
2
+ before_install: gem install bundler
3
+ script: bundle exec rake spec
4
+ addons:
5
+ code_climate:
6
+ repo_token: ab624e4e0be1ac70631beeb8cb61fed99d511a93d8bf437255a4ff94f69e7836
7
+ rvm:
8
+ - 2.0
9
+ - 2.1
10
+ - 2.2
11
+ - jruby
12
+ - ruby-head
13
+ - rbx-2
14
+ matrix:
15
+ allow_failures:
16
+ - rvm: rbx-2
17
+ - rvm: ruby-head
18
+ notifications:
19
+ email:
20
+ - roberto.ciatti@overhilllab.com
21
+ - matteo.foccoli@overhilllab.com
22
+ deploy:
23
+ provider: rubygems
24
+ api_key:
25
+ secure: f0JeJrfONLwfwYA2B8MZWNZ0yr7dZn9wtgZKKqP20F5qTjswlPSMndU3/WwbHoYYbCcmgj30zDDld4IBcoGofh+bSSPwSmgtm05+cKYakKiHz8Hqja0ygcxt9w+T7IC8anH0HdNd6DGKvXOBKC5x4w5xGeAiKRmYNGpm+htX6ZA=
26
+ gem: aria2_driver
27
+ on:
28
+ tags: true
29
+ repo: OverHillLab/aria2_driver
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aria2_driver.gemspec
4
+ gemspec
5
+
6
+ gem 'codeclimate-test-reporter', group: :test, require: false
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 OverHill Lab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Aria2Driver
2
+
3
+ Simple api to manage aria2c via api
4
+
5
+ # Status
6
+ [![Build Status](http://img.shields.io/travis/OverHillLab/aria2_driver/master.svg)](https://travis-ci.org/OverHillLab/aria2_driver?branch=master)
7
+ [![Code Climate](http://img.shields.io/codeclimate/github/OverHillLab/aria2_driver.svg)](https://codeclimate.com/github/OverHillLab/aria2_driver)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'aria2_driver'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install aria2_driver
24
+
25
+ ## Usage
26
+
27
+ TODO: Write usage instructions here
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it ( https://github.com/[my-github-username]/aria2_driver/fork )
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |task|
5
+ task.rspec_opts = ['--color']
6
+ end
7
+
8
+ task default: :spec
9
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'aria2_driver/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aria2_driver"
8
+ spec.version = Aria2Driver::VERSION
9
+ spec.authors = ['Roberto Ciatti', 'Matteo Foccoli']
10
+ spec.email = ['info@overhilllab.com']
11
+ spec.summary = 'Simple api to manage aria2c via api'
12
+ spec.description = ''
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency 'rspec'
24
+ spec.add_development_dependency 'json_spec'
25
+ spec.add_development_dependency 'webmock'
26
+ end
@@ -0,0 +1,14 @@
1
+ require 'aria2_driver/version'
2
+
3
+ require 'aria2_driver/json_rpc/client'
4
+ require 'aria2_driver/json_rpc/connection'
5
+ require 'aria2_driver/json_rpc/client'
6
+ require 'aria2_driver/json_rpc/request'
7
+ require 'aria2_driver/json_rpc/response'
8
+ require 'aria2_driver/json_rpc/error'
9
+
10
+ module Aria2Driver
11
+
12
+
13
+
14
+ end
@@ -0,0 +1,75 @@
1
+ require 'securerandom'
2
+ require 'json'
3
+
4
+ module Aria2Driver
5
+ module JsonRpc
6
+ class Client
7
+
8
+ attr_reader :id, :connection, :token
9
+
10
+ def initialize(host, options={})
11
+ @id = options[:id] || generate_uuid
12
+ @token = options[:token]
13
+ options.delete :id
14
+ options.delete :token
15
+ @connection = Aria2Driver::JsonRpc::Connection.new host, options
16
+ end
17
+
18
+ def request(request)
19
+ req_hash = request_to_hash(request)
20
+ http = Net::HTTP.new(connection.host, connection.port)
21
+ http_response = http.request_post(
22
+ request.path,
23
+ JSON.generate(req_hash),
24
+ {
25
+ 'Accept' => 'application/json',
26
+ 'Content-Type' => 'application/json'
27
+ }
28
+ )
29
+ Aria2Driver::JsonRpc::Response.new(JSON.parse(http_response.body))
30
+ end
31
+
32
+ def self.from_url(url, options={})
33
+ uri = URI.parse(url)
34
+ new uri.host, options.merge({scheme: uri.scheme, port: uri.port, path: uri.path})
35
+ end
36
+
37
+ def method_missing(method, *args)
38
+ if supported_request?(method)
39
+ rpc_method = snake_lower_camel method.to_s
40
+ if args.any?
41
+ request Aria2Driver::JsonRpc::Request.new "aria2.#{rpc_method}", args[0]
42
+ else
43
+ request Aria2Driver::JsonRpc::Request.new "aria2.#{rpc_method}"
44
+ end
45
+ end
46
+ end
47
+
48
+ def respond_to_missing?(method, include_private = false)
49
+ supported_request?(method)
50
+ end
51
+
52
+ private
53
+
54
+ def supported_request?(request)
55
+ [:get_version, :add_uri].include?(request)
56
+ end
57
+
58
+ def snake_lower_camel(snake)
59
+ snake.gsub(/(_.)/){ $1.upcase[-1] }
60
+ end
61
+
62
+ def generate_uuid
63
+ SecureRandom.uuid
64
+ end
65
+
66
+ def request_to_hash(request)
67
+ req_hash = request.to_hash
68
+ req_hash[:params].insert(0, "token:#{token}")
69
+ req_hash[:id] = id
70
+ req_hash
71
+ end
72
+
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,27 @@
1
+
2
+ module Aria2Driver
3
+ module JsonRpc
4
+ class Connection
5
+
6
+ DEFAULTS = {
7
+ scheme: 'http',
8
+ port: 80
9
+ }
10
+
11
+ attr_reader :scheme, :port, :host
12
+
13
+ def initialize(host, options={})
14
+ @host = host
15
+ check_defaults(options)
16
+ end
17
+
18
+ private
19
+
20
+ def check_defaults(options)
21
+ @scheme = options.fetch(:scheme, DEFAULTS[:scheme])
22
+ @port = options.fetch(:port, DEFAULTS[:port])
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,14 @@
1
+
2
+ module Aria2Driver
3
+ module JsonRpc
4
+ class Error
5
+ attr_reader :message, :code, :data
6
+
7
+ def initialize(error_hash)
8
+ @code = error_hash['code']
9
+ @message = error_hash['message']
10
+ @data = error_hash['data']
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,28 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ module Aria2Driver
5
+ module JsonRpc
6
+ class Request
7
+
8
+ DEFAULT_PATH = '/jsonrpc'
9
+ JSON_RPC_VERSION = '2.0'
10
+
11
+ attr_reader :rpc_method, :params, :path
12
+
13
+ def initialize(rpc_method, options={})
14
+ @path = options[:path] || DEFAULT_PATH
15
+ @rpc_method = rpc_method
16
+ @params = options[:params] || []
17
+ end
18
+
19
+ def to_hash
20
+ {
21
+ jsonrpc: JSON_RPC_VERSION,
22
+ method: rpc_method,
23
+ params: params
24
+ }
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,35 @@
1
+ module Aria2Driver
2
+ module JsonRpc
3
+ class Response
4
+ attr_reader :jsonrpc, :id, :result, :error
5
+
6
+ def initialize(response_hash)
7
+ @jsonrpc = response_hash['jsonrpc']
8
+ @id = response_hash['id']
9
+ detect_payload(response_hash)
10
+ end
11
+
12
+ def error?
13
+ error != nil
14
+ end
15
+
16
+ private
17
+
18
+ def result=(result_payload)
19
+ @result = result_payload
20
+ end
21
+
22
+ def error=(error_payload)
23
+ @error = Aria2Driver::JsonRpc::Error.new error_payload
24
+ end
25
+
26
+ def detect_payload(response_hash)
27
+ if response_hash['error']
28
+ self.error = response_hash['error']
29
+ else
30
+ self.result = response_hash['result']
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module Aria2Driver
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,107 @@
1
+ require 'spec_helper'
2
+ require 'support/mocks/json_rpc/get_version'
3
+ require 'support/mocks/json_rpc/add_uri'
4
+
5
+ module Aria2Driver
6
+ module JsonRpc
7
+
8
+
9
+ describe Client do
10
+
11
+ it 'should create with defaults' do
12
+ client = Aria2Driver::JsonRpc::Client.new 'localhost', {id: 'my', token: 'abcd-1234'}
13
+ expect(client.id).to eq('my')
14
+ expect(client.token).to eq('abcd-1234')
15
+ expect(client.connection).to have_attributes({
16
+ scheme: Aria2Driver::JsonRpc::Connection::DEFAULTS[:scheme],
17
+ host: 'localhost',
18
+ port: Aria2Driver::JsonRpc::Connection::DEFAULTS[:port]
19
+ })
20
+ end
21
+
22
+ it 'should create from url' do
23
+ client = Aria2Driver::JsonRpc::Client.from_url(
24
+ 'https://localhost:9090/jsonrpc', {id: 'my', token: 'abcd-1234'})
25
+ expect(client.id).to eq('my')
26
+ expect(client.token).to eq('abcd-1234')
27
+ expect(client.connection).to have_attributes({
28
+ scheme: 'https',
29
+ host: 'localhost',
30
+ port: 9090
31
+ })
32
+ end
33
+
34
+ describe 'requests' do
35
+ it 'simple successful generic request' do
36
+ stubbed_request = Mocks::JsonRpc::GetVersionRequest.new('localhost', {port: 80, params: ["token:abcd-1234"]})
37
+ mock_response = Mocks::JsonRpc::GetVersionSuccessfulResponse.new
38
+ stubbed_request.with_response(mock_response)
39
+
40
+ client = Aria2Driver::JsonRpc::Client.from_url(
41
+ 'https://localhost:80/jsonrpc', {id: 'local_client', token: 'abcd-1234'})
42
+ response = client.request(Aria2Driver::JsonRpc::Request.new 'aria2.getVersion')
43
+
44
+ expect(response.error?).to be_falsey
45
+ expect(response.result['version']).to eq(mock_response.result[:version])
46
+ expect(response.result['enabledFeatures']).to eq(mock_response.result[:enabledFeatures])
47
+ end
48
+
49
+ it 'simple unsuccessful generic request' do
50
+ stubbed_request = Mocks::JsonRpc::GetVersionRequest.new('localhost', {port: 80, params: ["token:abcd-1234"]})
51
+ mock_error_response = Mocks::JsonRpc::ErrorResponse.new({code: -32700, message: 'Parse error'})
52
+ stubbed_request.with_response(mock_error_response)
53
+
54
+ client = Aria2Driver::JsonRpc::Client.from_url(
55
+ 'https://localhost:80/jsonrpc', {id: 'local_client', token: 'abcd-1234'})
56
+ response = client.request(Aria2Driver::JsonRpc::Request.new 'aria2.getVersion')
57
+
58
+ expect(response.error?).to be_truthy
59
+ expect(response.error).to have_attributes({
60
+ code: mock_error_response.code,
61
+ message: mock_error_response.message
62
+ })
63
+ end
64
+
65
+ it 'get_version request' do
66
+ stubbed_request = Mocks::JsonRpc::GetVersionRequest.new('localhost', {port: 80, params: ["token:abcd-1234"]})
67
+ mock_response = Mocks::JsonRpc::GetVersionSuccessfulResponse.new
68
+ stubbed_request.with_response(mock_response)
69
+
70
+ aria2 = Aria2Driver::JsonRpc::Client.from_url(
71
+ 'https://localhost:80/jsonrpc', {id: 'local_client', token: 'abcd-1234'})
72
+
73
+ expect(aria2.respond_to?(:get_version)).to be true
74
+
75
+ response = aria2.get_version
76
+
77
+ expect(response.error?).to be_falsey
78
+ expect(response.result['version']).to eq(mock_response.result[:version])
79
+ expect(response.result['enabledFeatures']).to eq(mock_response.result[:enabledFeatures])
80
+ end
81
+
82
+ it 'add_uri request' do
83
+ stubbed_request = Mocks::JsonRpc::AddUriRequest.new('localhost',
84
+ {
85
+ port: 80,
86
+ params: [
87
+ "token:abcd-1234",
88
+ ['http://www.example.com/a.jpg'],
89
+ {"dir" => "/tmp/"}
90
+ ]
91
+ })
92
+ mock_response = Mocks::JsonRpc::AddUriSuccessfulResponse.new
93
+ stubbed_request.with_response(mock_response)
94
+
95
+ aria2 = Aria2Driver::JsonRpc::Client.from_url(
96
+ 'https://localhost:80/jsonrpc', {id: 'local_client', token: 'abcd-1234'})
97
+
98
+ response = aria2.add_uri({params: [['http://www.example.com/a.jpg'], {"dir" => "/tmp/"}]})
99
+
100
+ expect(response.error?).to be_falsey
101
+ expect(response.result).to eq(mock_response.result)
102
+ end
103
+
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ module Aria2Driver
4
+ module JsonRpc
5
+ describe Connection do
6
+ it 'should create a new connection with defaults parameters' do
7
+ conn = Connection.new 'localhost'
8
+ expect(conn).to have_attributes({
9
+ scheme: 'http',
10
+ host: 'localhost',
11
+ port: 80
12
+ })
13
+ end
14
+
15
+ it 'should override defaults' do
16
+ conn = Connection.new 'example', {scheme: 'https', host: 'example', port: 9090}
17
+ expect(conn).to have_attributes({
18
+ scheme: 'https',
19
+ host: 'example',
20
+ port: 9090
21
+ })
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ module Aria2Driver
4
+ module JsonRpc
5
+ describe Error do
6
+
7
+ let :error_payload do
8
+ {
9
+ 'code' => 25,
10
+ 'message' => 'message',
11
+ 'data' => {'data' => 'error data'}
12
+ }
13
+ end
14
+
15
+ it 'should create a new error from hash' do
16
+ error = Error.new error_payload
17
+ expect(error).to have_attributes({
18
+ code: 25,
19
+ message: 'message',
20
+ data: {'data' => 'error data'}
21
+ })
22
+ end
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ module Aria2Driver
4
+ module JsonRpc
5
+ describe Request do
6
+
7
+ let :method do
8
+ 'aria2.method'
9
+ end
10
+
11
+ let :params do
12
+ ['sample_gid', {max_download_limit: '10K'}]
13
+ end
14
+
15
+ subject do
16
+ Aria2Driver::JsonRpc::Request.new method, {params: params}
17
+ end
18
+
19
+ it 'should create a new request' do
20
+ expect(subject).not_to be_nil
21
+ expect(subject.path).to eq(Aria2Driver::JsonRpc::Request::DEFAULT_PATH)
22
+ expect(subject.to_hash).to eq({
23
+ jsonrpc: '2.0',
24
+ method: method,
25
+ params: params
26
+ })
27
+ end
28
+ end
29
+ end
30
+
31
+
32
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ module Aria2Driver
4
+ module JsonRpc
5
+ describe Response do
6
+
7
+ let :successful_response do
8
+ {
9
+ 'id' => 'local_client',
10
+ 'jsonrpc' => '2.0',
11
+ 'result' => {
12
+ 'enabledFeatures' => ['BitTorrent','Gzip'],
13
+ 'version' => '1.18.9'
14
+ }
15
+ }
16
+ end
17
+
18
+ let :error_response do
19
+ {
20
+ 'id' => 'local_client',
21
+ 'jsonrpc' => '2.0',
22
+ 'error' => {
23
+ 'code' => 3600,
24
+ 'message' => 'invalid request'
25
+ }
26
+ }
27
+ end
28
+
29
+ it 'should build a response with a result' do
30
+ response = Aria2Driver::JsonRpc::Response.new successful_response
31
+
32
+ expect(response.id).to eq('local_client')
33
+ expect(response.jsonrpc).to eq('2.0')
34
+ expect(response.error?).to be_falsey
35
+ expect(response.error).to be_nil
36
+
37
+ result = response.result
38
+
39
+ expect(result).not_to be_nil
40
+ expect(result['version']).to eq('1.18.9')
41
+ expect(result['enabledFeatures']).to eq(['BitTorrent','Gzip'])
42
+ end
43
+
44
+ it 'should build a response with a error' do
45
+ response = Aria2Driver::JsonRpc::Response.new error_response
46
+
47
+ expect(response.id).to eq('local_client')
48
+ expect(response.jsonrpc).to eq('2.0')
49
+ expect(response.error?).to be_truthy
50
+ expect(response.result).to be_nil
51
+
52
+ error = response.error
53
+
54
+ expect(error).not_to be_nil
55
+ expect(error.code).to eq(error_response['error']['code'])
56
+ expect(error.message).to eq(error_response['error']['message'])
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,19 @@
1
+ if RUBY_ENGINE == "rbx"
2
+ require "codeclimate-test-reporter"
3
+ CodeClimate::TestReporter.start
4
+ end
5
+
6
+ require 'json_spec'
7
+ require 'webmock/rspec'
8
+
9
+ require 'aria2_driver'
10
+
11
+ RSpec.configure do |config|
12
+ config.include JsonSpec::Helpers
13
+ config.mock_with :rspec
14
+
15
+ # Re-Enable connection to codeclimate to post coverage report
16
+ config.after(:suite) do
17
+ WebMock.disable_net_connect!(:allow => 'codeclimate.com')
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ require_relative './request'
2
+ require_relative './response'
3
+
4
+ module Mocks
5
+ module JsonRpc
6
+
7
+ class AddUriSuccessfulResponse < SuccessfulResponse
8
+ def result
9
+ "2089b05ecca3d829"
10
+ end
11
+ end
12
+
13
+ class AddUriRequest < Request
14
+
15
+ protected
16
+
17
+ def rpc_method
18
+ 'aria2.addUri'
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,30 @@
1
+ require_relative './request'
2
+ require_relative './response'
3
+
4
+ module Mocks
5
+ module JsonRpc
6
+
7
+ class GetVersionSuccessfulResponse < SuccessfulResponse
8
+ def result
9
+ {
10
+ enabledFeatures: ["BitTorrent",
11
+ "Firefox3 Cookie",
12
+ "GZip", "HTTPS",
13
+ "Message Digest",
14
+ "Metalink", "XML-RPC"],
15
+ version: "1.18.9"
16
+ }
17
+ end
18
+ end
19
+
20
+ class GetVersionRequest < Request
21
+
22
+ protected
23
+
24
+ def rpc_method
25
+ 'aria2.getVersion'
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,78 @@
1
+ require 'webmock/rspec'
2
+
3
+ module Mocks
4
+ module JsonRpc
5
+
6
+ class Request
7
+ attr_reader :request_method, :uri, :body, :headers, :params,
8
+ :client_id, :json_rpc_version, :stubbed_request
9
+
10
+ DEFAULT_HEADERS = {
11
+ 'Accept' => 'application/json',
12
+ 'Content-Type' => 'application/json'
13
+ }
14
+ DEFAULT_SCHEME = 'http'
15
+ DEFAULT_PORT = 80
16
+ DEFAULT_PATH = 'jsonrpc'
17
+ DEFAULT_CLIENT_ID = 'fake_id'
18
+ DEFAULT_JSON_RPC_VERSION = '2.0'
19
+
20
+
21
+ def initialize(host, options={})
22
+ @request_method = options[:method] || :post
23
+ @uri = build_uri(
24
+ host, options[:path] || DEFAULT_PATH,
25
+ options[:port] || DEFAULT_PORT,
26
+ options[:scheme] || DEFAULT_SCHEME)
27
+ @params = options[:params] || []
28
+ @client_id = options[:client_id] || DEFAULT_CLIENT_ID
29
+ @json_rpc_version = options[:json_rpc_version] || DEFAULT_JSON_RPC_VERSION
30
+ set_headers DEFAULT_HEADERS
31
+ stub_request
32
+ end
33
+
34
+ def with_response(response)
35
+ request rpc_method, response
36
+ end
37
+
38
+
39
+ protected
40
+
41
+ def rpc_method
42
+ raise NotImplementedError
43
+ end
44
+
45
+ private
46
+
47
+ def request(rpc, response)
48
+ set_body(rpc, params) if !params.empty?
49
+ response.stub_for self
50
+ end
51
+
52
+ def build_uri(host, path, port, scheme)
53
+ "#{scheme}://#{host}:#{port}/#{path}"
54
+ end
55
+
56
+ def set_body(rpc_method, params)
57
+ @body = JSON.generate({
58
+ jsonrpc: '2.0',
59
+ method: rpc_method,
60
+ params: params,
61
+ id: 'local_client'
62
+ })
63
+ end
64
+
65
+ def stub_request
66
+ @stubbed_request = WebMock::API.stub_request(
67
+ request_method, uri
68
+ )
69
+ end
70
+
71
+ def set_headers(headers)
72
+ @headers = headers
73
+ end
74
+ end
75
+ end
76
+ end
77
+
78
+
@@ -0,0 +1,89 @@
1
+ module Mocks
2
+ module JsonRpc
3
+ class Response
4
+
5
+ attr_reader :request
6
+
7
+ def stub_for(request)
8
+ @request = request
9
+ stubbed_request = request.stubbed_request
10
+ with_hash = {}
11
+ with_hash[:body] = request.body if request.body
12
+ with_hash[:headers] = request.headers if request.headers
13
+
14
+ stubbed_request.with(with_hash) if !with_hash.empty?
15
+
16
+ stubbed_request.to_return({
17
+ status: response_status(),
18
+ body: response_body()
19
+ })
20
+ end
21
+
22
+ protected
23
+
24
+ def response_body
25
+ raise NotImplementedError
26
+ end
27
+
28
+ def response_status
29
+ raise NotImplementedError
30
+ end
31
+ end
32
+
33
+ class SuccessfulResponse < Response
34
+
35
+ def initialize(options={})
36
+ @result = options[:result] || {}
37
+ end
38
+
39
+ def result
40
+ @result
41
+ end
42
+
43
+ protected
44
+
45
+ def response_body
46
+ JSON.generate({
47
+ jsonrpc: request.json_rpc_version,
48
+ result: result,
49
+ client_id: request.client_id
50
+ })
51
+ end
52
+
53
+ def response_status
54
+ 200
55
+ end
56
+
57
+ end
58
+
59
+ class ErrorResponse < Response
60
+
61
+ attr_reader :message, :code, :data
62
+
63
+ DEFAULT_ERROR_CODE = -3200
64
+ DEFAULT_ERROR_MESSAGE = 'Server error'
65
+
66
+ def initialize(options={})
67
+ @code = options[:code] || DEFAULT_ERROR_CODE
68
+ @message = options[:message] || DEFAULT_ERROR_MESSAGE
69
+ @data = options[:data]
70
+ end
71
+
72
+ def response_body
73
+ JSON.generate({
74
+ jsonrpc: request.json_rpc_version,
75
+ error: {}.tap do |error|
76
+ error[:code] = code
77
+ error[:message] = message
78
+ error[:data] = data if data
79
+ end,
80
+ client_id: request.client_id
81
+ })
82
+ end
83
+
84
+ def response_status
85
+ 200
86
+ end
87
+ end
88
+ end
89
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aria2_driver
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Roberto Ciatti
8
+ - Matteo Foccoli
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-03-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.7'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.7'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: json_spec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: webmock
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ description: ''
85
+ email:
86
+ - info@overhilllab.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - aria2_driver.gemspec
98
+ - lib/aria2_driver.rb
99
+ - lib/aria2_driver/json_rpc/client.rb
100
+ - lib/aria2_driver/json_rpc/connection.rb
101
+ - lib/aria2_driver/json_rpc/error.rb
102
+ - lib/aria2_driver/json_rpc/request.rb
103
+ - lib/aria2_driver/json_rpc/response.rb
104
+ - lib/aria2_driver/version.rb
105
+ - spec/aria2_driver/json_rpc/client_spec.rb
106
+ - spec/aria2_driver/json_rpc/connection_spec.rb
107
+ - spec/aria2_driver/json_rpc/error_spec.rb
108
+ - spec/aria2_driver/json_rpc/request_spec.rb
109
+ - spec/aria2_driver/json_rpc/response_spec.rb
110
+ - spec/spec_helper.rb
111
+ - spec/support/mocks/json_rpc/add_uri.rb
112
+ - spec/support/mocks/json_rpc/get_version.rb
113
+ - spec/support/mocks/json_rpc/request.rb
114
+ - spec/support/mocks/json_rpc/response.rb
115
+ homepage: ''
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.4.5
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Simple api to manage aria2c via api
139
+ test_files:
140
+ - spec/aria2_driver/json_rpc/client_spec.rb
141
+ - spec/aria2_driver/json_rpc/connection_spec.rb
142
+ - spec/aria2_driver/json_rpc/error_spec.rb
143
+ - spec/aria2_driver/json_rpc/request_spec.rb
144
+ - spec/aria2_driver/json_rpc/response_spec.rb
145
+ - spec/spec_helper.rb
146
+ - spec/support/mocks/json_rpc/add_uri.rb
147
+ - spec/support/mocks/json_rpc/get_version.rb
148
+ - spec/support/mocks/json_rpc/request.rb
149
+ - spec/support/mocks/json_rpc/response.rb