Class: HttpGet::Client
- Inherits:
-
Object
- Object
- HttpGet::Client
- Defined in:
- lib/http_get/client.rb
Defined Under Namespace
Classes: BlockedError, MissingError, RedirectError
Instance Method Summary collapse
- #get(url, params = {}, redirects = 0, after_success: ->(resp) { resp }, before_redirect: ->(url) { url }, blocked_condition: ->(resp) { false }) ⇒ Object
-
#initialize(user_agent: USER_AGENTS.sample) ⇒ Client
constructor
A new instance of Client.
- #with_proxy(proxy) ⇒ Object
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) { resp }, before_redirect: ->(url) { url }, blocked_condition: ->(resp) { false }) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/http_get/client.rb', line 18 def get(url, params = {}, redirects = 0, after_success: ->(resp) { resp }, before_redirect: ->(url) { url }, blocked_condition: ->(resp) { false }) raise RedirectError if redirects > 5 url = Addressable::URI.parse(url).normalize.to_s resp = HTTPClient.new(@opts).get(url, params) if resp.status_code == 200 instance_eval { after_success.call(resp) } elsif resp.status_code == 301 redirect_url = before_redirect.call(resp.header['Location'].first) get(redirect_url, params, redirects + 1, after_success: after_success, before_redirect: before_redirect, blocked_condition: blocked_condition) elsif blocked_condition.call(resp) raise BlockedError else raise MissingError end 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 |