Class: RightScraper::Builders::Filesystem

Inherits:
Base
  • 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



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

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

Instance Method Details

#finishObject

Tell the scanner we’re done.



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

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



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

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



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

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



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

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
      # bind the temporary parameters to the callback in case it is not
      # invoked immediately.
      bind_now = lambda{|file| lambda{ open(file) {|f| f.read} } }.call(fullpath)
      @scanner.notice(relative_position, &bind_now)
    end
  end
end