Class: ProxyFetcher::HTTPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/proxy_fetcher/utils/http_client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ HTTPClient

Returns a new instance of HTTPClient.



5
6
7
8
9
10
11
12
# File 'lib/proxy_fetcher/utils/http_client.rb', line 5

def initialize(url)
  @uri = URI.parse(url)
  @http = Net::HTTP.new(@uri.host, @uri.port)
  return unless https?

  @http.use_ssl = true
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



3
4
5
# File 'lib/proxy_fetcher/utils/http_client.rb', line 3

def http
  @http
end

#uriObject (readonly)

Returns the value of attribute uri.



3
4
5
# File 'lib/proxy_fetcher/utils/http_client.rb', line 3

def uri
  @uri
end

Class Method Details

.fetch(url) ⇒ Object



27
28
29
# File 'lib/proxy_fetcher/utils/http_client.rb', line 27

def fetch(url)
  new(url).fetch
end

Instance Method Details

#fetchObject



14
15
16
17
18
19
20
# File 'lib/proxy_fetcher/utils/http_client.rb', line 14

def fetch
  request = Net::HTTP::Get.new(@uri.to_s)
  request['Connection'] = 'keep-alive'
  request['User-Agent'] = ProxyFetcher.config.user_agent
  response = @http.request(request)
  response.body
end

#https?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/proxy_fetcher/utils/http_client.rb', line 22

def https?
  @uri.is_a?(URI::HTTPS)
end