lib/thermite/util.rb in thermite-0.7.0 vs lib/thermite/util.rb in thermite-0.8.0

- old
+ new

@@ -16,10 +16,12 @@ # NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, # DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT # OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +require 'net/http' + module Thermite # # Utility methods # module Util @@ -30,8 +32,29 @@ # Should probably replace with a Logger return unless config.debug_filename @debug ||= File.open(config.debug_filename, 'w') @debug.write("#{msg}\n") + @debug.flush end + + # + # Wrapper for a Net::HTTP GET request that handles redirects. + # + # :nocov: + def http_get(uri, retries_left = 10) + raise RedirectError, 'Too many redirects' if retries_left.zero? + + case (response = Net::HTTP.get_response(URI(uri))) + when Net::HTTPClientError + nil + when Net::HTTPServerError + raise Net::HTTPServerException.new(response.message, response) + when Net::HTTPFound, Net::HTTPPermanentRedirect + http_get(response['location'], retries_left - 1) + else + StringIO.new(response.body) + end + end + # :nocov: end end