Class: Muxer::Request
- Inherits:
-
Object
- Object
- Muxer::Request
- Defined in:
- lib/muxer/request.rb
Overview
Muxer::Request is designed to wrap the requests that Muxer uses to parallelize the web requests and handle timeouts.
Instance Attribute Summary collapse
-
#completed ⇒ Boolean
(also: #completed?)
Is the request completed.
-
#error ⇒ Boolean
Have we had an error?.
-
#headers ⇒ Hash
Request headers to use with the request.
-
#id ⇒ Symbol
be assigned by the user.
-
#method ⇒ Symbol
HTTP method to use.
-
#params ⇒ Hash
Request parameters.
-
#redirects ⇒ Integer
How many redirects to follow.
-
#runtime ⇒ Float
Runtime for the request.
-
#timeout ⇒ Number
Seconds for the timeout.
-
#url ⇒ String
URL to use.
Instance Method Summary collapse
-
#initialize ⇒ Request
constructor
A new instance of Request.
-
#process! ⇒ Object
process! executes the web request.
-
#response ⇒ Object
response is the actual http request’s response.
Constructor Details
#initialize ⇒ Request
Returns a new instance of Request.
31 32 33 34 35 36 37 38 39 |
# File 'lib/muxer/request.rb', line 31 def initialize @method = :get @completed = false @timeout = 10 @headers = {} @params = {} @request = nil @error = nil end |
Instance Attribute Details
#completed ⇒ Boolean Also known as: completed?
Returns Is the request completed.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/muxer/request.rb', line 26 class Request attr_accessor :url, :timeout, :headers, :params, :redirects, :id attr_reader :method, :completed, :error, :runtime alias_method :completed?, :completed def initialize @method = :get @completed = false @timeout = 10 @headers = {} @params = {} @request = nil @error = nil end # sets the HTTP method of the request as long as the method # is one off the standard http methods. The method can be sent # in as a string or a symbol. # The valid options are: # :get, :post, :head, :options, :put, :delete # # @param method [string, symbol] HTTP Method of the request # @return true def method=(method) method = method.downcase.to_sym @method = method if [:get, :post, :head, :options, :put, :delete].include? method true end # process! executes the web request. It cannot be called from # outside of an EventMachine loop. # # @return self def process! @start = Time.now http = EventMachine::HttpRequest.new(url, connect_timeout: timeout, inactivity_timeout: timeout, ) @request = http.public_send(method, ) @request.callback { @completed = true; @runtime = Time.now - @start; @start = nil } @request.errback { @completed = @error = true; @runtime = Time.now - @start; @start = nil} self end # response is the actual http request's response. def response if @request @request.response end end private def = { head: headers } if [:post, :put].include? method [:body] = params else [:query] = params end [:redirects] = redirects .reject!{|_,v| empty? v} end def empty?(opt) (opt.nil? || opt == {} || opt == [] || opt == '') end end |
#error ⇒ Boolean
Returns Have we had an error?.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/muxer/request.rb', line 26 class Request attr_accessor :url, :timeout, :headers, :params, :redirects, :id attr_reader :method, :completed, :error, :runtime alias_method :completed?, :completed def initialize @method = :get @completed = false @timeout = 10 @headers = {} @params = {} @request = nil @error = nil end # sets the HTTP method of the request as long as the method # is one off the standard http methods. The method can be sent # in as a string or a symbol. # The valid options are: # :get, :post, :head, :options, :put, :delete # # @param method [string, symbol] HTTP Method of the request # @return true def method=(method) method = method.downcase.to_sym @method = method if [:get, :post, :head, :options, :put, :delete].include? method true end # process! executes the web request. It cannot be called from # outside of an EventMachine loop. # # @return self def process! @start = Time.now http = EventMachine::HttpRequest.new(url, connect_timeout: timeout, inactivity_timeout: timeout, ) @request = http.public_send(method, ) @request.callback { @completed = true; @runtime = Time.now - @start; @start = nil } @request.errback { @completed = @error = true; @runtime = Time.now - @start; @start = nil} self end # response is the actual http request's response. def response if @request @request.response end end private def = { head: headers } if [:post, :put].include? method [:body] = params else [:query] = params end [:redirects] = redirects .reject!{|_,v| empty? v} end def empty?(opt) (opt.nil? || opt == {} || opt == [] || opt == '') end end |
#headers ⇒ Hash
Returns Request headers to use with the request.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/muxer/request.rb', line 26 class Request attr_accessor :url, :timeout, :headers, :params, :redirects, :id attr_reader :method, :completed, :error, :runtime alias_method :completed?, :completed def initialize @method = :get @completed = false @timeout = 10 @headers = {} @params = {} @request = nil @error = nil end # sets the HTTP method of the request as long as the method # is one off the standard http methods. The method can be sent # in as a string or a symbol. # The valid options are: # :get, :post, :head, :options, :put, :delete # # @param method [string, symbol] HTTP Method of the request # @return true def method=(method) method = method.downcase.to_sym @method = method if [:get, :post, :head, :options, :put, :delete].include? method true end # process! executes the web request. It cannot be called from # outside of an EventMachine loop. # # @return self def process! @start = Time.now http = EventMachine::HttpRequest.new(url, connect_timeout: timeout, inactivity_timeout: timeout, ) @request = http.public_send(method, ) @request.callback { @completed = true; @runtime = Time.now - @start; @start = nil } @request.errback { @completed = @error = true; @runtime = Time.now - @start; @start = nil} self end # response is the actual http request's response. def response if @request @request.response end end private def = { head: headers } if [:post, :put].include? method [:body] = params else [:query] = params end [:redirects] = redirects .reject!{|_,v| empty? v} end def empty?(opt) (opt.nil? || opt == {} || opt == [] || opt == '') end end |
#id ⇒ Symbol
be assigned by the user
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/muxer/request.rb', line 26 class Request attr_accessor :url, :timeout, :headers, :params, :redirects, :id attr_reader :method, :completed, :error, :runtime alias_method :completed?, :completed def initialize @method = :get @completed = false @timeout = 10 @headers = {} @params = {} @request = nil @error = nil end # sets the HTTP method of the request as long as the method # is one off the standard http methods. The method can be sent # in as a string or a symbol. # The valid options are: # :get, :post, :head, :options, :put, :delete # # @param method [string, symbol] HTTP Method of the request # @return true def method=(method) method = method.downcase.to_sym @method = method if [:get, :post, :head, :options, :put, :delete].include? method true end # process! executes the web request. It cannot be called from # outside of an EventMachine loop. # # @return self def process! @start = Time.now http = EventMachine::HttpRequest.new(url, connect_timeout: timeout, inactivity_timeout: timeout, ) @request = http.public_send(method, ) @request.callback { @completed = true; @runtime = Time.now - @start; @start = nil } @request.errback { @completed = @error = true; @runtime = Time.now - @start; @start = nil} self end # response is the actual http request's response. def response if @request @request.response end end private def = { head: headers } if [:post, :put].include? method [:body] = params else [:query] = params end [:redirects] = redirects .reject!{|_,v| empty? v} end def empty?(opt) (opt.nil? || opt == {} || opt == [] || opt == '') end end |
#method ⇒ Symbol
Returns HTTP method to use.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/muxer/request.rb', line 26 class Request attr_accessor :url, :timeout, :headers, :params, :redirects, :id attr_reader :method, :completed, :error, :runtime alias_method :completed?, :completed def initialize @method = :get @completed = false @timeout = 10 @headers = {} @params = {} @request = nil @error = nil end # sets the HTTP method of the request as long as the method # is one off the standard http methods. The method can be sent # in as a string or a symbol. # The valid options are: # :get, :post, :head, :options, :put, :delete # # @param method [string, symbol] HTTP Method of the request # @return true def method=(method) method = method.downcase.to_sym @method = method if [:get, :post, :head, :options, :put, :delete].include? method true end # process! executes the web request. It cannot be called from # outside of an EventMachine loop. # # @return self def process! @start = Time.now http = EventMachine::HttpRequest.new(url, connect_timeout: timeout, inactivity_timeout: timeout, ) @request = http.public_send(method, ) @request.callback { @completed = true; @runtime = Time.now - @start; @start = nil } @request.errback { @completed = @error = true; @runtime = Time.now - @start; @start = nil} self end # response is the actual http request's response. def response if @request @request.response end end private def = { head: headers } if [:post, :put].include? method [:body] = params else [:query] = params end [:redirects] = redirects .reject!{|_,v| empty? v} end def empty?(opt) (opt.nil? || opt == {} || opt == [] || opt == '') end end |
#params ⇒ Hash
Returns request parameters.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/muxer/request.rb', line 26 class Request attr_accessor :url, :timeout, :headers, :params, :redirects, :id attr_reader :method, :completed, :error, :runtime alias_method :completed?, :completed def initialize @method = :get @completed = false @timeout = 10 @headers = {} @params = {} @request = nil @error = nil end # sets the HTTP method of the request as long as the method # is one off the standard http methods. The method can be sent # in as a string or a symbol. # The valid options are: # :get, :post, :head, :options, :put, :delete # # @param method [string, symbol] HTTP Method of the request # @return true def method=(method) method = method.downcase.to_sym @method = method if [:get, :post, :head, :options, :put, :delete].include? method true end # process! executes the web request. It cannot be called from # outside of an EventMachine loop. # # @return self def process! @start = Time.now http = EventMachine::HttpRequest.new(url, connect_timeout: timeout, inactivity_timeout: timeout, ) @request = http.public_send(method, ) @request.callback { @completed = true; @runtime = Time.now - @start; @start = nil } @request.errback { @completed = @error = true; @runtime = Time.now - @start; @start = nil} self end # response is the actual http request's response. def response if @request @request.response end end private def = { head: headers } if [:post, :put].include? method [:body] = params else [:query] = params end [:redirects] = redirects .reject!{|_,v| empty? v} end def empty?(opt) (opt.nil? || opt == {} || opt == [] || opt == '') end end |
#redirects ⇒ Integer
Returns How many redirects to follow.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/muxer/request.rb', line 26 class Request attr_accessor :url, :timeout, :headers, :params, :redirects, :id attr_reader :method, :completed, :error, :runtime alias_method :completed?, :completed def initialize @method = :get @completed = false @timeout = 10 @headers = {} @params = {} @request = nil @error = nil end # sets the HTTP method of the request as long as the method # is one off the standard http methods. The method can be sent # in as a string or a symbol. # The valid options are: # :get, :post, :head, :options, :put, :delete # # @param method [string, symbol] HTTP Method of the request # @return true def method=(method) method = method.downcase.to_sym @method = method if [:get, :post, :head, :options, :put, :delete].include? method true end # process! executes the web request. It cannot be called from # outside of an EventMachine loop. # # @return self def process! @start = Time.now http = EventMachine::HttpRequest.new(url, connect_timeout: timeout, inactivity_timeout: timeout, ) @request = http.public_send(method, ) @request.callback { @completed = true; @runtime = Time.now - @start; @start = nil } @request.errback { @completed = @error = true; @runtime = Time.now - @start; @start = nil} self end # response is the actual http request's response. def response if @request @request.response end end private def = { head: headers } if [:post, :put].include? method [:body] = params else [:query] = params end [:redirects] = redirects .reject!{|_,v| empty? v} end def empty?(opt) (opt.nil? || opt == {} || opt == [] || opt == '') end end |
#runtime ⇒ Float
Returns Runtime for the request.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/muxer/request.rb', line 26 class Request attr_accessor :url, :timeout, :headers, :params, :redirects, :id attr_reader :method, :completed, :error, :runtime alias_method :completed?, :completed def initialize @method = :get @completed = false @timeout = 10 @headers = {} @params = {} @request = nil @error = nil end # sets the HTTP method of the request as long as the method # is one off the standard http methods. The method can be sent # in as a string or a symbol. # The valid options are: # :get, :post, :head, :options, :put, :delete # # @param method [string, symbol] HTTP Method of the request # @return true def method=(method) method = method.downcase.to_sym @method = method if [:get, :post, :head, :options, :put, :delete].include? method true end # process! executes the web request. It cannot be called from # outside of an EventMachine loop. # # @return self def process! @start = Time.now http = EventMachine::HttpRequest.new(url, connect_timeout: timeout, inactivity_timeout: timeout, ) @request = http.public_send(method, ) @request.callback { @completed = true; @runtime = Time.now - @start; @start = nil } @request.errback { @completed = @error = true; @runtime = Time.now - @start; @start = nil} self end # response is the actual http request's response. def response if @request @request.response end end private def = { head: headers } if [:post, :put].include? method [:body] = params else [:query] = params end [:redirects] = redirects .reject!{|_,v| empty? v} end def empty?(opt) (opt.nil? || opt == {} || opt == [] || opt == '') end end |
#timeout ⇒ Number
Returns Seconds for the timeout.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/muxer/request.rb', line 26 class Request attr_accessor :url, :timeout, :headers, :params, :redirects, :id attr_reader :method, :completed, :error, :runtime alias_method :completed?, :completed def initialize @method = :get @completed = false @timeout = 10 @headers = {} @params = {} @request = nil @error = nil end # sets the HTTP method of the request as long as the method # is one off the standard http methods. The method can be sent # in as a string or a symbol. # The valid options are: # :get, :post, :head, :options, :put, :delete # # @param method [string, symbol] HTTP Method of the request # @return true def method=(method) method = method.downcase.to_sym @method = method if [:get, :post, :head, :options, :put, :delete].include? method true end # process! executes the web request. It cannot be called from # outside of an EventMachine loop. # # @return self def process! @start = Time.now http = EventMachine::HttpRequest.new(url, connect_timeout: timeout, inactivity_timeout: timeout, ) @request = http.public_send(method, ) @request.callback { @completed = true; @runtime = Time.now - @start; @start = nil } @request.errback { @completed = @error = true; @runtime = Time.now - @start; @start = nil} self end # response is the actual http request's response. def response if @request @request.response end end private def = { head: headers } if [:post, :put].include? method [:body] = params else [:query] = params end [:redirects] = redirects .reject!{|_,v| empty? v} end def empty?(opt) (opt.nil? || opt == {} || opt == [] || opt == '') end end |
#url ⇒ String
Returns URL to use.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/muxer/request.rb', line 26 class Request attr_accessor :url, :timeout, :headers, :params, :redirects, :id attr_reader :method, :completed, :error, :runtime alias_method :completed?, :completed def initialize @method = :get @completed = false @timeout = 10 @headers = {} @params = {} @request = nil @error = nil end # sets the HTTP method of the request as long as the method # is one off the standard http methods. The method can be sent # in as a string or a symbol. # The valid options are: # :get, :post, :head, :options, :put, :delete # # @param method [string, symbol] HTTP Method of the request # @return true def method=(method) method = method.downcase.to_sym @method = method if [:get, :post, :head, :options, :put, :delete].include? method true end # process! executes the web request. It cannot be called from # outside of an EventMachine loop. # # @return self def process! @start = Time.now http = EventMachine::HttpRequest.new(url, connect_timeout: timeout, inactivity_timeout: timeout, ) @request = http.public_send(method, ) @request.callback { @completed = true; @runtime = Time.now - @start; @start = nil } @request.errback { @completed = @error = true; @runtime = Time.now - @start; @start = nil} self end # response is the actual http request's response. def response if @request @request.response end end private def = { head: headers } if [:post, :put].include? method [:body] = params else [:query] = params end [:redirects] = redirects .reject!{|_,v| empty? v} end def empty?(opt) (opt.nil? || opt == {} || opt == [] || opt == '') end end |
Instance Method Details
#process! ⇒ Object
process! executes the web request. It cannot be called from outside of an EventMachine loop.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/muxer/request.rb', line 60 def process! @start = Time.now http = EventMachine::HttpRequest.new(url, connect_timeout: timeout, inactivity_timeout: timeout, ) @request = http.public_send(method, ) @request.callback { @completed = true; @runtime = Time.now - @start; @start = nil } @request.errback { @completed = @error = true; @runtime = Time.now - @start; @start = nil} self end |
#response ⇒ Object
response is the actual http request’s response.
77 78 79 80 81 |
# File 'lib/muxer/request.rb', line 77 def response if @request @request.response end end |