Class: HttpspecSimple::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/httpspec_simple/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, opt = {}) ⇒ Request

Returns a new instance of Request.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/httpspec_simple/request.rb', line 8

def initialize(url, opt = {})
  @url = URI.parse(url)
  http = Net::HTTP.new(@url.host, @url.port)
  unless opt[:timeout].nil?
    http.read_timeout = opt[:timeout]
    http.open_timeout = opt[:timeout]
  end
  retry_count = opt[:retry].to_i
  @status, @response_time = http.start do |http|
    res, response_time = process_time do
      open_timeout_error = if Net.const_defined?(:OpenTimeout) then Net::OpenTimeout else Timeout::Error end
      read_timeout_error = if Net.const_defined?(:ReadTimeout) then Net::ReadTimeout else Timeout::Error end
      begin
        res = http.request(Net::HTTP::Get.new(@url.path))
        raise RequestError.new if res.kind_of?(Net::HTTPClientError) or res.kind_of?(Net::HTTPServerError)
      rescue open_timeout_error, read_timeout_error, RequestError
        retry if (retry_count-=1) > 0
      end
      res
    end
    unless res.nil?
      [res.code, response_time]
    else
      ['timeout', response_time]
    end
  end
end

Instance Attribute Details

#response_timeObject (readonly)

Returns the value of attribute response_time.



6
7
8
# File 'lib/httpspec_simple/request.rb', line 6

def response_time
  @response_time
end

#statusObject (readonly)

Returns the value of attribute status.



6
7
8
# File 'lib/httpspec_simple/request.rb', line 6

def status
  @status
end

Instance Method Details

#process_timeObject



36
37
38
39
40
# File 'lib/httpspec_simple/request.rb', line 36

def process_time
  start_time = Time.now
  ret = yield
  [ret, Time.now - start_time]
end

#to_sObject



42
43
44
# File 'lib/httpspec_simple/request.rb', line 42

def to_s
  @url.to_s
end