RockNinja
RockNinja

Reputation: 2309

How to send params to a method?

I am trying to make a query to Webmaster Tool api using the Ruby Client.

params = {
  start_date: "2015-01-14",
  end_date: "2015-01-14"
}

AuthWebmastersService.query_search_analytics("http://www.ex.com/", params)

When I'm trying to make that request I get ArgumentError (unknown keywords: start_date, end_date), why is this happening?

Here is the method definition.

Upvotes: 3

Views: 282

Answers (4)

Steve Bazyl
Steve Bazyl

Reputation: 11672

This is a ruby issue with auto-expanding the last argument if is a hash.

Two workarounds:

1 - Use a proper object instead of a hash:

params = Google::Apis::WebmastersV3::SearchAnalyticsQueryRequest.new(
  start_date: "2015-01-14",
  end_date: "2015-01-14"
)

AuthWebmastersService.query_search_analytics("http://www.ex.com/", params)

2 - Append an empty hash as a param:

params = {
  start_date: "2015-01-14",
  end_date: "2015-01-14"
}

AuthWebmastersService.query_search_analytics("http://www.ex.com/", params, {})

Upvotes: 1

Stefan
Stefan

Reputation: 114178

It doesn't work as expected, because Ruby converts your hash to keyword arguments, i.e.

query_search_analytics("...", {start_date: "2015-01-14", end_date: "2015-01-14"})

becomes:

query_search_analytics("...", start_date: "2015-01-14", end_date: "2015-01-14")

To get the expected result, you have to append an empty hash:

query_search_analytics("http://www.ex.com/", params, {})

Upvotes: 3

Jakub Troszok
Jakub Troszok

Reputation: 103733

This error: ArgumentError (unknown keywords: start_date, end_date) is telling you that you specified the wrong keyword attributes.

Your method takes:

def query_search_analytics(site_url, search_analytics_query_request_object = nil, fields: nil, quota_user: nil, user_ip: nil, options: nil, &block)
  • site_url as first argument
  • search_analytics_query_request_object - as second argument with default value nil
  • multiple keyword arguments (and a block)

When you call your method like you wrote:

  • site_url will have value "http://www.ex.com/"
  • search_analytics_query_request_object will be nill
  • and hash will be applied to keyword arguments and ruby will raise the error as there are no startDate and endDate keyword argument

What you need to do is to:

query_search_analytics("http://www.ex.com/", options: params)

Upvotes: 0

sawa
sawa

Reputation: 168101

Because it only takes the keyword arguments fields, quota_user, user_ip, options, and start_date, end_date are not among them.

Upvotes: -2

Related Questions