Class: Trufflepig::Search

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

Constant Summary collapse

EXCLUDED_FILENAMES =
/#{%w{
  jquery prototype yui dojo extjs raphael zepto enyo ember modernizr
  bootstrap foundation
}.join('|')}/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Search

Returns a new instance of Search.



10
11
12
13
# File 'lib/trufflepig/search.rb', line 10

def initialize(path)
  @results = []
  @path = path
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/trufflepig/search.rb', line 3

def path
  @path
end

#resultsObject

Returns the value of attribute results.



3
4
5
# File 'lib/trufflepig/search.rb', line 3

def results
  @results
end

Instance Method Details

#performObject

Raises:

  • (Errno::ENOENT)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/trufflepig/search.rb', line 15

def perform
  raise Errno::ENOENT unless File.exists? path

  if File.directory?(path)
    Dir.chdir path
    files = {
      :html => Dir.glob(File.join("**", "*.html")),
      :js => Dir.glob(File.join("**", "*.js")),
      :css => Dir.glob(File.join("**", "*.css"))
    }
    files.each do |type, paths|
      paths.each  do |path|
        next if File.directory?(path)
        scan path unless path.split('/').last.match EXCLUDED_FILENAMES
      end
    end
  else
    scan path
  end
end

#scan(file_path) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/trufflepig/search.rb', line 36

def scan(file_path)
  return unless File.exists? file_path
  content = File.open(file_path, "rb").read

  features.each do |feature|
    next unless feature["detection_pattern"]
    results << feature if content.match(/#{feature["detection_pattern"]}/)
  end
end