Skip to content

Instantly share code, notes, and snippets.

@oseifrimpong
Created August 4, 2015 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oseifrimpong/f6a65995ea1306855e9d to your computer and use it in GitHub Desktop.
Save oseifrimpong/f6a65995ea1306855e9d to your computer and use it in GitHub Desktop.
require 'adwords_api'
class ApplicationController < ActionController::Base
before_filter :authenticate
protect_from_forgery
private
# Returns the API version in use.
def get_api_version()
return :v201506
end
#Returns currently selected account.
def selected_account()
@selected_account ||= session[:selected_account]
return @selected_account
end
# Sets current account to the specified one.
def selected_account=(new_selected_account)
@selected_account = new_selected_account
session[:selected_account] = @selected_account
end
# Checks if we have a valid credentials.
def authenticate()
token = session[:token]
redirect_to login_prompt_path if token.nil?
return !token.nil?
end
# Returns an API object.
def get_adwords_api()
@api ||= create_adwords_api()
return @api
end
# Creates an instance of AdWords API class. Uses a configuration file and
# Rails config directory.
def create_adwords_api()
config_filename = File.join(Rails.root, 'config', 'adwords_api.yml')
@api = AdwordsApi::Api.new(config_filename)
token = session[:token]
# If we have an OAuth2 token in session we use the credentials from it.
if token
credentials = @api.credential_handler()
credentials.set_credential(:oauth2_token, token)
credentials.set_credential(:client_customer_id, selected_account)
end
return @api
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment