Class: Unwind::RedirectFollower

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_url, limit = 5) ⇒ RedirectFollower

Returns a new instance of RedirectFollower.



14
15
16
17
18
# File 'lib/unwind.rb', line 14

def initialize(original_url, limit=5)
 @original_url, @redirect_limit = original_url, limit
 @redirects = []
 
end

Instance Attribute Details

#final_urlObject (readonly)

Returns the value of attribute final_url.



12
13
14
# File 'lib/unwind.rb', line 12

def final_url
  @final_url
end

#original_urlObject (readonly)

Returns the value of attribute original_url.



12
13
14
# File 'lib/unwind.rb', line 12

def original_url
  @original_url
end

#redirect_limitObject (readonly)

Returns the value of attribute redirect_limit.



12
13
14
# File 'lib/unwind.rb', line 12

def redirect_limit
  @redirect_limit
end

#redirectsObject (readonly)

Returns the value of attribute redirects.



12
13
14
# File 'lib/unwind.rb', line 12

def redirects
  @redirects
end

#responseObject (readonly)

Returns the value of attribute response.



12
13
14
# File 'lib/unwind.rb', line 12

def response
  @response
end

Class Method Details

.resolve(original_url, limit = 5) ⇒ Object



44
45
46
# File 'lib/unwind.rb', line 44

def self.resolve(original_url, limit=5)
  new(original_url, limit).resolve
end

Instance Method Details

#redirected?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/unwind.rb', line 20

def redirected? 
  !(self.final_url == self.original_url)
end

#resolve(current_url = nil, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/unwind.rb', line 24

def resolve(current_url=nil, options={})

  ok_to_continue?

  current_url ||= self.original_url
  #adding this header because we really only care about resolving the url
  headers = (options || {}).merge({"accept-encoding" => "none"})
  response = Faraday.get(current_url, {}, headers)

  if is_response_redirect?(response)
    handle_redirect(redirect_url(response), current_url, response, headers)
  elsif meta_uri = meta_refresh?(current_url, response)
    handle_redirect(meta_uri, current_url, response, headers)
  else
    handle_final_response(current_url, response)
  end

  self
end