muxer 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -0
- data/lib/muxer.rb +25 -0
- data/lib/muxer/multiplexer.rb +45 -0
- data/lib/muxer/request.rb +33 -0
- data/lib/muxer/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5b6bf490074eecb77f38826926df3d98ed41c346
|
4
|
+
data.tar.gz: 6aa3894bd41fd4e01b7ed9dc4873443f8c741196
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b816181726162c1b342811f43d8e4f8f23f9772cf69728a7e9621e0f7ecd90efb3adf2ad52c3f5c8039481bac9f3349706c55233c5f23347feab71f8ebb8ba3c
|
7
|
+
data.tar.gz: 3375538c04cd1793dd4364a8def79be7689a241c14ba7dc2265ebe5d83348481c6371c7fe9b354665b7eb635fe014a0173322335c643363426a32286b419db73
|
data/README.md
CHANGED
@@ -4,6 +4,7 @@
|
|
4
4
|
[![Coverage](https://img.shields.io/codeclimate/coverage/github/ChrisMacNaughton/muxer.svg?style=flat-square)](https://codeclimate.com/github/ChrisMacNaughton/muxer)
|
5
5
|
[![Build](https://img.shields.io/travis-ci/ChrisMacNaughton/muxer.svg?style=flat-square)](https://travis-ci.org/ChrisMacNaughton/muxer)
|
6
6
|
[![Dependencies](https://img.shields.io/gemnasium/ChrisMacNaughton/muxer.svg?style=flat-square)](https://gemnasium.com/ChrisMacNaughton/muxer)
|
7
|
+
[![Docs](https://inch-ci.org/github/ChrisMacNaughton/muxer.svg?branch=master)](http://inch-ci.org/github/ChrisMacNaughton/muxer/branch/master)
|
7
8
|
[![Downloads](https://img.shields.io/gem/dtv/muxer.svg?style=flat-square)](https://rubygems.org/gems/muxer)
|
8
9
|
[![Tags](https://img.shields.io/github/tag/ChrisMacNaughton/muxer.svg?style=flat-square)](https://github.com/ChrisMacNaughton/muxer/tags)
|
9
10
|
[![Releases](https://img.shields.io/github/release/ChrisMacNaughton/muxer.svg?style=flat-square)](https://github.com/ChrisMacNaughton/muxer/releases)
|
data/lib/muxer.rb
CHANGED
@@ -5,7 +5,32 @@ require 'muxer/version'
|
|
5
5
|
require 'muxer/request'
|
6
6
|
require 'muxer/multiplexer'
|
7
7
|
|
8
|
+
# Muxer is a parallel request module that allows timeouts for each
|
9
|
+
# individual request as well as a global timeout for all requests.
|
10
|
+
# In addition to the specific timeouts, Muxer will allow a pending
|
11
|
+
# request to continue pending even if its timeout has passed as long
|
12
|
+
# as there is still a request waiting that has a longer timeout
|
13
|
+
|
8
14
|
module Muxer
|
15
|
+
# Example Usage:
|
16
|
+
#
|
17
|
+
# response = Muxer.execute do |muxer|
|
18
|
+
# muxer.add_url "http://www.rubydoc.info", timeout: 0.5
|
19
|
+
# muxer.add_url "https://www.google.com", timeout: 0.25
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# In the above example, Google's response could continue pending if
|
23
|
+
# rubydoc.info had not yet returned or passed its timeout.
|
24
|
+
#
|
25
|
+
# Returns a hash like:
|
26
|
+
# ```ruby
|
27
|
+
# {
|
28
|
+
# failed: [],
|
29
|
+
# completed: []
|
30
|
+
# }
|
31
|
+
# ```
|
32
|
+
#
|
33
|
+
# @return [Hash]
|
9
34
|
def self.execute
|
10
35
|
multiplexer = Multiplexer.new
|
11
36
|
|
data/lib/muxer/multiplexer.rb
CHANGED
@@ -1,12 +1,40 @@
|
|
1
1
|
module Muxer
|
2
|
+
# Multiplexer is the core class of Muxer that actually multiplexes
|
3
|
+
# web requests. Multiplexer has a lists of Muxer::Requests that will
|
4
|
+
# be executed and added to the completed or failed response when the
|
5
|
+
# timeouts have been reached.
|
6
|
+
#
|
7
|
+
# @!attribute requests
|
8
|
+
# @return [Array] Muxer::Requests that are setup in this Multiplexer
|
9
|
+
# @!attribute timeout
|
10
|
+
# @return [Number] Seconds for the timeout
|
2
11
|
class Multiplexer
|
3
12
|
attr_reader :requests
|
4
13
|
attr_writer :timeout
|
14
|
+
# multiplexer = Multiplexer.new
|
5
15
|
def initialize
|
6
16
|
@requests = []
|
7
17
|
@timeout = nil
|
8
18
|
end
|
9
19
|
|
20
|
+
# add_url builds a Request object and passes it to add_request
|
21
|
+
#
|
22
|
+
# m.add_url('https://www.google.com', {timeout: 3}) # gives a 3 second
|
23
|
+
# timeout to a request to https://www.google.com
|
24
|
+
#
|
25
|
+
# url is merely the target URL
|
26
|
+
#
|
27
|
+
# `options` is a hash describing the web request
|
28
|
+
# {
|
29
|
+
# timeout: nil,
|
30
|
+
# method: :get,
|
31
|
+
# params: {},
|
32
|
+
# redirects: nil
|
33
|
+
# }
|
34
|
+
#
|
35
|
+
# @param url [string] The URL for the web request
|
36
|
+
# @param options [{symbol => Object}] The parameters for the web request
|
37
|
+
# @return true
|
10
38
|
def add_url(url, options = {})
|
11
39
|
options.keys.each do |key|
|
12
40
|
options[key.to_sym] = options.delete(key)
|
@@ -20,12 +48,29 @@ module Muxer
|
|
20
48
|
request.send("#{key}=".to_sym, val) if val
|
21
49
|
end
|
22
50
|
add_request request
|
51
|
+
true
|
23
52
|
end
|
24
53
|
|
54
|
+
# add_request adds a request to Multiplexer
|
55
|
+
#
|
56
|
+
# request = Muxer::Request.new
|
57
|
+
# request.url = 'https://www.google.com'
|
58
|
+
# request.timeout = 3
|
59
|
+
# m.add_request request
|
60
|
+
#
|
61
|
+
# gives a 3 second timeout to a request to https://www.google.com
|
62
|
+
#
|
63
|
+
# @param request [Muxer::Request] the Request to add to the multiplexer
|
64
|
+
# @return true
|
25
65
|
def add_request(request)
|
26
66
|
requests << request
|
67
|
+
true
|
27
68
|
end
|
28
69
|
|
70
|
+
# executes the actual event loop that manages creating, sending,
|
71
|
+
# and processing the finished / timed out web requests
|
72
|
+
#
|
73
|
+
# @return [Hash] Keys are :succeeded, :failed
|
29
74
|
def execute
|
30
75
|
@responses = {succeeded: [], failed: [], pending: []}
|
31
76
|
@start = Time.now
|
data/lib/muxer/request.rb
CHANGED
@@ -1,4 +1,23 @@
|
|
1
1
|
module Muxer
|
2
|
+
# Muxer::Request is designed to wrap the requests that Muxer uses
|
3
|
+
# to parallelize the web requests and handle timeouts.
|
4
|
+
#
|
5
|
+
# @!attribute url
|
6
|
+
# @return [String] URL to use
|
7
|
+
# @!attribute timeout
|
8
|
+
# @return [Number] Seconds for the timeout
|
9
|
+
# @!attribute headers
|
10
|
+
# @return [Hash] Request headers to use with the request
|
11
|
+
# @!attribute params
|
12
|
+
# @return [Hash] request parameters
|
13
|
+
# @!attribute redirects
|
14
|
+
# @return [Integer] How many redirects to follow
|
15
|
+
# @!attribute method
|
16
|
+
# @return [Symbol] HTTP method to use
|
17
|
+
# @!attribute completed
|
18
|
+
# @return [Boolean] Is the request completed
|
19
|
+
# @!attribute error
|
20
|
+
# @return [Boolean] Have we had an error?
|
2
21
|
class Request
|
3
22
|
attr_accessor :url, :timeout, :headers, :params, :redirects
|
4
23
|
attr_reader :method, :completed, :error
|
@@ -14,12 +33,25 @@ module Muxer
|
|
14
33
|
@error = nil
|
15
34
|
end
|
16
35
|
|
36
|
+
# sets the HTTP method of the request as long as the method
|
37
|
+
# is one off the standard http methods. The method can be sent
|
38
|
+
# in as a string or a symbol.
|
39
|
+
# The valid options are:
|
40
|
+
# :get, :post, :head, :options, :put, :delete
|
41
|
+
#
|
42
|
+
# @param method [string, symbol] HTTP Method of the request
|
43
|
+
# @return true
|
17
44
|
def method=(method)
|
18
45
|
method = method.downcase.to_sym
|
19
46
|
|
20
47
|
@method = method if [:get, :post, :head, :options, :put, :delete].include? method
|
48
|
+
true
|
21
49
|
end
|
22
50
|
|
51
|
+
# process! executes the web request. It cannot be called from
|
52
|
+
# outside of an EventMachine loop.
|
53
|
+
#
|
54
|
+
# @return self
|
23
55
|
def process!
|
24
56
|
http = EventMachine::HttpRequest.new(url,
|
25
57
|
connect_timeout: timeout,
|
@@ -35,6 +67,7 @@ module Muxer
|
|
35
67
|
self
|
36
68
|
end
|
37
69
|
|
70
|
+
# response is the actual http request's response.
|
38
71
|
def response
|
39
72
|
if @request
|
40
73
|
@request.response
|
data/lib/muxer/version.rb
CHANGED
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.3.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-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: eventmachine
|