Class: Extractor

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

Instance Method Summary collapse

Instance Method Details

#get_directory(args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/extractor.rb', line 33

def get_directory(args)
  begin
    case args.count
      when 0
        Dir.pwd
      when 1
        File.absolute_path(args.first)
      else
        raise ArgumentError, 'Arguments limit breached'
    end
  rescue ArgumentError => e
    show_error('Unable to set directory', e)
  end
end

#op_file(dir, options) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/extractor.rb', line 48

def op_file(dir,options)
  begin
    attr_labels = %w[directory file_name latitude longitude]
    dir_name = File.basename(dir)
    if options[:html]
      Template::Html.new(dir_name, attr_labels)
    else
      Template::Csv.new(dir_name, attr_labels)
    end

  rescue SystemCallError => e
    STDERR.puts 'Unable to initialize the output file'
    STDERR.puts 'Make sure you have write permission to your pwd'
    if options[:html]
      STDERR.puts 'If you have not installed the gem, check your config file'
    end
    STDERR.puts e.message
    exit
  end
end

#perform(options, args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/extractor.rb', line 8

def perform(options, args)
  show_throughput('Setting directory', options[:verbose])
  dir = get_directory(args)

  show_throughput('Create output file', options[:verbose])
  output_file = op_file(dir, options)

  begin
    show_throughput('Scan Started', options[:verbose])
    FileScanner.new(dir, output_file, options[:verbose]).begin_scan
  rescue SystemCallError => e
    show_error('Unable to scan this directory', e)
  rescue StandardError => e
    show_error('', e)
  end
  show_throughput('Scan Completed', options[:verbose])

  show_throughput('Finalising O/P File', options[:verbose])
  output_file.close

  show_throughput('Exit', options[:verbose])
  show_throughput("OutPut Recorded in file: #{output_file.file_name}", true)
  exit
end

#show_error(msg, e) ⇒ Object



73
74
75
76
77
# File 'lib/extractor.rb', line 73

def show_error(msg, e)
  STDERR.puts msg
  STDERR.puts e.message
  exit
end

#show_throughput(msg, verbose) ⇒ Object



69
70
71
# File 'lib/extractor.rb', line 69

def show_throughput(msg, verbose)
  puts msg if verbose
end