Class: Unwind::RedirectFollower
- Inherits:
-
Object
- Object
- Unwind::RedirectFollower
- Defined in:
- lib/unwind.rb
Instance Attribute Summary collapse
-
#final_url ⇒ Object
readonly
Returns the value of attribute final_url.
-
#original_url ⇒ Object
readonly
Returns the value of attribute original_url.
-
#redirect_limit ⇒ Object
readonly
Returns the value of attribute redirect_limit.
-
#redirects ⇒ Object
readonly
Returns the value of attribute redirects.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(original_url, limit = 5) ⇒ RedirectFollower
constructor
A new instance of RedirectFollower.
- #redirected? ⇒ Boolean
- #resolve(current_url = nil, options = {}) ⇒ Object
Constructor Details
#initialize(original_url, limit = 5) ⇒ RedirectFollower
Returns a new instance of RedirectFollower.
14 15 16 17 |
# 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_url ⇒ Object (readonly)
Returns the value of attribute final_url.
12 13 14 |
# File 'lib/unwind.rb', line 12 def final_url @final_url end |
#original_url ⇒ Object (readonly)
Returns the value of attribute original_url.
12 13 14 |
# File 'lib/unwind.rb', line 12 def original_url @original_url end |
#redirect_limit ⇒ Object (readonly)
Returns the value of attribute redirect_limit.
12 13 14 |
# File 'lib/unwind.rb', line 12 def redirect_limit @redirect_limit end |
#redirects ⇒ Object (readonly)
Returns the value of attribute redirects.
12 13 14 |
# File 'lib/unwind.rb', line 12 def redirects @redirects end |
#response ⇒ Object (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
57 58 59 |
# File 'lib/unwind.rb', line 57 def self.resolve(original_url, limit=5) new(original_url, limit).resolve end |
Instance Method Details
#redirected? ⇒ Boolean
19 20 21 |
# File 'lib/unwind.rb', line 19 def redirected? !(self.final_url == self.original_url) end |
#resolve(current_url = nil, options = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/unwind.rb', line 23 def resolve(current_url=nil, ={}) ok_to_continue? current_url ||= self.original_url #adding this header because we really only care about resolving the url headers = ( || {}).merge({"accept-encoding" => "none"}) url = URI.parse(current_url) request = Net::HTTP::Get.new(url) headers.each do |header, value| request.add_field(header, value) end response = Net::HTTP.start( url.host, url.port, :use_ssl => url.scheme == 'https' ) do |http| http.request(request) end if is_response_redirect?(response) handle_redirect(redirect_url(response), current_url, response, headers) elsif = (current_url, response) handle_redirect(, current_url, response, headers) else handle_final_response(current_url, response) end self end |