Module: Nuggets::URI::RedirectMixin

Included in:
URI
Defined in:
lib/nuggets/uri/redirect_mixin.rb

Constant Summary

URI_REDIRECT_MAX_STEPS =

Maximum number of redirects to follow.

20
URI_REDIRECT_HTTP_CACHE =

Cache for HTTP objects to reuse.

::Hash.new { |h, k| h[k] = ::Net::HTTP.new(*k) }

Instance Method Summary (collapse)

Instance Method Details

- (Object) follow_redirect(uri, steps = URI_REDIRECT_MAX_STEPS, cache = URI_REDIRECT_HTTP_CACHE)

call-seq:

URI.follow_redirect(uri[, steps]) { |req, path| ... } => aResponse or +nil+

Performs any HTTP request on uri while following at most steps redirects. Accepts both strings and URI objects for uri. Defers to the given block to perform the actual request; yields the request object and the request URI string.

Returns the response object if successful, or nil if uri is not an HTTP URI, the redirect limit has been exceeded, or any connection error occurs.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/nuggets/uri/redirect_mixin.rb', line 50

def follow_redirect(uri, steps = URI_REDIRECT_MAX_STEPS, cache = URI_REDIRECT_HTTP_CACHE)
  unless uri.is_a?(::URI::HTTP)
    uri = ::URI.parse(uri.to_s)
    return unless uri.is_a?(::URI::HTTP)
  end

  req = cache[[uri.host, uri.port]]
  req.use_ssl = uri.is_a?(::URI::HTTPS)

  ctx = req.instance_variable_get(:@ssl_context) if req.use_ssl?
  ctx.verify_mode ||= ::OpenSSL::SSL::VERIFY_NONE if ctx

  res = yield req, uri.request_uri
  return res unless res.is_a?(::Net::HTTPRedirection)
  return nil unless steps > 0

  follow_redirect(res['Location'], steps - 1, cache) { |*a| yield(*a) }
rescue ::SocketError, ::Errno::EHOSTUNREACH, ::Errno::ENOENT
end

- (Object) get_redirect(uri, steps = URI_REDIRECT_MAX_STEPS)

call-seq:

URI.get_redirect(uri[, steps]) => aResponse or +nil+
URI.get_redirect(uri[, steps]) { |res| ... } => anObject or +nil+

Performs a GET request on uri while following at most steps redirects. If successful, yields the response to the given block and returns its return value, or the response itself if no block was given. Returns nil in case of failure.

See Nuggets::URI::RedirectMixin#follow_redirect for more information.



95
96
97
98
# File 'lib/nuggets/uri/redirect_mixin.rb', line 95

def get_redirect(uri, steps = URI_REDIRECT_MAX_STEPS)
  res = follow_redirect(uri, steps) { |req, path| req.get(path) }
  res && block_given? ? yield(res) : res
end

- (Object) head_redirect(uri, steps = URI_REDIRECT_MAX_STEPS)

call-seq:

URI.head_redirect(uri[, steps]) => aResponse or +nil+
URI.head_redirect(uri[, steps]) { |res| ... } => anObject or +nil+

Performs a HEAD request on uri while following at most steps redirects. If successful, yields the response to the given block and returns its return value, or the response itself if no block was given. Returns nil in case of failure.

See Nuggets::URI::RedirectMixin#follow_redirect for more information.



80
81
82
83
# File 'lib/nuggets/uri/redirect_mixin.rb', line 80

def head_redirect(uri, steps = URI_REDIRECT_MAX_STEPS)
  res = follow_redirect(uri, steps) { |req, path| req.head(path) }
  res && block_given? ? yield(res) : res
end