Class: Wrest::Native::Redirection

Inherits:
Response
  • Object
show all
Defined in:
lib/wrest/native/redirection.rb

Overview

Constructed by Wrest::Response.new if the HTTP response code is 3xx (en.wikipedia.org/wiki/300_Multiple_Choices#3xx_Redirection)

This class is necessary because Net::HTTP doesn't seem to support redirection natively.

Instance Attribute Summary

Attributes inherited from Response

#deserialised_body, #http_response

Instance Method Summary (collapse)

Methods inherited from Response

#==, #cache_control_headers, #cacheable?, #can_be_validated?, #code_cacheable?, #connection_closed?, #current_age, #deserialise, #deserialise_using, #expired?, #expires, #expires_not_in_its_past?, #expires_not_in_our_past?, #freshness_lifetime, #hash, #headers, #initialize, #initialize_copy, #last_modified, #max_age, new, #no_cache_flag_not_set?, #no_store_flag_not_set?, #parse_datefield, #pragma_nocache_not_set?, #recalculate_cache_control_headers, #recalculate_freshness_lifetime, #response_date, #vary_tag_not_set?

Methods included from HttpCodes

#accepted?, #bad_request?, #created?, #forbidden?, #found?, #internal_server_error?, #method_not_allowed?, #moved_permanently?, #no_content?, #not_acceptable?, #not_found?, #not_modified?, #ok?, #see_other?, #temporary_redirect?, #unauthorized?, #unprocessable_entity?

Constructor Details

This class inherits a constructor from Wrest::Native::Response

Instance Method Details

- (Object) follow(redirect_request_options = {})

A get is invoked on the url stored in the response headers under the key 'location' and the new Response is returned.

The follow_redirects_count and follow_redirects_limit options should be present. follow_redirects_count will be incremented by 1.

This method will raise a Wrest::Exceptions::AutoRedirectLimitExceeded if the follow_redirects_count equals the follow_redirects_limit.



27
28
29
30
31
32
33
34
35
# File 'lib/wrest/native/redirection.rb', line 27

def follow(redirect_request_options = {})
  target = self['location']
  redirect_request_options = redirect_request_options.clone

  raise Wrest::Exceptions::AutoRedirectLimitExceeded if (redirect_request_options[:follow_redirects_count] += 1) > redirect_request_options[:follow_redirects_limit]

  Wrest.logger.debug "-| Redirecting to #{target}"
  Wrest::Uri.new(target, redirect_request_options).get
end