Class: RightScraper::Builders::Filesystem

Inherits:
Builder
  • Object
show all
Defined in:
lib/right_scraper/builders/filesystem.rb

Overview

Build metadata by scanning the filesystem.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Filesystem

Create a new filesystem scanner. In addition to the options recognized by Builder, this class recognizes :retriever and :scanner.

Options

:scanner

Required. Scanner currently being used

:ignorable_paths

Ignore directories whose name belong to this list

Parameters

options(Hash)

scraper options



41
42
43
44
45
# File 'lib/right_scraper/builders/filesystem.rb', line 41

def initialize(options={})
  super
  @scanner = options.fetch(:scanner)
  @ignorable_paths = options[:ignorable_paths]
end

Instance Method Details

#finishObject

Tell the scanner we’re done.



48
49
50
51
# File 'lib/right_scraper/builders/filesystem.rb', line 48

def finish
  super
  @scanner.finish
end

#go(dir, resource) ⇒ Object

Run builder for this resource.

Parameters

dir(String)

directory resource exists at

resource(Object)

resource instance being built



58
59
60
61
62
63
64
# File 'lib/right_scraper/builders/filesystem.rb', line 58

def go(dir, resource)
  @logger.operation(:scanning_filesystem, "rooted at #{dir}") do
    @scanner.begin(resource)
    maybe_scan(Dir.new(dir), nil)
    @scanner.end(resource)
  end
end

#maybe_scan(directory, position) ⇒ Object



66
67
68
69
70
# File 'lib/right_scraper/builders/filesystem.rb', line 66

def maybe_scan(directory, position)
  if @scanner.notice_dir(position)
    scan(directory, position)
  end
end

#scan(directory, position) ⇒ Object

Scan the contents of directory.

Parameters

directory(Dir)

directory to scan

position(String)

relative pathname for directory from root of resource



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/right_scraper/builders/filesystem.rb', line 77

def scan(directory, position)
  directory.each do |entry|
    next if entry == '.' || entry == '..'
    next if @ignorable_paths && @ignorable_paths.include?(entry)

    fullpath = File.join(directory.path, entry)
    relative_position = position ? File.join(position, entry) : entry

    if File.directory?(fullpath)
      maybe_scan(Dir.new(fullpath), relative_position)
    else
      @scanner.notice(relative_position) do
        open(fullpath) {|f| f.read}
      end
    end
  end
end