3

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.

RockNinja
  • 2,179
  • 4
  • 16
  • 16
  • This looks strange, I've posted [another question](http://stackoverflow.com/q/34794043/477037) that focuses on the underlying problem. – Stefan Jan 14 '16 at 16:01
  • 1
    `query_search_analytics("http://www.ex.com/", params, {})` should work if `params` actually is the `search_analytics_query_request_object` – Stefan Jan 14 '16 at 16:23
  • @Stefan, yes what you said is working :) – RockNinja Jan 14 '16 at 16:27

4 Answers4

3

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, {})
Stefan
  • 109,145
  • 14
  • 143
  • 218
1

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, {})
Steve Bazyl
  • 11,002
  • 3
  • 21
  • 24
0

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)
Jakub Troszok
  • 99,267
  • 11
  • 41
  • 53
-2

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

sawa
  • 165,429
  • 45
  • 277
  • 381
  • 1
    `query_search_analytics(site_url, search_analytics_query_request_object = nil, fields: nil, quota_user: nil, user_ip: nil, options: nil, &block)` – RockNinja Jan 14 '16 at 15:49