Class: Polipus::HTTP
- Inherits:
-
Object
- Object
- Polipus::HTTP
- Defined in:
- lib/polipus/http.rb
Constant Summary collapse
- REDIRECT_LIMIT =
Maximum number of redirects to follow on each get_response
5
- RESCUABLE_ERRORS =
[ EOFError, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EHOSTUNREACH, Errno::EINVAL, Errno::EPIPE, Errno::ETIMEDOUT, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, SocketError, Timeout::Error, Zlib::DataError, Zlib::GzipFile::Error ]
Instance Method Summary collapse
-
#accept_cookies? ⇒ Boolean
Does this HTTP client accept cookies from the server?.
- #cookie_jar ⇒ Object
-
#fetch_page(url, referer = nil, depth = nil) ⇒ Object
Fetch a single Page from the response of an HTTP request to url.
-
#fetch_pages(url, referer = nil, depth = nil) ⇒ Object
Create new Pages from the response of an HTTP request to url, including redirects.
-
#initialize(opts = {}) ⇒ HTTP
constructor
A new instance of HTTP.
-
#open_timeout ⇒ Object
HTTP open timeout in seconds.
-
#proxy_host ⇒ Object
The proxy address string.
-
#proxy_host_port ⇒ Object
Shorthand to get proxy info with a single call It returns an array of [‘addr’, port].
-
#proxy_port ⇒ Object
The proxy port.
-
#read_timeout ⇒ Object
HTTP read timeout in seconds.
-
#redirect_limit ⇒ Object
The maximum number of redirects to follow.
-
#user_agent ⇒ Object
The user-agent string which will be sent with each request, or nil if no such option is set.
Constructor Details
#initialize(opts = {}) ⇒ HTTP
Returns a new instance of HTTP.
27 28 29 30 31 |
# File 'lib/polipus/http.rb', line 27 def initialize(opts = {}) @connections = {} @connections_hits = {} @opts = opts end |
Instance Method Details
#accept_cookies? ⇒ Boolean
Does this HTTP client accept cookies from the server?
125 126 127 |
# File 'lib/polipus/http.rb', line 125 def @opts[:accept_cookies] end |
#cookie_jar ⇒ Object
129 130 131 132 |
# File 'lib/polipus/http.rb', line 129 def @opts[:cookie_jar] ||= ::HTTP::CookieJar.new @opts[:cookie_jar] end |
#fetch_page(url, referer = nil, depth = nil) ⇒ Object
Fetch a single Page from the response of an HTTP request to url. Just gets the final destination page.
37 38 39 |
# File 'lib/polipus/http.rb', line 37 def fetch_page(url, referer = nil, depth = nil) fetch_pages(url, referer, depth).last end |
#fetch_pages(url, referer = nil, depth = nil) ⇒ Object
Create new Pages from the response of an HTTP request to url, including redirects
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/polipus/http.rb', line 45 def fetch_pages(url, referer = nil, depth = nil) url = URI(url) pages = [] get(url, referer) do |response, code, location, redirect_to, response_time| handle_compression response pages << Page.new(location, body: response.body, code: code, headers: response.to_hash, referer: referer, depth: depth, redirect_to: redirect_to, response_time: response_time, fetched_at: Time.now.to_i) end pages rescue *RESCUABLE_ERRORS => e if verbose? puts e.inspect puts e.backtrace end [Page.new(url, error: e, referer: referer, depth: depth)] end |
#open_timeout ⇒ Object
HTTP open timeout in seconds
119 120 121 |
# File 'lib/polipus/http.rb', line 119 def open_timeout @opts[:open_timeout] end |
#proxy_host ⇒ Object
The proxy address string
88 89 90 91 |
# File 'lib/polipus/http.rb', line 88 def proxy_host return proxy_host_port.first unless @opts[:proxy_host_port].nil? @opts[:proxy_host].respond_to?(:call) ? @opts[:proxy_host].call(self) : @opts[:proxy_host] end |
#proxy_host_port ⇒ Object
Shorthand to get proxy info with a single call It returns an array of [‘addr’, port]
105 106 107 |
# File 'lib/polipus/http.rb', line 105 def proxy_host_port @opts[:proxy_host_port].respond_to?(:call) ? @opts[:proxy_host_port].call(self) : @opts[:proxy_host_port] end |
#proxy_port ⇒ Object
The proxy port
96 97 98 99 |
# File 'lib/polipus/http.rb', line 96 def proxy_port return proxy_host_port.last unless @opts[:proxy_host_port].nil? @opts[:proxy_port].respond_to?(:call) ? @opts[:proxy_port].call(self) : @opts[:proxy_port] end |
#read_timeout ⇒ Object
HTTP read timeout in seconds
112 113 114 |
# File 'lib/polipus/http.rb', line 112 def read_timeout @opts[:read_timeout] end |
#redirect_limit ⇒ Object
The maximum number of redirects to follow
73 74 75 |
# File 'lib/polipus/http.rb', line 73 def redirect_limit @opts[:redirect_limit] || REDIRECT_LIMIT end |
#user_agent ⇒ Object
The user-agent string which will be sent with each request, or nil if no such option is set
81 82 83 |
# File 'lib/polipus/http.rb', line 81 def user_agent @opts[:user_agent] end |