class RocketJob::Category::Input

Define the layout for each category of input or output data

Public Instance Methods

cleanse_header!() click to toggle source

Cleanses the header column names when ‘cleanse_header` is true

# File lib/rocket_job/category/input.rb, line 89
def cleanse_header!
  return unless header_cleanser == :default

  ignored_columns = tabular.header.cleanse!
  logger.warn("Stripped out invalid columns from custom header", ignored_columns) unless ignored_columns.empty?

  self.columns = tabular.header.columns
end
data_store(job) click to toggle source
# File lib/rocket_job/category/input.rb, line 110
def data_store(job)
  RocketJob::Sliced::Input.new(
    collection_name: build_collection_name(:input, job),
    slice_class:     serializer_class,
    slice_size:      slice_size
  )
end
extract_header_callback(on_first) click to toggle source

Return a lambda to extract the header row from the uploaded file.

# File lib/rocket_job/category/input.rb, line 146
def extract_header_callback(on_first)
  return on_first unless tabular? && tabular.header?

  case mode
  when :line
    lambda do |line|
      tabular.parse_header(line)
      cleanse_header!
      self.columns = tabular.header.columns
      # Call chained on_first if present
      on_first&.call(line)
    end
  when :array
    lambda do |row|
      tabular.header.columns = row
      cleanse_header!
      self.columns = category.tabular.header.columns
      # Call chained on_first if present
      on_first&.call(line)
    end
  end
end
tabular() click to toggle source
# File lib/rocket_job/category/input.rb, line 98
def tabular
  @tabular ||= IOStreams::Tabular.new(
    columns:          columns,
    format:           format == :auto ? nil : format,
    format_options:   format_options&.to_h&.deep_symbolize_keys,
    file_name:        file_name,
    allowed_columns:  allowed_columns,
    required_columns: required_columns,
    skip_unknown:     skip_unknown
  )
end
upload_path(stream = nil, original_file_name: nil) click to toggle source

Returns [IOStreams::Path] of file to upload. Auto-detects file format from file name when format is :auto.

# File lib/rocket_job/category/input.rb, line 120
def upload_path(stream = nil, original_file_name: nil)
  unless stream || file_name
    raise(ArgumentError, "Either supply a file name to upload, or set input_collection.file_name first")
  end

  path           = IOStreams.new(stream || file_name)
  path.file_name = original_file_name if original_file_name
  self.file_name = path.file_name

  # Auto detect the format based on the upload file name if present.
  if format == :auto
    self.format = path.format || :csv
    # Rebuild tabular with new values.
    @tabular = nil
  end

  # Remove non-printable characters from tabular input formats.
  if tabular?
    # Cannot change the length of fixed width lines.
    replace = format == :fixed ? " " : ""
    path.option_or_stream(:encode, encoding: "UTF-8", cleaner: :printable, replace: replace)
  end
  path
end