Class: Embulk::Input::GoogleSpreadsheets::Pager

Inherits:
Object
  • Object
show all
Defined in:
lib/embulk/input/google_spreadsheets/pager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task) ⇒ Pager

Returns a new instance of Pager.



9
10
11
12
13
14
15
16
17
# File 'lib/embulk/input/google_spreadsheets/pager.rb', line 9

def initialize(task)
  @start_row = task['start_row']
  @start_column = task['start_column']
  @end_row = task['end_row']
  @end_column = task['end_column']
  @max_fetch_rows = task['max_fetch_rows']

  validate!
end

Instance Attribute Details

#end_columnObject (readonly)

Returns the value of attribute end_column.



7
8
9
# File 'lib/embulk/input/google_spreadsheets/pager.rb', line 7

def end_column
  @end_column
end

#end_rowObject (readonly)

Returns the value of attribute end_row.



7
8
9
# File 'lib/embulk/input/google_spreadsheets/pager.rb', line 7

def end_row
  @end_row
end

#max_fetch_rowsObject (readonly)

Returns the value of attribute max_fetch_rows.



7
8
9
# File 'lib/embulk/input/google_spreadsheets/pager.rb', line 7

def max_fetch_rows
  @max_fetch_rows
end

#start_columnObject (readonly)

Returns the value of attribute start_column.



7
8
9
# File 'lib/embulk/input/google_spreadsheets/pager.rb', line 7

def start_column
  @start_column
end

#start_rowObject (readonly)

Returns the value of attribute start_row.



7
8
9
# File 'lib/embulk/input/google_spreadsheets/pager.rb', line 7

def start_row
  @start_row
end

Instance Method Details

#each_record(client, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/embulk/input/google_spreadsheets/pager.rb', line 23

def each_record(client, &block)
  max_row_num = max_accessible_row_num(client)

  total_fetched_rows = 0
  last_fetched_row_num = start_row - 1
  while true do
    start_row_num = last_fetched_row_num + 1
    end_row_num = last_fetched_row_num + max_fetch_rows
    if end_row_num >= max_row_num
      end_row_num = max_row_num
    end

    range = range(start_row_num, end_row_num)
    page = client.worksheet_values(range)
    unless page # no values
      logger.warn { '`embulk-input-google_spreadsheets`: no data is found.' } if total_fetched_rows <= 0
      break
    end

    num_fetched_rows = 0
    page.each do |record|
      break false if no_limit? and empty_record?(record)
      num_fetched_rows += 1
      yield(record)
    end
    total_fetched_rows = total_fetched_rows + num_fetched_rows
    logger.info { "`embulk-input-google_spreadsheets`: fetched #{num_fetched_rows} rows in #{range} (tatal: #{total_fetched_rows} rows)" }
    break if num_fetched_rows < max_fetch_rows

    last_fetched_row_num = end_row_num
    break if last_fetched_row_num >= max_row_num
  end
end

#loggerObject



19
20
21
# File 'lib/embulk/input/google_spreadsheets/pager.rb', line 19

def logger
  GoogleSpreadsheets.logger
end