Class: Muxer::Request

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeRequest

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

#completedBoolean Also known as: completed?

Returns Is the request completed.

Returns:

  • (Boolean)

    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_options
    )

    @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 request_options
    options = {
      head: headers
    }
    if [:post, :put].include? method
      options[:body] = params
    else
      options[:query] = params
    end

    options[:redirects] = redirects
    options.reject!{|_,v| empty? v}
    options
  end

  def empty?(opt)
    (opt.nil? || opt == {} || opt == [] || opt == '')
  end
end

#errorBoolean

Returns Have we had an error?.

Returns:

  • (Boolean)

    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_options
    )

    @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 request_options
    options = {
      head: headers
    }
    if [:post, :put].include? method
      options[:body] = params
    else
      options[:query] = params
    end

    options[:redirects] = redirects
    options.reject!{|_,v| empty? v}
    options
  end

  def empty?(opt)
    (opt.nil? || opt == {} || opt == [] || opt == '')
  end
end

#headersHash

Returns Request headers to use with the request.

Returns:

  • (Hash)

    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_options
    )

    @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 request_options
    options = {
      head: headers
    }
    if [:post, :put].include? method
      options[:body] = params
    else
      options[:query] = params
    end

    options[:redirects] = redirects
    options.reject!{|_,v| empty? v}
    options
  end

  def empty?(opt)
    (opt.nil? || opt == {} || opt == [] || opt == '')
  end
end

#idSymbol

be assigned by the user

Returns:

  • (Symbol)

    ID for this request, the ID is arbitrary and to



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_options
    )

    @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 request_options
    options = {
      head: headers
    }
    if [:post, :put].include? method
      options[:body] = params
    else
      options[:query] = params
    end

    options[:redirects] = redirects
    options.reject!{|_,v| empty? v}
    options
  end

  def empty?(opt)
    (opt.nil? || opt == {} || opt == [] || opt == '')
  end
end

#methodSymbol

Returns HTTP method to use.

Returns:

  • (Symbol)

    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_options
    )

    @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 request_options
    options = {
      head: headers
    }
    if [:post, :put].include? method
      options[:body] = params
    else
      options[:query] = params
    end

    options[:redirects] = redirects
    options.reject!{|_,v| empty? v}
    options
  end

  def empty?(opt)
    (opt.nil? || opt == {} || opt == [] || opt == '')
  end
end

#paramsHash

Returns request parameters.

Returns:

  • (Hash)

    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_options
    )

    @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 request_options
    options = {
      head: headers
    }
    if [:post, :put].include? method
      options[:body] = params
    else
      options[:query] = params
    end

    options[:redirects] = redirects
    options.reject!{|_,v| empty? v}
    options
  end

  def empty?(opt)
    (opt.nil? || opt == {} || opt == [] || opt == '')
  end
end

#redirectsInteger

Returns How many redirects to follow.

Returns:

  • (Integer)

    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_options
    )

    @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 request_options
    options = {
      head: headers
    }
    if [:post, :put].include? method
      options[:body] = params
    else
      options[:query] = params
    end

    options[:redirects] = redirects
    options.reject!{|_,v| empty? v}
    options
  end

  def empty?(opt)
    (opt.nil? || opt == {} || opt == [] || opt == '')
  end
end

#runtimeFloat

Returns Runtime for the request.

Returns:

  • (Float)

    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_options
    )

    @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 request_options
    options = {
      head: headers
    }
    if [:post, :put].include? method
      options[:body] = params
    else
      options[:query] = params
    end

    options[:redirects] = redirects
    options.reject!{|_,v| empty? v}
    options
  end

  def empty?(opt)
    (opt.nil? || opt == {} || opt == [] || opt == '')
  end
end

#timeoutNumber

Returns Seconds for the timeout.

Returns:

  • (Number)

    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_options
    )

    @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 request_options
    options = {
      head: headers
    }
    if [:post, :put].include? method
      options[:body] = params
    else
      options[:query] = params
    end

    options[:redirects] = redirects
    options.reject!{|_,v| empty? v}
    options
  end

  def empty?(opt)
    (opt.nil? || opt == {} || opt == [] || opt == '')
  end
end

#urlString

Returns URL to use.

Returns:

  • (String)

    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_options
    )

    @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 request_options
    options = {
      head: headers
    }
    if [:post, :put].include? method
      options[:body] = params
    else
      options[:query] = params
    end

    options[:redirects] = redirects
    options.reject!{|_,v| empty? v}
    options
  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.

Returns:

  • self



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_options
  )

  @request.callback { @completed = true; @runtime = Time.now - @start; @start = nil }
  @request.errback { @completed = @error = true; @runtime = Time.now - @start; @start = nil}
  self
end

#responseObject

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