Class: SimpleImagesDownloader::SourceFile

Inherits:
Object
  • Object
show all
Includes:
Validatable
Defined in:
lib/simple_images_downloader/source_file.rb

Overview

SourceFile class Responsible for opening the file of URLs and validating it

Examples:

SimpleImagesDownloader::SourceFile.new('./urls.txt').each_line do |line|
  puts line
end

Instance Method Summary collapse

Methods included from Validatable

#validate!

Constructor Details

#initialize(path, validators = [FilePersistanceValidator.new, FileAccessibilityValidator.new]) ⇒ SourceFile

Returns a new instance of SourceFile.

Parameters:

  • path (String)

    path to file

  • validators (Array) (defaults to: [FilePersistanceValidator.new, FileAccessibilityValidator.new])

    array of validators



17
18
19
20
# File 'lib/simple_images_downloader/source_file.rb', line 17

def initialize(path, validators = [FilePersistanceValidator.new, FileAccessibilityValidator.new])
  @path       = path
  @validators = validators
end

Instance Method Details

#each_line {|line| ... } ⇒ Object

Yields:

  • (line)

    passes each line of file to block

Yield Parameters:

  • line (String)

    line of file



24
25
26
27
28
29
30
31
32
# File 'lib/simple_images_downloader/source_file.rb', line 24

def each_line(&block)
  validate!({ path: @path })

  begin
    file.each(chomp: true, &block)
  ensure
    file.close
  end
end