Module: Checkapi

Defined in:
lib/checkapi.rb,
lib/checkapi/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.runObject

Your code goes here…



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/checkapi.rb', line 7

def self.run
    options = {}
    option_parser = OptionParser.new do |opts|
      opts.banner = 'here is help messages of the command line tool.'

      opts.on('-f FILENAME', '--file FILENAME', 'file name') do |value|
        options[:filename] = value
      end

      opts.on('-k A,B', '--keyword A,B', Array, 'List of search keywords') do |value|
        options[:keywords] = value
      end

    end.parse!


    keywords = options[:keywords]
    keywords ||= {}

    if keywords.count > 0
     if which("ag").nil?
       system("brew update")
       system("brew install ag")
     end
     regex = keywords[0]
     if keywords.count > 1

       keywords.each do |keyword|

        index = keywords.index keyword 
        if index == 0 
          regex = "#{keyword}"
        elsif
          regex += "[\\s\\S]*#{keyword}" 
        end
  
       end

     end

     if File.exist? "CheckApiResult.txt" 
       system 'rm CheckApiResult.txt'
     end
     system "ag -C 200 \"#{regex}\" --heading --nonumbers > CheckApiResult.txt"

     puts "open CheckApiResult.txt to view result!"

    else

      file_name = options[:filename]
      if file_name 
       if File.exist? "CheckApiResult.txt" 
         system 'rm CheckApiResult.txt'
       end
       result_path = "#{Pathname.pwd}/CheckApiResult.txt"
       result_file = File.new(result_path , "w+")
       file_list_string = `find . -name *#{file_name}*` 
       file_list_string.each_line do |file_path|
         file_path = File.expand_path(file_path)
         file_path = file_path.strip
         if File.exist? file_path
           content = File.read(file_path)
           result_file << content 
           result_file << "\n\n"
         else
           puts "#{file_path}not exist!"
         end
       end
       result_file.close
      end

    end

end

.which(cmd) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/checkapi.rb', line 83

def self.which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each { |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    }
  end
  return nil
end