class ProxyFetcher::Client::Request

ProxyFetcher::Client HTTP request abstraction.

Attributes

headers[R]

@!attribute [r] headers

@return [Hash] HTTP headers
max_redirects[R]

@!attribute [r] max_redirects

@return [Integer] Maximum count of requests (if fails)
method[R]

@!attribute [r] method

@return [String, Symbol] HTTP request method
payload[R]

@!attribute [r] payload

@return [String, Hash] Request payload
proxy[R]

@!attribute [r] proxy

@return [Proxy] Proxy to process the request
ssl_options[R]

@!attribute [r] ssl_options

@return [Hash] SSL options
timeout[R]

@!attribute [r] timeout

@return [Integer] Request timeout
url[R]

@!attribute [r] url

@return [String] Request URL

Public Class Methods

execute(**args) click to toggle source

Initializes a new HTTP request and processes it

@return [String]

response body (requested resource content)
# File lib/proxy_fetcher/client/request.rb, line 44
def self.execute(**args)
  new(**args).execute
end
new(**args) click to toggle source

Initialize new HTTP request

@return [Request]

# File lib/proxy_fetcher/client/request.rb, line 52
def initialize(**args)
  raise ArgumentError, "args must be a Hash!" unless args.is_a?(Hash)

  @url = args.fetch(:url)
  @method = args.fetch(:method).to_s.downcase
  @headers = (args[:headers] || {}).dup
  @payload = args[:payload]
  @timeout = args.fetch(:timeout, ProxyFetcher.config.client_timeout)
  @ssl_options = args.fetch(:ssl_options, default_ssl_options)

  @proxy = args.fetch(:proxy)
  @max_redirects = args.fetch(:max_redirects, 10)

  @http = build_http_client
end

Public Instance Methods

execute() click to toggle source

Executes HTTP request with defined options.

@return [String]

response body (requested resource content)
# File lib/proxy_fetcher/client/request.rb, line 73
def execute
  response = send_request
  response.body.to_s
rescue HTTP::Redirector::TooManyRedirectsError
  raise ProxyFetcher::Exceptions::MaximumRedirectsReached
end

Private Instance Methods

build_http_client() click to toggle source

Builds HTTP client.

@return [HTTP::Client]

HTTP client
# File lib/proxy_fetcher/client/request.rb, line 87
def build_http_client
  HTTP.via(proxy.addr, proxy.port.to_i)
    .headers(headers)
    .timeout(connect: timeout, read: timeout)
    .follow(max_hops: max_redirects)
end
default_ssl_options() click to toggle source

Default SSL options that will be used for connecting to resources the uses secure connection. By default ProxyFetcher wouldn't verify SSL certs.

@return [OpenSSL::SSL::SSLContext] SSL context

# File lib/proxy_fetcher/client/request.rb, line 100
def default_ssl_options
  ssl_ctx = OpenSSL::SSL::SSLContext.new
  ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
  ssl_ctx
end
send_request() click to toggle source

Sends HTTP request to the URL. Check for the payload and it's type in order to build valid request.

@return [HTTP::Response] request response

# File lib/proxy_fetcher/client/request.rb, line 111
def send_request
  if payload
    payload_type = payload.is_a?(String) ? :body : :form

    @http.public_send(method, url, payload_type => payload, ssl_context: ssl_options)
  else
    @http.public_send(method, url, ssl_context: ssl_options)
  end
end