Class: HttpGet::Client

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

Defined Under Namespace

Classes: BlockedError, MissingError, RedirectError

Instance Method Summary collapse

Constructor Details

#initialize(user_agent: USER_AGENTS.sample) ⇒ Client

Returns a new instance of Client.



7
8
9
# File 'lib/http_get/client.rb', line 7

def initialize(user_agent: USER_AGENTS.sample)
  @opts = { agent_name: user_agent }
end

Instance Method Details

#get(url, params = {}, redirects: 0, after_success: ->(resp, _self) { resp }) ⇒ Object

Raises:



18
19
20
21
22
23
24
25
26
# File 'lib/http_get/client.rb', line 18

def get(url, params = {}, redirects: 0, after_success: ->(resp, _self) { resp })
  raise RedirectError if redirects > 5

  url = Addressable::URI.parse(url).normalize.to_s

  resp = HTTPClient.new(@opts).get(url, params)

  after_success.call(resp, self)
end

#with_proxy(proxy) ⇒ Object



11
12
13
14
15
16
# File 'lib/http_get/client.rb', line 11

def with_proxy(proxy)
  host, port, user, password = proxy
  proxy_str = [[user, password].join(':'), "#{host}:#{port}"].join('@')
  @opts = @opts.merge({ proxy:  'http://' + proxy_str })
  self
end