Class: Bisu::Source::GoogleSheet

Inherits:
Object
  • Object
show all
Defined in:
lib/bisu/source/google_sheet.rb

Instance Method Summary collapse

Constructor Details

#initialize(sheet_id, keys_column) ⇒ GoogleSheet

Returns a new instance of GoogleSheet.



7
8
9
10
# File 'lib/bisu/source/google_sheet.rb', line 7

def initialize(sheet_id, keys_column)
  @sheet_id = sheet_id
  @key_column = keys_column
end

Instance Method Details

#to_i18Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bisu/source/google_sheet.rb', line 12

def to_i18
  raw = raw_data(@sheet_id)

  Logger.info("Downloading dictionary from Google Sheet...")

  non_language_columns = ["id", "updated", "category", "title", "content", "link", @key_column]

  kb = {}
  raw["entry"].each do |entry|
    unless (key = entry[@key_column]) && key = key.first
      raise "Bisu::Source::GoogleSheet: Cannot find key column '#{@key_column}'"
    end

    entry.select { |c| !non_language_columns.include?(c) }.each do |lang, texts|
      kb[lang] ||= {}
      kb[lang][key] = texts.first unless texts.first == {}
    end
  end

  Logger.info("Google Sheet parsed successfully!")
  Logger.info("Found #{kb.count} languages.")

  kb
end