muxer 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -1
- data/lib/muxer/multiplexer.rb +14 -2
- data/lib/muxer/request.rb +14 -3
- data/lib/muxer/version.rb +1 -1
- data/spec/cassettes/muxer/post/can.yml +38 -0
- data/spec/cassettes/muxer/post/with_params.yml +38 -0
- data/spec/cassettes/muxer/put/can.yml +38 -0
- data/spec/cassettes/muxer/put/with_params.yml +38 -0
- data/spec/muxer/multiplexer_spec.rb +48 -29
- data/spec/muxer_spec.rb +54 -0
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8939fea35a80bfeef5db55769e7a33139fa6b56d
|
4
|
+
data.tar.gz: 25e24c9494a923b310fb3148711f0edb974535b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2dc03106231b3d0878709abfd4fabec810bdf9a322b7636eb048430743dbccde95ab2ca026a0caebb92d6732d5a1ddca6ad46e6e91daa84ac87560ba7e807d6c
|
7
|
+
data.tar.gz: 2e627c3684ceddc10c56a7f6dd2e73be297d1a2e85c0b02eea69d00edc8dd902e84afb9ca40661210452cf97d5407679607155b0f17d5e849bae2d7e14fdc9a0
|
data/.travis.yml
CHANGED
@@ -9,4 +9,8 @@ rvm:
|
|
9
9
|
script: bundle exec rspec spec
|
10
10
|
addons:
|
11
11
|
code_climate:
|
12
|
-
repo_token: 6aa82237af30a07e040f64f0a184915522cdeead2da8df84b3a634bc9f189064
|
12
|
+
repo_token: 6aa82237af30a07e040f64f0a184915522cdeead2da8df84b3a634bc9f189064
|
13
|
+
notifications:
|
14
|
+
email:
|
15
|
+
on_success: never
|
16
|
+
on_failure: change
|
data/lib/muxer/multiplexer.rb
CHANGED
@@ -7,10 +7,22 @@ module Muxer
|
|
7
7
|
@timeout = nil
|
8
8
|
end
|
9
9
|
|
10
|
-
def add_url(url,
|
10
|
+
def add_url(url, options = {})
|
11
|
+
options.keys.each do |key|
|
12
|
+
options[key.to_sym] = options.delete(key)
|
13
|
+
end
|
14
|
+
options = {timeout: nil, method: :get, params: {}}.merge(options)
|
15
|
+
timeout =
|
11
16
|
request = Request.new
|
12
17
|
request.url = url
|
13
|
-
|
18
|
+
options.each do |key, val|
|
19
|
+
next unless request.respond_to? ("#{key}=".to_sym)
|
20
|
+
request.send("#{key}=".to_sym, val) if val
|
21
|
+
end
|
22
|
+
add_request request
|
23
|
+
end
|
24
|
+
|
25
|
+
def add_request(request)
|
14
26
|
requests << request
|
15
27
|
end
|
16
28
|
|
data/lib/muxer/request.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Muxer
|
2
2
|
class Request
|
3
|
-
attr_accessor :url, :timeout, :headers
|
3
|
+
attr_accessor :url, :timeout, :headers, :params
|
4
4
|
attr_reader :method, :completed, :error
|
5
5
|
|
6
6
|
alias_method :completed?, :completed
|
@@ -9,6 +9,7 @@ module Muxer
|
|
9
9
|
@completed = false
|
10
10
|
@timeout = 10
|
11
11
|
@headers = {}
|
12
|
+
@params = {}
|
12
13
|
@request = nil
|
13
14
|
@error = nil
|
14
15
|
end
|
@@ -24,8 +25,16 @@ module Muxer
|
|
24
25
|
connect_timeout: timeout,
|
25
26
|
inactivity_timeout: timeout,
|
26
27
|
)
|
28
|
+
options = {
|
29
|
+
head: headers
|
30
|
+
}
|
31
|
+
if [:post, :put].include? method
|
32
|
+
options[:body] = params
|
33
|
+
else
|
34
|
+
options[:query] = params
|
35
|
+
end
|
27
36
|
@request = http.public_send(method,
|
28
|
-
|
37
|
+
options
|
29
38
|
)
|
30
39
|
|
31
40
|
@request.callback { @completed = true }
|
@@ -34,7 +43,9 @@ module Muxer
|
|
34
43
|
end
|
35
44
|
|
36
45
|
def response
|
37
|
-
@request
|
46
|
+
if @request
|
47
|
+
@request.response
|
48
|
+
end
|
38
49
|
end
|
39
50
|
end
|
40
51
|
end
|
data/lib/muxer/version.rb
CHANGED
@@ -0,0 +1,38 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: post
|
5
|
+
uri: https://httpbin.org/post
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers: {}
|
10
|
+
response:
|
11
|
+
status:
|
12
|
+
code: 200
|
13
|
+
message: OK
|
14
|
+
headers:
|
15
|
+
Server:
|
16
|
+
- nginx
|
17
|
+
Date:
|
18
|
+
- Wed, 15 Apr 2015 16:28:34 GMT
|
19
|
+
Content-Type:
|
20
|
+
- application/json
|
21
|
+
Content-Length:
|
22
|
+
- '326'
|
23
|
+
Connection:
|
24
|
+
- close
|
25
|
+
Access-Control-Allow-Origin:
|
26
|
+
- '*'
|
27
|
+
Access-Control-Allow-Credentials:
|
28
|
+
- 'true'
|
29
|
+
body:
|
30
|
+
encoding: UTF-8
|
31
|
+
string: "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {}, \n \"form\":
|
32
|
+
{}, \n \"headers\": {\n \"Content-Length\": \"0\", \n \"Content-Type\":
|
33
|
+
\"application/x-www-form-urlencoded\", \n \"Host\": \"httpbin.org\", \n
|
34
|
+
\ \"User-Agent\": \"EventMachine HttpClient\"\n }, \n \"json\": null,
|
35
|
+
\n \"origin\": \"72.92.31.249\", \n \"url\": \"https://httpbin.org/post\"\n}\n"
|
36
|
+
http_version:
|
37
|
+
recorded_at: Wed, 15 Apr 2015 16:28:37 GMT
|
38
|
+
recorded_with: VCR 2.9.3
|
@@ -0,0 +1,38 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: post
|
5
|
+
uri: https://httpbin.org/post
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: test=success
|
9
|
+
headers: {}
|
10
|
+
response:
|
11
|
+
status:
|
12
|
+
code: 200
|
13
|
+
message: OK
|
14
|
+
headers:
|
15
|
+
Server:
|
16
|
+
- nginx
|
17
|
+
Date:
|
18
|
+
- Wed, 15 Apr 2015 16:28:34 GMT
|
19
|
+
Content-Type:
|
20
|
+
- application/json
|
21
|
+
Content-Length:
|
22
|
+
- '352'
|
23
|
+
Connection:
|
24
|
+
- close
|
25
|
+
Access-Control-Allow-Origin:
|
26
|
+
- '*'
|
27
|
+
Access-Control-Allow-Credentials:
|
28
|
+
- 'true'
|
29
|
+
body:
|
30
|
+
encoding: UTF-8
|
31
|
+
string: "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {}, \n \"form\":
|
32
|
+
{\n \"test\": \"success\"\n }, \n \"headers\": {\n \"Content-Length\":
|
33
|
+
\"12\", \n \"Content-Type\": \"application/x-www-form-urlencoded\", \n
|
34
|
+
\ \"Host\": \"httpbin.org\", \n \"User-Agent\": \"EventMachine HttpClient\"\n
|
35
|
+
\ }, \n \"json\": null, \n \"origin\": \"72.92.31.249\", \n \"url\": \"https://httpbin.org/post\"\n}\n"
|
36
|
+
http_version:
|
37
|
+
recorded_at: Wed, 15 Apr 2015 16:28:37 GMT
|
38
|
+
recorded_with: VCR 2.9.3
|
@@ -0,0 +1,38 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: put
|
5
|
+
uri: https://httpbin.org/put
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers: {}
|
10
|
+
response:
|
11
|
+
status:
|
12
|
+
code: 200
|
13
|
+
message: OK
|
14
|
+
headers:
|
15
|
+
Server:
|
16
|
+
- nginx
|
17
|
+
Date:
|
18
|
+
- Wed, 15 Apr 2015 16:28:34 GMT
|
19
|
+
Content-Type:
|
20
|
+
- application/json
|
21
|
+
Content-Length:
|
22
|
+
- '325'
|
23
|
+
Connection:
|
24
|
+
- close
|
25
|
+
Access-Control-Allow-Origin:
|
26
|
+
- '*'
|
27
|
+
Access-Control-Allow-Credentials:
|
28
|
+
- 'true'
|
29
|
+
body:
|
30
|
+
encoding: UTF-8
|
31
|
+
string: "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {}, \n \"form\":
|
32
|
+
{}, \n \"headers\": {\n \"Content-Length\": \"0\", \n \"Content-Type\":
|
33
|
+
\"application/x-www-form-urlencoded\", \n \"Host\": \"httpbin.org\", \n
|
34
|
+
\ \"User-Agent\": \"EventMachine HttpClient\"\n }, \n \"json\": null,
|
35
|
+
\n \"origin\": \"72.92.31.249\", \n \"url\": \"https://httpbin.org/put\"\n}\n"
|
36
|
+
http_version:
|
37
|
+
recorded_at: Wed, 15 Apr 2015 16:28:37 GMT
|
38
|
+
recorded_with: VCR 2.9.3
|
@@ -0,0 +1,38 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: put
|
5
|
+
uri: https://httpbin.org/put
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: test=success
|
9
|
+
headers: {}
|
10
|
+
response:
|
11
|
+
status:
|
12
|
+
code: 200
|
13
|
+
message: OK
|
14
|
+
headers:
|
15
|
+
Server:
|
16
|
+
- nginx
|
17
|
+
Date:
|
18
|
+
- Wed, 15 Apr 2015 16:28:34 GMT
|
19
|
+
Content-Type:
|
20
|
+
- application/json
|
21
|
+
Content-Length:
|
22
|
+
- '351'
|
23
|
+
Connection:
|
24
|
+
- close
|
25
|
+
Access-Control-Allow-Origin:
|
26
|
+
- '*'
|
27
|
+
Access-Control-Allow-Credentials:
|
28
|
+
- 'true'
|
29
|
+
body:
|
30
|
+
encoding: UTF-8
|
31
|
+
string: "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {}, \n \"form\":
|
32
|
+
{\n \"test\": \"success\"\n }, \n \"headers\": {\n \"Content-Length\":
|
33
|
+
\"12\", \n \"Content-Type\": \"application/x-www-form-urlencoded\", \n
|
34
|
+
\ \"Host\": \"httpbin.org\", \n \"User-Agent\": \"EventMachine HttpClient\"\n
|
35
|
+
\ }, \n \"json\": null, \n \"origin\": \"72.92.31.249\", \n \"url\": \"https://httpbin.org/put\"\n}\n"
|
36
|
+
http_version:
|
37
|
+
recorded_at: Wed, 15 Apr 2015 16:28:37 GMT
|
38
|
+
recorded_with: VCR 2.9.3
|
@@ -1,49 +1,68 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
class Muxer::TestRequest < Muxer::Request
|
4
|
+
def test_timeout=(timeout)
|
5
|
+
@test_timeout = timeout
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_timeout
|
9
|
+
@test_timeout ||= 0.0001
|
10
|
+
end
|
11
|
+
|
12
|
+
def process!
|
13
|
+
EM::Timer.new(test_timeout) do
|
14
|
+
@completed = true
|
15
|
+
end
|
16
|
+
|
17
|
+
return self
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
3
21
|
RSpec.describe Muxer::Multiplexer do
|
22
|
+
|
23
|
+
def new_request(url, timeout = nil, test_timeout = nil)
|
24
|
+
request = Muxer::TestRequest.new
|
25
|
+
request.url = url
|
26
|
+
request.timeout = timeout if timeout
|
27
|
+
request.test_timeout = test_timeout if test_timeout
|
28
|
+
request
|
29
|
+
end
|
30
|
+
|
4
31
|
let(:multiplexer) { Muxer::Multiplexer.new }
|
5
32
|
|
6
33
|
it 'kills requests with a timeout' do
|
7
|
-
|
8
|
-
|
9
|
-
response = multiplexer.execute
|
34
|
+
multiplexer.add_request(new_request('https://github.com/', 0.0001, 0.01))
|
35
|
+
response = multiplexer.execute
|
10
36
|
|
11
|
-
|
12
|
-
|
13
|
-
end
|
37
|
+
expect(response[:succeeded].count).to eq(0)
|
38
|
+
expect(response[:failed].count).to eq(1)
|
14
39
|
end
|
15
40
|
|
16
41
|
it 'lets a request wait on the longer one' do
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
response = multiplexer.execute
|
42
|
+
multiplexer.add_request(new_request('https://github.com/', 0.0001, 0.0002))
|
43
|
+
multiplexer.add_request(new_request('https://codeclimate.com/', 2, 0.002))
|
44
|
+
response = multiplexer.execute
|
21
45
|
|
22
|
-
|
23
|
-
|
24
|
-
end
|
46
|
+
expect(response[:succeeded].count).to eq(2)
|
47
|
+
expect(response[:failed].count).to eq(0)
|
25
48
|
end
|
26
49
|
|
27
50
|
it 'kills requests with a global timeout' do
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
response = multiplexer.execute
|
51
|
+
multiplexer.add_request(new_request('https://github.com/', nil, 0.002))
|
52
|
+
multiplexer.timeout = 0.0001
|
53
|
+
response = multiplexer.execute
|
32
54
|
|
33
|
-
|
34
|
-
|
35
|
-
end
|
55
|
+
expect(response[:succeeded].count).to eq(0)
|
56
|
+
expect(response[:failed].count).to eq(1)
|
36
57
|
end
|
37
58
|
|
38
59
|
it 'kills one of two requests with a global timeout' do
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
expect(response[:failed].count).to eq(1)
|
47
|
-
end
|
60
|
+
multiplexer.add_request(new_request('https://github.com/', nil, 1.0))
|
61
|
+
multiplexer.add_request(new_request('https://www.google.com/', nil, 0.00001))
|
62
|
+
multiplexer.timeout = 0.0001
|
63
|
+
response = multiplexer.execute
|
64
|
+
|
65
|
+
expect(response[:succeeded].count).to eq(1)
|
66
|
+
expect(response[:failed].count).to eq(1)
|
48
67
|
end
|
49
68
|
end
|
data/spec/muxer_spec.rb
CHANGED
@@ -42,5 +42,59 @@ RSpec.describe Muxer, "execute" do
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
+
it 'can make a POST' do
|
46
|
+
VCR.use_cassette('muxer/post/can') do
|
47
|
+
response = Muxer.execute do |muxer|
|
48
|
+
muxer.add_url 'https://httpbin.org/post', method: :post
|
49
|
+
end
|
50
|
+
expect(response[:succeeded]).to be_kind_of(Array)
|
51
|
+
expect(response[:succeeded].count).to eq(1)
|
52
|
+
|
53
|
+
response_body = JSON.parse(response[:succeeded][0].response)
|
54
|
+
expect(response_body['form']).to eq({})
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'can make a POST with params' do
|
59
|
+
VCR.use_cassette('muxer/post/with_params') do
|
60
|
+
response = Muxer.execute do |muxer|
|
61
|
+
muxer.add_url 'https://httpbin.org/post', method: :post, params: {test: :success}
|
62
|
+
end
|
63
|
+
expect(response[:succeeded]).to be_kind_of(Array)
|
64
|
+
expect(response[:succeeded].count).to eq(1)
|
65
|
+
|
66
|
+
response_body = JSON.parse(response[:succeeded][0].response)
|
67
|
+
expect(response_body['form']).to be_kind_of(Hash)
|
68
|
+
expect(response_body['form']['test']).to eq('success')
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'can make a PUT' do
|
73
|
+
VCR.use_cassette('muxer/put/can') do
|
74
|
+
response = Muxer.execute do |muxer|
|
75
|
+
muxer.add_url 'https://httpbin.org/put', method: :put
|
76
|
+
end
|
77
|
+
expect(response[:succeeded]).to be_kind_of(Array)
|
78
|
+
expect(response[:succeeded].count).to eq(1)
|
79
|
+
|
80
|
+
response_body = JSON.parse(response[:succeeded][0].response)
|
81
|
+
expect(response_body['form']).to eq({})
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'can make a PUT with params' do
|
86
|
+
VCR.use_cassette('muxer/put/with_params') do
|
87
|
+
response = Muxer.execute do |muxer|
|
88
|
+
muxer.add_url 'https://httpbin.org/put', method: :put, params: {test: :success}
|
89
|
+
end
|
90
|
+
expect(response[:succeeded]).to be_kind_of(Array)
|
91
|
+
expect(response[:succeeded].count).to eq(1)
|
92
|
+
|
93
|
+
response_body = JSON.parse(response[:succeeded][0].response)
|
94
|
+
expect(response_body['form']).to be_kind_of(Hash)
|
95
|
+
expect(response_body['form']['test']).to eq('success')
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
45
99
|
|
46
100
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: muxer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris MacNaughton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: eventmachine
|
@@ -160,6 +160,10 @@ files:
|
|
160
160
|
- spec/cassettes/muxer/makes_a_web_request.yml
|
161
161
|
- spec/cassettes/muxer/makes_multiple_web_requests.yml
|
162
162
|
- spec/cassettes/muxer/multiplexer/with_global_timeout.yml
|
163
|
+
- spec/cassettes/muxer/post/can.yml
|
164
|
+
- spec/cassettes/muxer/post/with_params.yml
|
165
|
+
- spec/cassettes/muxer/put/can.yml
|
166
|
+
- spec/cassettes/muxer/put/with_params.yml
|
163
167
|
- spec/muxer/multiplexer_spec.rb
|
164
168
|
- spec/muxer/request_spec.rb
|
165
169
|
- spec/muxer_spec.rb
|
@@ -193,6 +197,10 @@ test_files:
|
|
193
197
|
- spec/cassettes/muxer/makes_a_web_request.yml
|
194
198
|
- spec/cassettes/muxer/makes_multiple_web_requests.yml
|
195
199
|
- spec/cassettes/muxer/multiplexer/with_global_timeout.yml
|
200
|
+
- spec/cassettes/muxer/post/can.yml
|
201
|
+
- spec/cassettes/muxer/post/with_params.yml
|
202
|
+
- spec/cassettes/muxer/put/can.yml
|
203
|
+
- spec/cassettes/muxer/put/with_params.yml
|
196
204
|
- spec/muxer/multiplexer_spec.rb
|
197
205
|
- spec/muxer/request_spec.rb
|
198
206
|
- spec/muxer_spec.rb
|