Class: MagicReveal::Conductor
- Inherits:
-
Object
- Object
- MagicReveal::Conductor
- Defined in:
- lib/magic_reveal/conductor.rb
Overview
Fetchs a github zip file and unpacks it for use.
Instance Attribute Summary collapse
-
#enable_warnings ⇒ Object
readonly
Returns the value of attribute enable_warnings.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#fetch(save_path, limit = 5) ⇒ Object
rubocop:disable MethodLength.
-
#initialize(url = nil) ⇒ Conductor
constructor
A new instance of Conductor.
- #unpack(zip_file, directory) ⇒ Object
Constructor Details
#initialize(url = nil) ⇒ Conductor
Returns a new instance of Conductor.
12 13 14 |
# File 'lib/magic_reveal/conductor.rb', line 12 def initialize(url = nil) self.url = url unless url.nil? end |
Instance Attribute Details
#enable_warnings ⇒ Object (readonly)
Returns the value of attribute enable_warnings.
10 11 12 |
# File 'lib/magic_reveal/conductor.rb', line 10 def enable_warnings @enable_warnings end |
#url ⇒ Object
Returns the value of attribute url.
10 11 12 |
# File 'lib/magic_reveal/conductor.rb', line 10 def url @url end |
Instance Method Details
#fetch(save_path, limit = 5) ⇒ Object
rubocop:disable MethodLength
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# 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) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# 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 |