Class: LitmusPaper::Dependency::FileContents

Inherits:
Object
  • Object
show all
Defined in:
lib/litmus_paper/dependency/file_contents.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, regex, options = {}) ⇒ FileContents

Returns a new instance of FileContents.



4
5
6
7
8
# File 'lib/litmus_paper/dependency/file_contents.rb', line 4

def initialize(path, regex, options = {})
  @path = path
  @regex = regex
  @timeout = options.fetch(:timeout, 5)
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/litmus_paper/dependency/file_contents.rb', line 10

def available?
  Timeout.timeout(@timeout) do
    if File.read(@path).match(@regex)
      true
    else
      LitmusPaper.logger.info("Available check of #{@path} failed, content did not match #{@regex.inspect}")
      false
    end
  end
rescue Timeout::Error
  LitmusPaper.logger.info("Timeout reading #{@path}")
  false
rescue => e
  LitmusPaper.logger.info("Error reading #{@path}: '#{e.message}'")
  false
end

#to_sObject



27
28
29
# File 'lib/litmus_paper/dependency/file_contents.rb', line 27

def to_s
  "Dependency::FileContents(#{@path}, #{@regex.inspect})"
end