class Stockboy::Providers::File

Get data from a local file

Allows for selecting the appropriate file to be read from the given directory by glob pattern or regex pattern. By default the :last file in the list is used, but can be controlled by sorting and reducing with the {#pick} option.

Job template DSL

provider :file do
  file_dir '/data'
  file_name /report-[0-9]+\.csv/
  pick ->(list) { list[-2] }
end

Public Class Methods

new(opts={}, &block) click to toggle source

Initialize a File provider

Calls superclass method Stockboy::Provider::new
# File lib/stockboy/providers/file.rb, line 40
def initialize(opts={}, &block)
  super(opts, &block)
  @file_dir     = opts[:file_dir]
  @file_name    = opts[:file_name]
  @file_newer   = opts[:file_newer]
  @file_smaller = opts[:file_smaller]
  @file_larger  = opts[:file_larger]
  @pick         = opts[:pick] || :last
  DSL.new(self).instance_eval(&block) if block_given?
end

Public Instance Methods

clear() click to toggle source
Calls superclass method Stockboy::Provider#clear
# File lib/stockboy/providers/file.rb, line 62
def clear
  super
  @matching_file = nil
  @data_size = nil
  @data_time = nil
end
delete_data() click to toggle source
# File lib/stockboy/providers/file.rb, line 51
def delete_data
  raise Stockboy::OutOfSequence, "must confirm #matching_file or calling #data" unless picked_matching_file?

  logger.info "Deleting file #{file_dir}/#{matching_file}"
  ::File.delete matching_file
end
matching_file() click to toggle source
# File lib/stockboy/providers/file.rb, line 58
def matching_file
  @matching_file ||= pick_from(file_list.sort)
end

Private Instance Methods

fetch_data() click to toggle source
# File lib/stockboy/providers/file.rb, line 71
def fetch_data
  errors << "file #{file_name} not found" unless matching_file
  data_file = ::File.new(matching_file, 'r') if matching_file
  validate_file(data_file)
  @data = data_file.read if valid?
end
file_list() click to toggle source
# File lib/stockboy/providers/file.rb, line 88
def file_list
  case file_name
  when Regexp
    Dir.entries(file_dir)
       .select { |i| i =~ file_name }
       .map    { |i| full_path(i) }
  when String
    Dir[full_path(file_name)]
  end
end
full_path(file_name) click to toggle source
# File lib/stockboy/providers/file.rb, line 99
def full_path(file_name)
  ::File.join(file_dir, file_name)
end
picked_matching_file?() click to toggle source
# File lib/stockboy/providers/file.rb, line 84
def picked_matching_file?
  !!@matching_file
end
read_data_size(file) click to toggle source
# File lib/stockboy/providers/file.rb, line 131
def read_data_size(file)
  @data_size ||= file.size
end
read_data_time(file) click to toggle source
# File lib/stockboy/providers/file.rb, line 135
def read_data_time(file)
  @data_time ||= file.mtime
end
validate() click to toggle source
# File lib/stockboy/providers/file.rb, line 78
def validate
  errors << "file_dir must be specified" if file_dir.blank?
  errors << "file_name must be specified" if file_name.blank?
  errors.empty?
end
validate_file(data_file) click to toggle source
# File lib/stockboy/providers/file.rb, line 103
def validate_file(data_file)
  return errors << "no matching files" unless data_file
  validate_file_newer(data_file)
  validate_file_smaller(data_file)
  validate_file_larger(data_file)
end
validate_file_larger(data_file) click to toggle source
# File lib/stockboy/providers/file.rb, line 124
def validate_file_larger(data_file)
  read_data_size(data_file)
  if file_larger && data_size < file_larger
    errors << "file size smaller than #{file_larger}"
  end
end
validate_file_newer(data_file) click to toggle source
# File lib/stockboy/providers/file.rb, line 110
def validate_file_newer(data_file)
  read_data_time(data_file)
  if file_newer && data_time < file_newer
    errors << "no new files since #{file_newer}"
  end
end
validate_file_smaller(data_file) click to toggle source
# File lib/stockboy/providers/file.rb, line 117
def validate_file_smaller(data_file)
  read_data_size(data_file)
  if file_smaller && data_size > file_smaller
    errors << "file size larger than #{file_smaller}"
  end
end