Class: ProxyFetcher::Client::Request

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

Overview

ProxyFetcher::Client HTTP request abstraction.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Request

Initialize new HTTP request

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



17
18
19
# File 'lib/proxy_fetcher/client/request.rb', line 17

def headers
  @headers
end

#max_redirectsObject (readonly)

Returns the value of attribute max_redirects.



33
34
35
# File 'lib/proxy_fetcher/client/request.rb', line 33

def max_redirects
  @max_redirects
end

#methodObject (readonly)

Returns the value of attribute method.



9
10
11
# File 'lib/proxy_fetcher/client/request.rb', line 9

def method
  @method
end

#payloadObject (readonly)

Returns the value of attribute payload.



25
26
27
# File 'lib/proxy_fetcher/client/request.rb', line 25

def payload
  @payload
end

#proxyObject (readonly)

Returns the value of attribute proxy.



29
30
31
# File 'lib/proxy_fetcher/client/request.rb', line 29

def proxy
  @proxy
end

#ssl_optionsObject (readonly)

Returns the value of attribute ssl_options.



37
38
39
# File 'lib/proxy_fetcher/client/request.rb', line 37

def ssl_options
  @ssl_options
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



21
22
23
# File 'lib/proxy_fetcher/client/request.rb', line 21

def timeout
  @timeout
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

.execute(args) ⇒ String

Initializes a new HTTP request and processes it

Returns:

  • (String)

    response body (requested resource content)



44
45
46
# File 'lib/proxy_fetcher/client/request.rb', line 44

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

Instance Method Details

#executeString

Executes HTTP request with defined options.

Returns:

  • (String)

    response body (requested resource content)



73
74
75
76
77
78
# 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