Adwords API get keywords with ruby on rails

Hello,

I took the ruby on rails example, and tried to add to it a view which is displaying the keywords from an AdGroup, I am able to get the keywords, and the total no of keywords from a campaign but I don’t know why I can not display them in the view.

keyword model:

class Keyword

attr_reader :id

attr_reader :criteriaType

attr_reader :keywordText

def initialize(api_campaign)

@id = api_campaign[:id]

@criteriaType = api_campaign[:criteriaType]

@keywordText = api_campaign[:keywordText]

end

def self.get_keywords_list(response)

result = {}

if response[:entries]

response[:entries].each do |api_keyword|

keyword = Keyword.new(api_keyword)

result[keyword.id] = keyword

end

end

return result

end

end

keyword_controller

class KeywordController < ApplicationController

PAGE_SIZE = 50

def index()

@selected_account = selected_account

if @selected_account

response = request_keywords_list(‘AdGroupID’)

if response

@keywords = Keyword.get_keywords_list(response)

@keyword_count = response[:total_num_entries]

end

end

end

private

def request_keywords_list(ad_group_id)

logger.debug ad_group_id

api = get_adwords_api()

service = api.service(:AdGroupCriterionService, get_api_version())

selector = {

:fields => [‘Id’, ‘CriteriaType’, ‘KeywordText’],

:ordering => [{:field => ‘Id’, :sort_order => ‘ASCENDING’}],

:predicates => [{:field => ‘AdGroupId’, :operator => ‘EQUALS’, :values => [ad_group_id]},

				{:field => 'CriteriaType', :operator => 'EQUALS', :values => ['KEYWORD']}],

:paging => {:start_index => 0, :number_results => PAGE_SIZE}

}

result = nil

begin

result = service.get(selector)

logger.debug service.get(selector)

rescue AdwordsApi::Errors::ApiException => e

logger.fatal(“Exception occurred: %s\n%s” % [e.to_s, e.message])

flash.now[:alert] =

‘API request failed with an error, see logs for details’

end

return result

end

end

keyword/index.html.erb

Keywords list

<% if @selected_account.nil? %>

No account selected!

<% else %>

<% if @keywords.nil? or @keywords.empty? %>

  • No keywords available.

<% else %>

  • Total number of keywords: <%= @keyword_count %>
ID KeywordText CriteriaType

<% @keywords.each do |id, keyword| %>

<%= [keyword.id](http://keyword.id/) %> <%= keyword.keywordText %> <%= keyword.criteriaType %>

<% end %>

<% end %>

If someone can help me with some pointers, I’ll be grateful.

So I discovered what I did wrong,

def self.get_keywords_list(response)

result = {}

if response[:entries]

response[:entries].each do |api_keyword|

keyword = Keyword.new(api_keyword)

result[keyword.id] = keyword

end

end

return result

end

In my response loop I need to have something like

[keyword[:criterion][:id],
keyword[:criterion][:type],
keyword[:criterion][:text]]

but I am such a noob, that I don’t know how to implement that in my code.

Thank you