Class: Clients::HttpClient
- Inherits:
-
Object
- Object
- Clients::HttpClient
- Defined in:
- lib/clients/http_client.rb,
lib/clients/http_client/response.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Response
Class Attribute Summary collapse
Instance Attribute Summary collapse
- #cookies ⇒ Object
-
#proxy ⇒ Object
Returns the value of attribute proxy.
- #user_agent ⇒ Object
Class Method Summary collapse
Instance Method Summary collapse
- #get(url, **options, &block) ⇒ Object
- #has_cookies? ⇒ Boolean
- #head(url, **options, &block) ⇒ Object
-
#initialize(proxy: nil, logger: nil) ⇒ HttpClient
constructor
A new instance of HttpClient.
- #post(url, **options, &block) ⇒ Object
- #proxy? ⇒ Boolean
- #request(verb, url, **options) ⇒ Object
- #reset ⇒ Object
- #reset_cookies ⇒ Object
- #reset_proxy ⇒ Object
- #reset_user_agent ⇒ Object
- #store_cookies(cookies) ⇒ Object
Constructor Details
#initialize(proxy: nil, logger: nil) ⇒ HttpClient
Returns a new instance of HttpClient.
11 12 13 14 15 16 17 |
# File 'lib/clients/http_client.rb', line 11 def initialize( proxy: nil, logger: nil ) @proxy = proxy @logger = logger end |
Class Attribute Details
.user_agents ⇒ Object
93 94 95 |
# File 'lib/clients/http_client.rb', line 93 def user_agents @user_agents ||= File.readlines user_agents_path end |
Instance Attribute Details
#cookies ⇒ Object
80 81 82 |
# File 'lib/clients/http_client.rb', line 80 def ||= HTTP::CookieJar.new end |
#proxy ⇒ Object
Returns the value of attribute proxy.
9 10 11 |
# File 'lib/clients/http_client.rb', line 9 def proxy @proxy end |
#user_agent ⇒ Object
84 85 86 |
# File 'lib/clients/http_client.rb', line 84 def user_agent @user_agent ||= sample_user_agent end |
Class Method Details
.user_agents_path ⇒ Object
97 98 99 |
# File 'lib/clients/http_client.rb', line 97 def user_agents_path File.join File.dirname(__FILE__), "../../data/user_agents.txt" end |
Instance Method Details
#get(url, **options, &block) ⇒ Object
27 28 29 |
# File 'lib/clients/http_client.rb', line 27 def get(url, **, &block) request :get, url, **, &block end |
#has_cookies? ⇒ Boolean
23 24 25 |
# File 'lib/clients/http_client.rb', line 23 def .any? end |
#head(url, **options, &block) ⇒ Object
35 36 37 |
# File 'lib/clients/http_client.rb', line 35 def head(url, **, &block) request :head, url, **, &block end |
#post(url, **options, &block) ⇒ Object
31 32 33 |
# File 'lib/clients/http_client.rb', line 31 def post(url, **, &block) request :post, url, **, &block end |
#proxy? ⇒ Boolean
19 20 21 |
# File 'lib/clients/http_client.rb', line 19 def proxy? !!proxy end |
#request(verb, url, **options) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/clients/http_client.rb', line 39 def request(verb, url, **) = .merge(ssl_context: ssl_context) request = setup_request .delete(:follow_redirects) request = yield request if block_given? response = make_request(request, verb, url, **) Response.new response rescue raise HttpClientError.new(url: url, proxy: proxy) end |
#reset ⇒ Object
52 53 54 55 56 |
# File 'lib/clients/http_client.rb', line 52 def reset reset_user_agent reset_proxy end |
#reset_cookies ⇒ Object
76 77 78 |
# File 'lib/clients/http_client.rb', line 76 def = nil end |
#reset_proxy ⇒ Object
62 63 64 65 66 67 |
# File 'lib/clients/http_client.rb', line 62 def reset_proxy return unless proxy log "Reset proxy to #{proxy.host}:#{proxy.port}" proxy.reset! end |
#reset_user_agent ⇒ Object
58 59 60 |
# File 'lib/clients/http_client.rb', line 58 def reset_user_agent self.user_agent = nil end |
#store_cookies(cookies) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/clients/http_client.rb', line 69 def () return if .empty? .each do || self. << end end |