Make a Strawpoll

12

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!

Maltysen

Posted 2016-04-04T07:07:05.117

Reputation: 25 023

2(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} – Leaky Nun – 2016-04-04T07:26:15.217

1"NOTE: You must specify a "Content-Type: application/json" header in your request." – Leaky Nun – 2016-04-04T07:27:09.847

The API sort of doesn't work, or at least it doesn't work like the docs say it does – cat – 2016-04-09T21:32:40.813

https://strawpoll.me/api/v2/polls always responds with http 400 – cat – 2016-04-09T21:33:12.580

Are you required to return the strawpoll in http://? Can I return in https:// instead? – Value Ink – 2016-04-10T01:56:48.413

@KevinLau sure. – Maltysen – 2016-04-10T02:07:40.347

I can't get the API to respond properly with Factor or Python. @KevinLau have you tested yours? – cat – 2016-04-10T11:14:34.310

I have, and it returns a correct response that I can use to get the strawpoll link. – Value Ink – 2016-04-10T21:55:13.203

Answers

3

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 ;

cat

Posted 2016-04-04T07:07:05.117

Reputation: 4 989

1

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"]}

Value Ink

Posted 2016-04-04T07:07:05.117

Reputation: 10 608

Did you set the Content-Type: application/json header? – cat – 2016-04-10T11:15:58.497

I got a proper response back, so I'd assume that HTTParty does that for me already. – Value Ink – 2016-04-10T21:53:49.787

This gem won't install, which is really annoying since I'd like to figure out why I keep getting HTTP 400. – cat – 2016-04-10T23:14:16.437

What's your Ruby version? I updated mine to the version I have, and the gem says it needs 1.9.3+ – Value Ink – 2016-04-10T23:56:30.530