Sha256: b59848dd59eb1a71e511640707a2e10a4a217ac58fb7f2d5d351dab5ddd6e765

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

module Notu

  module HttpDownload

    def self.get(url, options = {})
      uri = url.is_a?(URI) ? url : URI.parse(url)
      raise Error.new("Invalid URL: #{url.inspect}") unless uri.is_a?(URI::HTTP)
      Notu.logger.debug('Notu') { "GET #{url}" }
      options.reverse_merge!(max_redirects: 10, timeout: 10, max_retries: 3, retry_sleep: 2)
      connection = Net::HTTP.new(uri.host, uri.port)
      connection.open_timeout = options[:timeout]
      connection.read_timeout = options[:timeout]
      if uri.is_a?(URI::HTTPS)
        connection.use_ssl = true
        connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
      end
      connection.start do |http|
        http.request_get(uri.request_uri, (options[:headers] || {}).stringify_keys) do |response|
          begin
            response.value # raise if not success
          rescue Net::HTTPRetriableError
            raise NetworkError.new('Max redirects has been reached') if options[:max_redirects] < 1
            options[:max_redirects] -= 1
            return get(response['Location'], options)
          end
          return response.body
        end
      end
    rescue Timeout::Error, Zlib::BufError => e
      raise NetworkError.new(e) if options[:max_retries] < 1
      options[:max_retries] -= 1
      sleep(options[:retry_sleep])
      get(url, options)
    rescue => e
      raise NetworkError.new(e)
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
notu-5.0.1 lib/notu/http_download.rb
notu-5.0.0 lib/notu/http_download.rb
notu-4.0.0 lib/notu/http_download.rb
notu-3.0.0 lib/notu/http_download.rb