class Imgfetcha::FileReader

Attributes

input_file[R]
result[R]

Public Class Methods

new(options) click to toggle source
# File lib/imgfetcha/file_reader.rb, line 5
def initialize(options)
  @input_file = options[:input_file]
  @verbose    = options[:verbose]
end

Public Instance Methods

run() click to toggle source
# File lib/imgfetcha/file_reader.rb, line 10
def run
  read_file
  filter_urls
  report_results if @verbose
  @result
end

Private Instance Methods

filter_urls() click to toggle source
# File lib/imgfetcha/file_reader.rb, line 26
def filter_urls
  @result = @contents.select { |url| URI.parse(url).is_a?(URI::HTTP) }
  raise NoUrlsFoundError if @result.empty?

  # TODO: group valid and unvalid URLs, warn about invalid ones

  @result
end
read_file() click to toggle source
# File lib/imgfetcha/file_reader.rb, line 19
def read_file
  raise InputFileNotSpecifiedError unless @input_file

  # Expand path so that Dir#chdir would understand paths relative to home
  @contents = File.read(File.expand_path(@input_file)).split
end
report_results() click to toggle source
# File lib/imgfetcha/file_reader.rb, line 35
def report_results
  puts "Found #{@result.count} URLs in #{@contents.count} lines:"
  puts @result
end