Class: HashThatTree::HashIt

Inherits:
Object
  • Object
show all
Defined in:
lib/hashit.rb

Overview

This class accepts a folder array and provides methods to iterate through all files in the folder creating a hash of each file within. The results are displayed to Standard Out in csv, html, json or standard format

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, folders) ⇒ HashIt

initialize the class with the folders to be processed



17
18
19
20
21
22
23
# File 'lib/hashit.rb', line 17

def initialize(options, folders )
	   @format = options['output']
	   @folders = folders
	   @file_data = []
	   @error_data = []
	 validate
end

Instance Attribute Details

#error_dataObject

the container for the files that could not be processed



14
15
16
# File 'lib/hashit.rb', line 14

def error_data
  @error_data
end

#file_dataObject

the container for the hashing results



13
14
15
# File 'lib/hashit.rb', line 13

def file_data
  @file_data
end

#foldersObject

path to folder containing files to hash



12
13
14
# File 'lib/hashit.rb', line 12

def folders
  @folders
end

#formatObject

the format to output the results to - standard, csv, html or json



11
12
13
# File 'lib/hashit.rb', line 11

def format
  @format
end

Instance Method Details

#create_hash_resultsObject

results of the comparisson



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hashit.rb', line 37

def create_hash_results
  
  @folders.each do |folder|
    Dir.foreach(folder) do |item|
      begin
        next if item == '.' or item == '..'
        fullfilename = File.expand_path(folder, item)
        the_hash = Digest::MD5.hexdigest(File.read(File.join(File.expand_path(folder), item.downcase)))
        @file_data << {:filename=>item, :folder=>folder, :filehash => the_hash}
      rescue
        @error_data << {:error=>"Skipped#{File.expand_path(folder, item)}"}
      end
    end
  end
end

#validateObject

Validates the supplied folders ensuring they exist



26
27
28
29
30
31
32
33
# File 'lib/hashit.rb', line 26

def validate
	@folders.each do |item|
      if(item==nil) || (item=="") || !Dir.exists?(item)
        puts "a valid folder path is required as argument #{item}"
        exit
      end
    end
end