class MagicReveal::Conductor
Fetchs a github zip file and unpacks it for use.
Attributes
enable_warnings[R]
url[R]
Public Class Methods
new(url = nil)
click to toggle source
# File lib/magic_reveal/conductor.rb, line 12 def initialize(url = nil) self.url = url unless url.nil? end
Public Instance Methods
fetch(save_path, limit = 5)
click to toggle source
# File lib/magic_reveal/conductor.rb, line 20 def fetch(save_path, limit = 5) # rubocop:disable MethodLength fail TooManyRedirects if limit <= 0 save_path = Pathname.new save_path request = Net::HTTP::Get.new url.path response = Net::HTTP.start(url.host, url.port, use_ssl: url.scheme == 'https') { |http| http.request(request) } case response when Net::HTTPSuccess then save_path.open('w') { |fp| fp.write response.body } when Net::HTTPRedirection then self.url = response['location'] warn "redirected to #{url}" if enable_warnings fetch(save_path, limit - 1) else fail Error, "Huh? #{response.value}" end end
unpack(zip_file, directory)
click to toggle source
# File lib/magic_reveal/conductor.rb, line 39 def unpack(zip_file, directory) directory = Pathname.new directory fail Error, "Directory '#{directory}' already exists." if directory.exist? Archive::Zip.extract zip_file.to_s, directory.to_s # Unwrap the outer-most unzipped directory. wrapper_dir = directory.children.first wrapper_dir.children.each { |c| c.rename(directory + c.basename) } wrapper_dir.rmdir end
url=(url)
click to toggle source
# File lib/magic_reveal/conductor.rb, line 16 def url=(url) @url = URI.parse url end