Class: FaultTolerantRequests::GetRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/fault_tolerant_requests/get_request.rb

Instance Method Summary collapse

Constructor Details

#initialize(proxy_ip = false, user_agent = false, debug = false) ⇒ GetRequest

Returns a new instance of GetRequest.



4
5
6
7
8
# File 'lib/fault_tolerant_requests/get_request.rb', line 4

def initialize(proxy_ip = false, user_agent = false, debug = false)
    @proxy_ip = proxy_ip
    @debug = debug
    @user_agent = user_agent
end

Instance Method Details

#do(url, attempts = 15, wait = 15) ⇒ Object



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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fault_tolerant_requests/get_request.rb', line 10

def do(url, attempts = 15, wait = 15)
    request_count = 0
    while request_count < attempts do
        begin
            options = {}
            options[:proxy] = URI.parse("http://#{@proxy_ip}:3128") if @proxy_ip
            options["User-Agent"] = @user_agent if @user_agent

            puts options if @debug
            puts " + #{url} (#{request_count})" if @debug

            output = URI.open(url, options).read

            puts output if @debug

            # request went through fine
            return output
        rescue OpenURI::HTTPError => p
            puts " --- Attempt #{request_count}: HTTP error!"
            puts "#{ p.inspect }"
            request_count+= 1
            sleep request_count * wait * 2

        rescue Net::ReadTimeout, Net::OpenTimeout, EOFError => e
            # timeout/eof occured, log and sleep
            puts " --- Attempt #{request_count}: timeout error!"
            puts "#{e.inspect}"
            request_count+= 1
            sleep request_count * wait

        rescue OpenSSL::SSL::SSLError => n
            # SSL certificate error
            puts " --- Attempt #{request_count}: SSL error!"
            puts "#{e.inspect}"
            request_count+= 1
            sleep request_count * wait

        end
    end

    # failed, raise an exception and exit, Rollbar will report
    raise StandardError.new "Exceeded timeout/error count while sending API requests"

end