class Embulk::Output::GoogleAnalytics::Client

Public Class Methods

new(task) click to toggle source
# File lib/embulk/output/google_analytics/client.rb, line 7
def initialize(task)
  @task = task
end

Public Instance Methods

service() click to toggle source
# File lib/embulk/output/google_analytics/client.rb, line 23
def service
  analytics_service = ::Google::Apis::AnalyticsV3::AnalyticsService.new
  analytics_service.authorization = Signet::OAuth2::Client.new({
    :token_credential_uri => @task["token_credential_uri"],
    :audience => @task["audience"],
    :client_id => @task["client_id"],
    :client_secret => @task["client_secret"],
    :refresh_token => @task["refresh_token"],
    :scope => @task["scope"],
  })
  analytics_service.authorization.fetch_access_token!
  analytics_service
end
upload(rows) click to toggle source
# File lib/embulk/output/google_analytics/client.rb, line 11
def upload(rows)
  self.service.upload_data(@task['account_id'], @task['webproperty_id'], @task['datasource_id'], {
    upload_source: make_upload_content(rows),
    content_type: 'application/octet-stream',
    options: {
      header: {
        'x-goog-upload-file-name' => @task['filename'],
      }
    }
  })
end

Private Instance Methods

make_upload_content(rows) click to toggle source
# File lib/embulk/output/google_analytics/client.rb, line 38
def make_upload_content(rows)
  io = StringIO.new
  io.puts(rows.first.collect{|k,v| k }.join(","))
  rows.each do |row|
    io.puts(row.collect{|k, v| v}.join(","))
  end
  io
end