Class: ChupaText::Decomposers::CSV

Inherits:
ChupaText::Decomposer show all
Includes:
Loggable
Defined in:
lib/chupa-text/decomposers/csv.rb

Instance Method Summary collapse

Methods inherited from ChupaText::Decomposer

#initialize, registry, #target_score

Constructor Details

This class inherits a constructor from ChupaText::Decomposer

Instance Method Details

#decompose(data) {|text_data| ... } ⇒ Object

Yields:

  • (text_data)

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/chupa-text/decomposers/csv.rb', line 38

def decompose(data)
  text = ""
  data.open do |input|
    begin
      csv = ::CSV.new(input, liberal_parsing: true)
      csv.each do |row|
        text << row.join("\t")
        text << "\n"
      end
    rescue ::CSV::MalformedCSVError => csv_error
      error do
        message = "#{log_tag} Failed to parse CSV: "
        message << "#{csv_error.class}: #{csv_error.message}\n"
        message << csv_error.backtrace.join("\n")
        message
      end
      return
    end
  end

  text_data = TextData.new(text, :source_data => data)
  if data.need_screenshot?
    text_data.screenshot = create_screenshot(data, text)
  end

  yield(text_data)
end

#target?(data) ⇒ Boolean

Returns:

  • (Boolean)

27
28
29
30
31
32
33
34
35
36
# File 'lib/chupa-text/decomposers/csv.rb', line 27

def target?(data)
  return true if data.mime_type == "text/csv"

  if data.text_plain? and
      (data["source-mime-types"] || []).include?("text/csv")
    return false
  end

  data.extension == "csv"
end