Class: RocketJob::Category::Input

Inherits:
Object
  • Object
show all
Includes:
Base, Plugins::Document, SemanticLogger::Loggable
Defined in:
lib/rocket_job/category/input.rb

Overview

Define the layout for each category of input or output data

Instance Method Summary collapse

Methods included from Base

#build_collection_name, #serializer_class, #tabular?

Instance Method Details

#cleanse_header!Object

Cleanses the header column names when ‘cleanse_header` is true



89
90
91
92
93
94
95
96
# 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) ⇒ Object



110
111
112
113
114
115
116
# 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) ⇒ Object

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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# 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

#tabularObject



98
99
100
101
102
103
104
105
106
107
108
# 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) ⇒ Object

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# 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