Sha256: bf63a90b3f74de0f05971e5c2b0060fdc0833ad2a7782b0a596e7adadf8c1ec5

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module Webdrivers
  class Network
    class << self
      def get(url, limit = 10)
        Webdrivers.logger.debug "Making network call to #{url}"

        response = get_response(url, limit)
        case response
        when Net::HTTPSuccess
          response.body
        else
          raise StandardError, "#{response.class::EXCEPTION_TYPE}: #{response.code} \"#{response.message}\" with #{url}"
        end
      end

      def get_url(url, limit = 10)
        Webdrivers.logger.debug "Making network call to #{url}"

        get_response(url, limit).uri.to_s
      end

      def get_response(url, limit = 10)
        raise StandardError, 'Too many HTTP redirects' if limit.zero?

        begin
          response = http.get_response(URI(url))
        rescue SocketError
          raise ConnectionError, "Can not reach #{url}"
        end

        Webdrivers.logger.debug "Get response: #{response.inspect}"

        if response.is_a?(Net::HTTPRedirection)
          location = response['location']
          Webdrivers.logger.debug "Redirected to #{location}"
          get_response(location, limit - 1)
        else
          response
        end
      end

      def http
        if using_proxy
          Net::HTTP.Proxy(Webdrivers.proxy_addr, Webdrivers.proxy_port,
                          Webdrivers.proxy_user, Webdrivers.proxy_pass)
        else
          Net::HTTP
        end
      end

      def using_proxy
        Webdrivers.proxy_addr && Webdrivers.proxy_port
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
webdrivers-3.9.1 lib/webdrivers/network.rb
webdrivers-3.9.0 lib/webdrivers/network.rb