13
\$\begingroup\$

Moderator note: https://strawpoll.me has officially shut down, and the linked API no longer appears to work or provide data. Therefore, new answers cannot actually do the task and so the challenge has been closed to prevent any new answers.

Strawpolls are great, and we use them in chat all the time for getting opinions on things. But they're kinda annoying to make since you need a webbrowser to make them, and everyone knows that I access PPCG only through SE APIs and curl. So I want you to write some code that uses Strawpoll's API to make a strawpoll with the given options.

Specs

Take as input a list in any reasonable format of options as strings, and a title which is also a string. Then use the API to make a strawpoll with that title and those options and output the url.

Multiples should not be allowed, duplicate checking should be enforced, and captchas should not be necessary.

Example

"My Awesome Poll"
["option 1", "I like pies", "I also like cakes"]

Output: http://strawpoll.me/7275997

This is , so shortest code in bytes wins!

\$\endgroup\$
9
  • 4
    \$\begingroup\$ (Just to help everyone) TL;DR: do this: https://strawpoll.me/api/v2/polls{"title":"This is a test poll.","options":["Option #1","Option #2"],"multi": true} \$\endgroup\$
    – Leaky Nun
    Commented Apr 4, 2016 at 7:26
  • 1
    \$\begingroup\$ "NOTE: You must specify a "Content-Type: application/json" header in your request." \$\endgroup\$
    – Leaky Nun
    Commented Apr 4, 2016 at 7:27
  • \$\begingroup\$ The API sort of doesn't work, or at least it doesn't work like the docs say it does \$\endgroup\$
    – cat
    Commented Apr 9, 2016 at 21:32
  • \$\begingroup\$ https://strawpoll.me/api/v2/polls always responds with http 400 \$\endgroup\$
    – cat
    Commented Apr 9, 2016 at 21:33
  • 1
    \$\begingroup\$ I’ve closed this question because strawpoll.me has officially shut down, along with it's API, and so future answers to this are impossible \$\endgroup\$ Commented Jan 3, 2023 at 22:03

3 Answers 3

3
\$\begingroup\$

Factor, 257 262 bytes

TUPLE: p title options dupcheck ;
[ command-line rest [ first ] [ rest ] bi "normal" p boa >json "https://strawpoll.me/api/v2/polls" <post-request> "application/json" "Content-Type" set-header http-request nip json> id>> 10 base> "http://strawpoll.me/" prepend ]

Expects a name as the first command-line arg, then options after that.

Requires com-auto-use.

Ungolfed:

TUPLE: poll
  title options dupcheck ;

: post-json ( post-data url -- response data )
  <post-request>
    "application/json" "Content-Type" set-header
  http-request ;

: strawpoll-main ( -- )
  command-line rest [ first ] [ rest ] bi "normal" poll boa
  >json "https://strawpoll.me/api/v2/polls" post-json nip
  json> id>> 10 base> "http://strawpoll.me/" prepend print ;
\$\endgroup\$
1
\$\begingroup\$

Ruby 2.2.4 + HTTParty: 129 bytes

Anonymous function; returns a string with the strawpoll URL. Remember to install the HTTParty gem before running.

->q,a{require'httparty';s="http%s://strawpoll.me/%s"
s%[p,HTTParty.post(s%[?s,"api/v2/polls"],body:{title:q,options:a},verify:p)["id"]]}

If the URL can be returned with https:// instead of http://, 126 bytes:

->q,a{require'httparty';s="https://strawpoll.me/%s"
s%HTTParty.post(s%"api/v2/polls",body:{title:q,options:a},verify:p)["id"]}
\$\endgroup\$
4
  • \$\begingroup\$ Did you set the Content-Type: application/json header? \$\endgroup\$
    – cat
    Commented Apr 10, 2016 at 11:15
  • \$\begingroup\$ I got a proper response back, so I'd assume that HTTParty does that for me already. \$\endgroup\$
    – Value Ink
    Commented Apr 10, 2016 at 21:53
  • \$\begingroup\$ This gem won't install, which is really annoying since I'd like to figure out why I keep getting HTTP 400. \$\endgroup\$
    – cat
    Commented Apr 10, 2016 at 23:14
  • \$\begingroup\$ What's your Ruby version? I updated mine to the version I have, and the gem says it needs 1.9.3+ \$\endgroup\$
    – Value Ink
    Commented Apr 10, 2016 at 23:56
1
+400
\$\begingroup\$

Python, 145 bytes

lambda t,o:post('https://api.strawpoll.com/v3/polls',json={"title":t,"poll_options":[{"value":x}for x in o]}).json()["url"]
from requests import*

A function which takes in a string for the title and set of strings for the options and returns the URL. I am not including an ATO/TIO link because you aren't able to do requests from those platforms.

In the intervening years since this question was asked, the original strawpoll API has become defunct, so I'm using the newer API, which is unfortunately more verbose.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.