Class: FaultTolerantRequests::GetJsonRequest

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of GetJsonRequest.



4
5
6
7
8
# File 'lib/fault_tolerant_requests/get_json_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
# File 'lib/fault_tolerant_requests/get_json_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

            requester = ::FaultTolerantRequests::GetRequest
                .new(
                    @proxy_ip, @user_agent, @debug
                )
            
            content = requester.do(
                    url, attempts, wait
                )

            output = ActiveSupport::JSON.decode( content ).deep_symbolize_keys
            
            return output

        rescue JSON::ParserError => p
            puts " --- Attempt #{request_count}: JSON parse error!"
            puts "#{ p.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