Class: Fetcher
- Inherits:
-
Object
- Object
- Fetcher
- Defined in:
- lib/abelard/load.rb
Overview
Wrap an HTTP session, handle making a request and following any redirects
Instance Attribute Summary collapse
-
#password ⇒ Object
Returns the value of attribute password.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #get(url, max_depth = 3) ⇒ Object
-
#initialize ⇒ Fetcher
constructor
A new instance of Fetcher.
Constructor Details
#initialize ⇒ Fetcher
Returns a new instance of Fetcher.
292 293 294 295 296 297 |
# File 'lib/abelard/load.rb', line 292 def initialize @host = nil @session = nil @user = nil @password = nil end |
Instance Attribute Details
#password ⇒ Object
Returns the value of attribute password.
298 299 300 |
# File 'lib/abelard/load.rb', line 298 def password @password end |
#user ⇒ Object
Returns the value of attribute user.
298 299 300 |
# File 'lib/abelard/load.rb', line 298 def user @user end |
Instance Method Details
#get(url, max_depth = 3) ⇒ Object
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/abelard/load.rb', line 300 def get(url, max_depth=3) if (url.host != @host) @host = url.host @session = Net::HTTP.new(@host, url.port) @session.use_ssl = (url.scheme == 'https') end request = Net::HTTP::Get.new(url) request.basic_auth(user, password) if user msg = "Reading (#{url.to_s})" msg << " as #{user}" if user $stderr.puts(msg) feedxml = @session.request(request) if (feedxml.is_a? Net::HTTPOK) yield feedxml elsif (feedxml.is_a? Net::HTTPMovedPermanently ) new_url = feedxml['Location'] if ( new_url == url.to_s ) then puts("Confused! redirect to same url #{new_url}") else if ( max_depth == 0 ) puts("Too many redirects") else puts("Redirecting to #{new_url}") get(URI(new_url), max_depth-1) { |r| yield r } end end else puts("GET returned #{feedxml.code}") puts(feedxml.body) end end |