class AdopsReportScrapper::AdxClient

require option with network_id to be passed into constructor

Constants

API_NAME
API_SCOPE
API_VERSION
CREDENTIAL_STORE_FILE

Public Instance Methods

date_supported?(date = nil) click to toggle source
# File lib/adops_report_scrapper/adx_client.rb, line 17
def date_supported?(date = nil)
  _date = date || @date
  return true if _date < Date.today
  false
end

Private Instance Methods

init_client() click to toggle source
# File lib/adops_report_scrapper/adx_client.rb, line 25
def init_client
  fail 'please specify adx account id' unless @options['account_id']
  @account_id = @options['account_id']
  authorization = nil

  file_storage = Google::APIClient::FileStorage.new(CREDENTIAL_STORE_FILE)
  if file_storage.authorization.nil?
    flow = Google::APIClient::InstalledAppFlow.new(
      :client_id => @login,
      :client_secret => @secret,
      :scope => [API_SCOPE]
    )
    authorization = flow.authorize(file_storage)
  else
    authorization = file_storage.authorization
  end

  @client = Google::APIClient::Service.new(API_NAME, API_VERSION,
    {
      :application_name => "Ruby #{API_NAME} ad report scrapper",
      :application_version => '1.0.0',
      :authorization => authorization
    }
  )
end
scrap() click to toggle source
# File lib/adops_report_scrapper/adx_client.rb, line 51
def scrap
  date_str = @date.strftime('%Y-%m-%d')
  option = {
      :accountId => @account_id,
      :startDate => date_str,
      :endDate => date_str,
      :metric => ['AD_REQUESTS', 'AD_IMPRESSIONS', 'CLICKS', 'EARNINGS'],
      :dimension => ['DATE', 'DFP_AD_UNITS', 'DFP_AD_UNIT_ID', 'COUNTRY_CODE', 'PLATFORM_TYPE_NAME', 'PRODUCT_NAME'],
      :alt => 'csv'
  }
  result = @client.accounts.reports.generate(option).execute
  @data = CSV.parse(result.body)
end