Class: ChupaText::Decomposers::Spreadsheet

Inherits:
Decomposer
  • Object
show all
Defined in:
lib/chupa-text/decomposers/spreadsheet.rb

Constant Summary collapse

TARGET_EXTENSIONS =
["ods", "xls", "xlsx", "xlsm", "xml"]
TARGET_MIME_TYPES =
[
  "application/vnd.oasis.opendocument.spreadsheet",
  "application/vnd.ms-excel",
  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
]

Instance Method Summary collapse

Instance Method Details

#decompose(data) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/chupa-text/decomposers/spreadsheet.rb', line 31

def decompose(data)
  book = Roo::Spreadsheet.open(data.path.to_s)
  book.sheets.each do |sheet_name|
    sheet = book.sheet(sheet_name)
    body = sheet.to_csv
    text_data = TextData.new(body, source_data: data)
    text_data["name"] = sheet_name
    text_data["digest"] = Digest::SHA1.hexdigest(body)
    text_data["size"] = body.bytesize
    text_data["first-row"] = sheet.first_row
    text_data["last-row"] = sheet.last_row
    text_data["first-column"] = sheet.first_column && sheet.first_column_as_letter
    text_data["last-column"] = sheet.last_column && sheet.last_column_as_letter
    yield text_data
  end
  book.close
end

#target?(data) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/chupa-text/decomposers/spreadsheet.rb', line 18

def target?(data)
  TARGET_EXTENSIONS.include?(data.extension) or
    TARGET_MIME_TYPES.include?(data.mime_type)
end

#target_score(data) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/chupa-text/decomposers/spreadsheet.rb', line 23

def target_score(data)
  if target?(data)
    10
  else
    nil
  end
end