Module: Thermite::Util

Included in:
Tasks
Defined in:
lib/thermite/util.rb

Overview

Utility methods

Instance Method Summary collapse

Instance Method Details

#debug(msg) ⇒ Object

Logs a debug message to the specified config.debug_filename, if set.


30
31
32
33
34
35
36
37
# File 'lib/thermite/util.rb', line 30

def debug(msg)
  # 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

#http_get(uri, retries_left = 10) ⇒ Object

Wrapper for a Net::HTTP GET request that handles redirects.

:nocov:

Raises:

  • (RedirectError)

43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/thermite/util.rb', line 43

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