Sha256: d7dac9cc477df8cb27e4fdb3488a13a0c819299f34301b4db47977585ea70929

Contents?: true

Size: 1.49 KB

Versions: 31

Compression:

Stored size: 1.49 KB

Contents

# coding: utf-8

require 'net/http'
require 'set'

class Net::HTTPResponse
  #noinspection RubyResolve
  attr_accessor :final_uri
end


module Net

  begin
    require 'net/https'
    HTTPS_SUPPORTED = true
  rescue LoadError
  end

  class HTTP
    def self.fetch(uri, args={}.freeze, &before_fetching)
      uri = URI.parse(uri) unless uri.is_a? URI
      proxy_host    = args[:proxy_host]
      proxy_port    = args[:proxy_port] || 80
      action        = args[:action] || :get
      data          = args[:data]
      max_redirects = args[:max_redirects] || 10

      proxy_class   = Proxy(proxy_host, proxy_port)
      #noinspection RubyArgCount
      request       = proxy_class.new(uri.host, uri.port)

      request.use_ssl = true if HTTPS_SUPPORTED && uri.scheme.eql?('https')

      yield request if block_given?
#debugger
      response = request.send(action, uri.path, data)

      urls_seen = args[:_url_seen] || Set.new

      case response
      when Net::HTTPRedirection
        if urls_seen.size < max_redirects && response['Location']
          urls_seen << uri
          new_uri = URI.parse(response['Location'])
          if urls_seen.member? new_uri
            nil
          else
            new_args = args.dup
            new_args[:_urls_seen] = urls_seen

            response = HTTP.fetch(new_uri, new_args, &before_fetching)
          end
        end
      when Net::HTTPSuccess
        response.final_uri = uri
      else
        response.error!
      end
      return response
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
libis-services-1.1.0 lib/libis/services/extend/http_fetch.rb
libis-services-1.0.13 lib/libis/services/extend/http_fetch.rb
libis-services-1.0.12 lib/libis/services/extend/http_fetch.rb
libis-services-1.0.11 lib/libis/services/extend/http_fetch.rb
libis-services-1.0.10 lib/libis/services/extend/http_fetch.rb
libis-services-1.0.7 lib/libis/services/extend/http_fetch.rb
libis-services-1.0.6 lib/libis/services/extend/http_fetch.rb
libis-services-1.0.5 lib/libis/services/extend/http_fetch.rb
libis-services-1.0.2 lib/libis/services/extend/http_fetch.rb
libis-services-1.0.1 lib/libis/services/extend/http_fetch.rb
libis-services-1.0.0 lib/libis/services/extend/http_fetch.rb
libis-services-0.2.1 lib/libis/services/extend/http_fetch.rb
libis-services-0.2.0 lib/libis/services/extend/http_fetch.rb
libis-services-0.1.18 lib/libis/services/extend/http_fetch.rb
libis-services-0.1.17 lib/libis/services/extend/http_fetch.rb
libis-services-0.1.16 lib/libis/services/extend/http_fetch.rb
libis-services-0.1.15 lib/libis/services/extend/http_fetch.rb
libis-services-0.1.14 lib/libis/services/extend/http_fetch.rb
libis-services-0.1.13 lib/libis/services/extend/http_fetch.rb
libis-services-0.1.12 lib/libis/services/extend/http_fetch.rb