Class: ProxyFetcher::Client::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/proxy_fetcher/client/request.rb

Constant Summary collapse

URL_ENCODED =
{
  'Content-Type' => 'application/x-www-form-urlencoded'
}.freeze
DEFAULT_SSL_OPTIONS =
{
  verify_mode: OpenSSL::SSL::VERIFY_NONE
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Request

Returns a new instance of Request.

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/proxy_fetcher/client/request.rb', line 19

def initialize(args)
  raise ArgumentError, 'args must be a Hash!' unless args.is_a?(Hash)

  @uri = URI.parse(args.fetch(:url))
  @method = args.fetch(:method).to_s.capitalize
  @headers = (args[:headers] || {}).dup
  @payload = preprocess_payload(args[:payload])
  @timeout = args.fetch(:timeout, ProxyFetcher.config.timeout)
  @ssl_options = args.fetch(:ssl_options, DEFAULT_SSL_OPTIONS)

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

  build_http_client
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



12
13
14
# File 'lib/proxy_fetcher/client/request.rb', line 12

def headers
  @headers
end

#httpObject (readonly)

Returns the value of attribute http.



12
13
14
# File 'lib/proxy_fetcher/client/request.rb', line 12

def http
  @http
end

#max_redirectsObject (readonly)

Returns the value of attribute max_redirects.



12
13
14
# File 'lib/proxy_fetcher/client/request.rb', line 12

def max_redirects
  @max_redirects
end

#methodObject (readonly)

Returns the value of attribute method.



12
13
14
# File 'lib/proxy_fetcher/client/request.rb', line 12

def method
  @method
end

#payloadObject (readonly)

Returns the value of attribute payload.



12
13
14
# File 'lib/proxy_fetcher/client/request.rb', line 12

def payload
  @payload
end

#proxyObject (readonly)

Returns the value of attribute proxy.



12
13
14
# File 'lib/proxy_fetcher/client/request.rb', line 12

def proxy
  @proxy
end

#ssl_optionsObject (readonly)

Returns the value of attribute ssl_options.



12
13
14
# File 'lib/proxy_fetcher/client/request.rb', line 12

def ssl_options
  @ssl_options
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



12
13
14
# File 'lib/proxy_fetcher/client/request.rb', line 12

def timeout
  @timeout
end

#uriObject (readonly)

Returns the value of attribute uri.



12
13
14
# File 'lib/proxy_fetcher/client/request.rb', line 12

def uri
  @uri
end

Class Method Details

.execute(args) ⇒ Object



15
16
17
# File 'lib/proxy_fetcher/client/request.rb', line 15

def self.execute(args)
  new(args).execute
end

Instance Method Details

#executeObject



35
36
37
38
39
40
41
# File 'lib/proxy_fetcher/client/request.rb', line 35

def execute
  request = request_class_for(method).new(uri, headers)

  http.start do |connection|
    process_response!(connection.request(request, payload))
  end
end