module SonyCameraRemoteAPI::Utils

Module providing utility methods

Public Instance Methods

generate_sequencial_filenames(prefix, ext, start: nil, num: nil, dir: nil) click to toggle source

Get file name in the format of <prefix>_<num>.<ext> If there are files that matches this format, get the next number of it. @param [String] prefix @param [String] ext @param [Fixnum] start @param [Fixnum] num @param [String] dir

# File lib/sony_camera_remote_api/utils.rb, line 14
def generate_sequencial_filenames(prefix, ext, start: nil, num: nil, dir: nil)
  if start
    count = start
  else
    count = get_next_file_number prefix, ext, dir: dir
  end
  gen = Enumerator.new do |y|
    loop do
      y << "#{prefix}_#{count}.#{ext}"
      count += 1
    end
  end
  if num
    return (0..(num-1)).map { gen.next }
  else
    return gen
  end
end
get_next_file_number(prefix, ext, dir: nil) click to toggle source

Get the next file number by searching files with format '<prefix>_d+.<ext>' in <dir>. @param [String] prefix @param [String] ext @param [String] dir

# File lib/sony_camera_remote_api/utils.rb, line 38
def get_next_file_number(prefix, ext, dir: nil)
  numbers = []
  Dir["#{dir}/*"].map do |f|
    begin
      num = f[/#{dir}\/#{prefix}_(\d+).#{ext}/, 1]
      numbers << num.to_i if num.present?
    rescue
      nil
    end
  end
  if numbers.empty?
    0
  else
    numbers.sort[-1] + 1
  end
end
partial_and_unique_match(pattern, candidates) click to toggle source

Search pattern in candidates. @param [String] pattern Pattern @param [Array<String>] candidates Candidates @return [Array<String, Fixnum>] matched candidate and the number of matched candidates.

# File lib/sony_camera_remote_api/utils.rb, line 60
def partial_and_unique_match(pattern, candidates)
  result = candidates.find { |c| c == pattern }
  return result, 1 if result
  result = candidates.select { |c| c =~ /#{pattern}/i }
  return result[0], 1 if result.size == 1
  result = candidates.select { |c| c =~ /#{pattern}/ }
  return result[0], 1 if result.size == 1
  return nil, result.size
end
print_array_in_columns(array, horizon, space, threshold) click to toggle source

Print array. @param [Array] array @param [Fixnum] horizon @param [Fixnum] space @param [Fixnum] threshold