hakamadare / rubygem-surveymonkey

Ruby API client for SurveyMonkey
https://rubygems.org/gems/surveymonkey
MIT License
4 stars 3 forks source link

Required field 'survey_id' is missing from get_response method call #4

Closed ghost closed 8 years ago

ghost commented 8 years ago

I'm able to retrieve a list of surveys with the following call:

surveys = Surveymonkey.get_survey_list('fields' => ['title', 'question_count', 'num_responses', 'date_created', 'date_modified'])

Similarly, I'm able to retrieve a list of respondents to a given survey with this call:

# survey_id is populated from the list gathered above    
respondents = Surveymonkey.get_respondent_list('survey_id' => survey_id.to_s) 

However, when I try to retrieve the responses with this call:

# respondent_ids is populated from the previous API call
response = Surveymonkey.get_responses('survey_id' => survey_id.to_s, 'respondent_ids' => respondent_ids)

I get the following error:

[2016-04-27T16:09:31] ERROR surveymonkey: execute[get_responses]: unable to execute API request: get_responses "{}"
[2016-04-27T16:09:31] ERROR surveymonkey: method_missing: Error 3 (Invalid Request): Required field 'survey_id' is missing
/usr/local/lib/ruby/gems/2.3.0/gems/surveymonkey-0.6.0/lib/surveymonkey/request.rb:61:in `execute': Error 3 (Invalid Request): Required field 'survey_id' is missing (Surveymonkey::Error)
    from /usr/local/lib/ruby/gems/2.3.0/gems/surveymonkey-0.6.0/lib/surveymonkey.rb:70:in `method_missing'

Am I missing something obvious, or is the get_responses method not working as intended?

ghost commented 8 years ago

Here's some relevant logger output - at face value, it looks like the arguments aren't getting passed from get_responses to post.

[2016-04-28T09:56:22] DEBUG surveymonkey: initialize: api_method: get_responses
[2016-04-28T09:56:22] DEBUG surveymonkey: initialize: args: [{"survey_id"=>67965837, "respondent_ids"=>["4673037086", "4673032990", "4673028893"]}]

...

[2016-04-28T09:56:22] DEBUG surveymonkey: api_method: path '/v2/surveys/get_responses'
[2016-04-28T09:56:22] DEBUG surveymonkey: api_method: method 'post'
[2016-04-28T09:56:22] DEBUG surveymonkey: initialize: enter
[2016-04-28T09:56:22] DEBUG surveymonkey: initialize: the_method: 'post' (was 'post')
[2016-04-28T09:56:22] DEBUG surveymonkey: initialize: method: post
[2016-04-28T09:56:22] DEBUG surveymonkey: api_method_params: parsing api method params from '{}'
[2016-04-28T09:56:22] DEBUG surveymonkey: api_method_params: parsed method params '{}'
ghost commented 8 years ago

I think I've narrowed the problem down a bit. When calling request.new, @method_params is not being assigned:

# This is is an empty hash when calling get_responses with call shown above
@method_params = api.api_method_params(param_hash.fetch('method_params', {}))

I'm able to circumvent this by adding method_params as a key to the original options hash:

# Hey, this works!
Surveymonkey.get_responses('method_params' => {'survey_id'=>'67965837', 'respondent_ids'=>['4673037086']})

Is this the intended behavior? If not, I can dig a little deeper and try to fix it.

hakamadare commented 8 years ago

hi @cescue! i can tell you two things:

  1. looking at other code i've written that uses this gem, i find that i use the syntax you mentioned in the last message (passing method_params as a key to the options hash) everywhere, and so i can confirm that that results in the desired behavior
  2. that seems ugly and clunky, and i don't like it. what exactly was i thinking?

i like your fix in #5, and i'm going to merge it; i just want to take a moment to determine whether i'll need to indicate that i'm breaking backwards compatibility.

hakamadare commented 8 years ago

thanks again! 👍 require ~> 1 to avoid this nonsense in the future