class AdopsReportScrapper::AdsenseClient
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/adsense_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/adsense_client.rb, line 25 def init_client fail 'please specify adsense 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/adsense_client.rb, line 51 def scrap date_str = @date.strftime('%Y-%m-%d') result = @client.accounts.reports.generate( :accountId => @account_id, :startDate => date_str, :endDate => date_str, :metric => ['AD_REQUESTS', 'INDIVIDUAL_AD_IMPRESSIONS', 'CLICKS', 'EARNINGS'], :dimension => ['DATE', 'AD_UNIT_CODE', 'AD_UNIT_NAME', 'COUNTRY_CODE', 'PLATFORM_TYPE_NAME'], :alt => 'csv').execute @data = CSV.parse(result.body) end