class Schleuder::Http
Attributes
request[R]
response[R]
Public Class Methods
get(url)
click to toggle source
# File lib/schleuder/http.rb, line 29 def self.get(url) nth_attempt ||= 1 new(url).run rescue NetworkError => error nth_attempt += 1 if nth_attempt < 4 retry else return error end end
new(url, options={})
click to toggle source
# File lib/schleuder/http.rb, line 9 def initialize(url, options={}) @request = Typhoeus::Request.new(url, default_options.merge(options)) end
Public Instance Methods
run()
click to toggle source
# File lib/schleuder/http.rb, line 13 def run @response = @request.run if @response.success? @response.body elsif @response.timed_out? raise_network_error(response, 'HTTP Request timed out.') elsif @response.code == 404 NotFoundError.new elsif @response.code == 0 # This happens e.g. if no response could be received. raise_network_error(@response, 'No HTTP response received.') else RuntimeError.new(@response.body.to_s.presence || @response.return_message) end end
Private Instance Methods
default_options()
click to toggle source
# File lib/schleuder/http.rb, line 49 def default_options { followlocation: true, proxy: Conf.http_proxy.presence } end
raise_network_error(response, fallback_msg)
click to toggle source
# File lib/schleuder/http.rb, line 43 def raise_network_error(response, fallback_msg) raise NetworkError.new( response.body.to_s.presence || response.return_message || fallback_msg ) end