cescue / monkey-business

Simple ruby client for the surveymonkey api
MIT License
3 stars 4 forks source link

Support for create_recipients endpoint? #1

Open coryschires opened 7 years ago

coryschires commented 7 years ago

Hi! Thanks for making this gem. As far as I can tell, it's the only gem that supports the latest version of the Survey Monkey API. So super helpful!

I'm trying to do something which I suspect is fairly common; nevertheless, I'm having a little trouble. Using their web UI, I've created a survey and added an email collector. I'm able to access both those resources using monkey-business. So far so good.

Next, I'd like my app to send this survey via email to one of our users after they've taken a specific action. In other words, if I've got the terminology correct, I'd like to create a new recipient for my survey's email collector.

Finally my question: It looks like the SurveyMonkey API provides an endpoint for exactly this use case: https://developer.surveymonkey.com/docs/methods/create_recipients/. Unfortunately, I can't figure out how to access this endpoint using monkey-business.

I'm able to list the recipients via:

client.collectors(id: COLLECTOR_ID).recipients.request

But unfortunately, I can't figure out how to add new recipients. I'd really appreciate any advice.

Thanks again!

cescue commented 7 years ago

Glad to hear the gem is working out for you!

Finally my question: It looks like the SurveyMonkey API provides an endpoint for exactly this use case: https://developer.surveymonkey.com/docs/methods/create_recipients/. Unfortunately, I can't figure out how to access this endpoint using monkey-business.

It looks like the create_recipients method is part of the (now defunct) version 2 API. At face value, it looks like you'd want to use the contact / contact list methods for this.

Something like this should work (untested):

# Create a list
client.contact_lists(name: 'Frog Enthusiasts').request(method: :post)

# Create contact and add to contact list
client.contact_lists(id: 1).contacts(first_name: 'Edward', last_name: 'Nigma', email: 'enigma@villains.org').request(method: :post)

If you contact lists are overkill for your use case, you can probably use the recipients method:

client.collectors(id: 1).recipients(email: 'enigma@villains.org').request(method: :post)

Let me know if this clears things up.

coryschires commented 7 years ago

@cescue

Thanks for the quick response!

I tried what you recommended and unfortunately it raised some API errors. First, I tried exactly what you suggested:

client.collectors(id: COLLECTOR_ID).recipients(email: 'enigma@villains.org').request(method: :post)
# => ArgumentError: wrong number of arguments (given 1, expected 0)
     from /bundle/gems/monkey-business-1.0.4/lib/api/api_resource.rb:25:in `request'

Okay, fine. Maybe I don't need to pass method: :post. So next I tried:

client.collectors(id: COLLECTOR_ID).recipients(email: 'enigma@villains.org').request
# => {
    "error" => {
                    "docs" => "https://developer.surveymonkey.com/api/v3/#error-codes",
                 "message" => "Invalid URL parameters.",
                      "id" => "1003",
                    "name" => "Bad Request",
        "http_status_code" => 400
    }
}

Thanks again for your help!

cescue commented 7 years ago

Hmm. It's been awhile since I've used the gem myself, so I may have given bad examples. Alternatively, could be a bug in my code. You'll definitely need to specify the request type, since the behavior of the API method is determined by the HTTP method used.

I'll look into this.

cescue commented 7 years ago

In the meantime, see what happens if you add the HTTP method as an option to the API resource - something like this:

client.collectors(id: COLLECTOR_ID).recipients(email: 'enigma@villains.org', method: :post).request
coryschires commented 7 years ago

Hmm. Unfortunately, still having trouble. I tried:

response = client.collectors(id: COLLECTOR_ID).recipients(email: 'enigma@villains.org', method: :post).request
# => {
    "server" => ["nginx"],
    "date" => ["Fri, 14 Jul 2017 20:35:54 GMT"],
    "content-type" => ["application/json; charset=UTF-8"],
    "transfer-encoding" => ["chunked"],
    "connection" => ["close"],
    "vary" => ["Accept-Encoding", "Accept-Encoding"],
    "set-cookie" => [
        "ep201=8rGbh0k1RRnomI5R7ie0eX4fWpY=; Domain=.surveymonkey.net; Path=/; Expires=Fri, 14-Jul-17 21:05:54 GMT",
        "ep202=+pt3pqKkP8hxYHpj4U6s9bLqhNA=; Domain=.surveymonkey.net; Path=/; Expires=Sun, 15-Jul-18 02:24:40 GMT"
    ],
    "rtss" => ["1-1163-3"],
    "sm-request-id" => ["58cf6721-2a01-449d-90d5-69dc3cb5965b"],
    "cache-control" => ["no-store, no-cache, max-age=0, private, must-revalidate"],
    "access-control-allow-headers" => ["Authorization, Content-Type"],
    "access-control-allow-methods" => ["POST, GET, PATCH, PUT, DELETE, OPTIONS, HEAD"],
    "access-control-allow-origin" => ["*"],
    "x-xss-protection" => ["1; mode=block"],
    "x-content-type-options" => ["nosniff"]
}

I'm guessing it's not using the correct URL based on this:

response.class # Net::HTTPNotFound < Net::HTTPClientError

If you any other ideas, please let me know. Thanks again for your help!!!