Class: RightScraper::Builders::Filesystem
- Defined in:
- lib/right_scraper/builders/filesystem.rb
Overview
Build metadata by scanning the filesystem.
Instance Method Summary collapse
-
#finish ⇒ Object
Tell the scanner we’re done.
-
#go(dir, resource) ⇒ Object
Run builder for this resource.
-
#initialize(options = {}) ⇒ Filesystem
constructor
Create a new filesystem scanner.
- #maybe_scan(directory, position) ⇒ Object
-
#scan(directory, position) ⇒ Object
Scan the contents of directory.
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(={}) super @scanner = .fetch(:scanner) @ignorable_paths = [:ignorable_paths] end |
Instance Method Details
#finish ⇒ Object
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 |