Class: ProxyFetcher::HTTPClient

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

Overview

Default ProxyFetcher HTTP client used to fetch proxy lists from the different providers. Uses ProxyFetcher configuration options for sending HTTP requests to providers URLs.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, method: :get, params: {}, headers: {}) ⇒ HTTPClient

Initialize HTTP client instance



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/proxy_fetcher/utils/http_client.rb', line 52

def initialize(url, method: :get, params: {}, headers: {})
  @url = url.to_s
  @method = method.to_sym
  @params = params
  @headers = headers

  unless HTTP::Request::METHODS.include?(@method)
    raise ArgumentError, "'#{@method}' is a wrong HTTP method name"
  end

  @timeout = ProxyFetcher.config.provider_proxies_load_timeout
  @http = build_http_engine
  @ssl_ctx = build_ssl_context
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#httpObject (readonly)

Returns the value of attribute http.



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

def http
  @http
end

#HTTP headers(headers) ⇒ Hash (readonly)

Returns headers.

Returns:

  • (Hash)

    headers



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

attr_reader :headers

#HTTP method(method) ⇒ String (readonly)

Returns HTTP method verb.

Returns:

  • (String)

    HTTP method verb



14
# File 'lib/proxy_fetcher/utils/http_client.rb', line 14

attr_reader :method

#HTTP params(params) ⇒ Hash (readonly)

Returns params.

Returns:

  • (Hash)

    params



18
# File 'lib/proxy_fetcher/utils/http_client.rb', line 18

attr_reader :params

#methodObject (readonly)

Returns the value of attribute method.



14
15
16
# File 'lib/proxy_fetcher/utils/http_client.rb', line 14

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



18
19
20
# File 'lib/proxy_fetcher/utils/http_client.rb', line 18

def params
  @params
end

#ssl_ctxObject (readonly)

Returns the value of attribute ssl_ctx.



30
31
32
# File 'lib/proxy_fetcher/utils/http_client.rb', line 30

def ssl_ctx
  @ssl_ctx
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



34
35
36
# File 'lib/proxy_fetcher/utils/http_client.rb', line 34

def timeout
  @timeout
end

#urlObject (readonly)

Returns the value of attribute url.



10
11
12
# File 'lib/proxy_fetcher/utils/http_client.rb', line 10

def url
  @url
end

Class Method Details

.fetch(*args, **kwargs, &block) ⇒ String

Fetches resource content by sending HTTP request to it. Synthetic sugar to simplify URIes fetching.

Parameters:

  • url (String)

    URL

Returns:

  • (String)

    resource content



44
45
46
# File 'lib/proxy_fetcher/utils/http_client.rb', line 44

def self.fetch(*args, **kwargs, &block)
  new(*args, **kwargs, &block).fetch
end

Instance Method Details

#fetch(**options) ⇒ String

Fetches resource content by sending HTTP request to it.

Returns:

  • (String)

    response body



72
73
74
75
76
77
78
79
80
# File 'lib/proxy_fetcher/utils/http_client.rb', line 72

def fetch(**options)
  response = perform_http_request
  return response if options.fetch(:raw, false)

  response.body.to_s
rescue StandardError => e
  ProxyFetcher.config.logger.warn("Failed to process request to #{url} (#{e.message})")
  ""
end