Class: EsiForRack::Lookup::Http::RedirectFollower
- Inherits:
-
Object
- Object
- EsiForRack::Lookup::Http::RedirectFollower
- Defined in:
- lib/esi_for_rack/lookup.rb
Defined Under Namespace
Classes: TooManyRedirects
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#code ⇒ Object
Returns the value of attribute code.
-
#redirect_limit ⇒ Object
Returns the value of attribute redirect_limit.
-
#response ⇒ Object
Returns the value of attribute response.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(url, limit = 5) ⇒ RedirectFollower
constructor
A new instance of RedirectFollower.
- #logger ⇒ Object
- #redirect_url ⇒ Object
- #resolve ⇒ Object
Constructor Details
#initialize(url, limit = 5) ⇒ RedirectFollower
Returns a new instance of RedirectFollower.
16 17 18 19 |
# File 'lib/esi_for_rack/lookup.rb', line 16 def initialize(url, limit=5) @url, @redirect_limit = url, limit logger.level = Logger::INFO end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
14 15 16 |
# File 'lib/esi_for_rack/lookup.rb', line 14 def body @body end |
#code ⇒ Object
Returns the value of attribute code.
14 15 16 |
# File 'lib/esi_for_rack/lookup.rb', line 14 def code @code end |
#redirect_limit ⇒ Object
Returns the value of attribute redirect_limit.
14 15 16 |
# File 'lib/esi_for_rack/lookup.rb', line 14 def redirect_limit @redirect_limit end |
#response ⇒ Object
Returns the value of attribute response.
14 15 16 |
# File 'lib/esi_for_rack/lookup.rb', line 14 def response @response end |
#url ⇒ Object
Returns the value of attribute url.
14 15 16 |
# File 'lib/esi_for_rack/lookup.rb', line 14 def url @url end |
Instance Method Details
#logger ⇒ Object
21 22 23 |
# File 'lib/esi_for_rack/lookup.rb', line 21 def logger @logger ||= Logger.new(STDOUT) end |
#redirect_url ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/esi_for_rack/lookup.rb', line 47 def redirect_url if response['location'].nil? response.body.match(/<a href=\"([^>]+)\">/i)[1] else response['location'] end end |
#resolve ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/esi_for_rack/lookup.rb', line 25 def resolve raise TooManyRedirects if redirect_limit < 0 self.response = Net::HTTP.get_response(URI.parse(url)) logger.info "redirect limit: #{redirect_limit}" logger.info "response code: #{response.code}" logger.debug "response body: #{response.body}" if response.kind_of?(Net::HTTPRedirection) self.url = redirect_url self.redirect_limit -= 1 logger.info "redirect found, headed to #{url}" resolve end self.body = response.body self.code = response.code self end |