Class: EsiForRack::Lookup::Http::RedirectFollower

Inherits:
Object
  • Object
show all
Defined in:
lib/esi_for_rack/lookup.rb

Defined Under Namespace

Classes: TooManyRedirects

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodyObject

Returns the value of attribute body.



14
15
16
# File 'lib/esi_for_rack/lookup.rb', line 14

def body
  @body
end

#codeObject

Returns the value of attribute code.



14
15
16
# File 'lib/esi_for_rack/lookup.rb', line 14

def code
  @code
end

#redirect_limitObject

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

#responseObject

Returns the value of attribute response.



14
15
16
# File 'lib/esi_for_rack/lookup.rb', line 14

def response
  @response
end

#urlObject

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

#loggerObject



21
22
23
# File 'lib/esi_for_rack/lookup.rb', line 21

def logger
  @logger ||= Logger.new(STDOUT)
end

#redirect_urlObject



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

#resolveObject

Raises:



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