Created
August 4, 2015 20:40
-
-
Save oseifrimpong/f6a65995ea1306855e9d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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