Class: Embulk::Output::GoogleSheetsRuby

Inherits:
OutputPlugin
  • Object
show all
Defined in:
lib/embulk/output/google_sheets_ruby.rb

Overview

Google Sheets Ruby output plugin for Embulk

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.transaction(config, _schema, _count) {|task| ... } ⇒ Object

Yields:

  • (task)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/embulk/output/google_sheets_ruby.rb', line 14

def self.transaction(config, _schema, _count)
  task = {
    'spreadsheet_id' => config.param('spreadsheet_id', :string),
    'range' => config.param('range', :string, default: 'A1'),
    'credentials_path' => config.param('credentials_path',
                                       :string,
                                       default: 'credentials.json'),
    'auth_method' => config.param('auth_method', :string, default: 'service_account')
  }

  yield(task)

  {}
end

Instance Method Details

#add(page) ⇒ Object



42
43
44
45
46
# File 'lib/embulk/output/google_sheets_ruby.rb', line 42

def add(page)
  page.each do |record|
    @rows << record
  end
end

#authorizeObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/embulk/output/google_sheets_ruby.rb', line 59

def authorize
  case @auth_method
  when 'service_account'
    return Google::Auth::ServiceAccountCredentials.make_creds(
      json_key_io: File.open(@credentials_path),
      scope: SCOPE
      )
  when 'authorized_user'
    return Google::Auth::UserRefreshCredentials.make_creds(
      json_key_io: File.open(@credentials_path),
      scope: SCOPE
      )
  else
    raise ConfigError.new("Unknown auth method: #{auth_method}")
  end
end

#commitObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/embulk/output/google_sheets_ruby.rb', line 48

def commit
  value_range = Google::Apis::SheetsV4::ValueRange.new
  value_range.range = @range
  value_range.major_dimension = 'ROWS'
  value_range.values = @rows

  update_sheet(value_range)

  {}
end

#initObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/embulk/output/google_sheets_ruby.rb', line 29

def init
  @spreadsheet_id = task['spreadsheet_id']
  @credentials_path = task['credentials_path']
  @range = task['range']
  @auth_method = task['auth_method']
  @rows = []
  @rows << schema.map(&:name)

  @service = Google::Apis::SheetsV4::SheetsService.new
  @service.client_options.application_name = APPLICATION_NAME
  @service.authorization = authorize
end

#update_sheet(value_range) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/embulk/output/google_sheets_ruby.rb', line 76

def update_sheet(value_range)
  @service.update_spreadsheet_value(
    @spreadsheet_id,
    value_range.range,
    value_range,
    value_input_option: 'USER_ENTERED'
  )
end