- Created 3 poll menthods for endpoints (#177)

  • Created 3 poll menthods for endpoints (#177)

  • poll vote

  • toggle poll status

  • poll voters

  • RuboCop Fixes

diff --git a/examples/polls.rb b/examples/polls.rb
new file mode 100644
index 0000000..84594f1
--- /dev/null
+++ b/examples/polls.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
+require File.expand_path('../../lib/discourse_api', __FILE__)
+
+client = DiscourseApi::Client.new("http://localhost:3000")
+client.api_key = "YOUR_API_KEY"
+client.api_username = "YOUR_USERNAME"
+
+options = ['8b4736b1ae3dfb5a28088530f036f9e5']
+poll_option_votes = client.poll_vote post_id: 5, poll_name: 'poll', options: options
+puts poll_option_votes.body['vote']
+
+poll_option_votes = client.toggle_poll_status(post_id: 5, poll_name: 'poll', status: 'closed')
+puts poll_option_votes[:body]['poll']['status']
+
+poll_option_votes = client.poll_voters(post_id: 5, poll_name: 'poll')
+puts poll_option_votes['voters']
diff --git a/lib/discourse_api/api/polls.rb b/lib/discourse_api/api/polls.rb
new file mode 100644
index 0000000..7fd223e
--- /dev/null
+++ b/lib/discourse_api/api/polls.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+module DiscourseApi
+  module API
+    module Polls
+      def poll_vote(args)
+        args = API.params(args)
+          .required(:post_id, :poll_name, :options)
+          .optional(:created_at)
+        put("/polls/vote", args)
+      end
+
+      def toggle_poll_status(args)
+        args = API.params(args)
+          .required(:post_id, :poll_name, :status)
+          .optional(:api_username)
+          .optional(:raise_errors)
+            put("/polls/toggle_status", args)
+      end
+
+      def poll_voters(args)
+        args = API.params(args)
+          .required(:post_id, :poll_name)
+          .optional(:opts)
+        response = get("/polls/voters.json", args)
+        response[:body]
+      end
+
+    end
+  end
+end
diff --git a/lib/discourse_api/client.rb b/lib/discourse_api/client.rb
index 233a115..746fcdb 100644
--- a/lib/discourse_api/client.rb
+++ b/lib/discourse_api/client.rb
@@ -9,6 +9,7 @@ require 'discourse_api/api/search'
 require 'discourse_api/api/sso'
 require 'discourse_api/api/tags'
 require 'discourse_api/api/topics'
+require 'discourse_api/api/polls'
 require 'discourse_api/api/posts'
 require 'discourse_api/api/users'
 require 'discourse_api/api/groups'
@@ -34,6 +35,7 @@ module DiscourseApi
     include DiscourseApi::API::SSO
     include DiscourseApi::API::Tags
     include DiscourseApi::API::Topics
+    include DiscourseApi::API::Polls
     include DiscourseApi::API::Posts
     include DiscourseApi::API::Users
     include DiscourseApi::API::Groups
@@ -118,6 +120,8 @@ module DiscourseApi
         conn.request :url_encoded
         # Parse responses as JSON
         conn.use FaradayMiddleware::ParseJson, content_type: 'application/json'
+        # For HTTP debugging, uncomment
+        # conn.response :logger
         # Use Faraday's default HTTP adapter
         conn.adapter Faraday.default_adapter
         #pass api_key and api_username on every request
diff --git a/spec/discourse_api/api/polls_spec.rb b/spec/discourse_api/api/polls_spec.rb
new file mode 100644
index 0000000..9021510
--- /dev/null
+++ b/spec/discourse_api/api/polls_spec.rb
@@ -0,0 +1,70 @@
+require 'spec_helper'
+
+describe DiscourseApi::API::Polls do
+  subject { DiscourseApi::Client.new("http://localhost:3000", "test_d7fd0429940", "test_user" )}
+
+  describe "#poll vote" do
+    before do
+      path = "http://localhost:3000/polls/vote"
+      stub_put(path)
+          .to_return(body: fixture("polls_vote.json"), headers: { content_type: "application/json" })
+    
+    end
+
+    it "requests the correct resource" do
+      options = ['8b4736b1ae3dfb5a28088530f036f9e5']
+      subject.poll_vote post_id: 5, poll_name: 'poll', options: options
+      expect(a_put("http://localhost:3000/polls/vote")).to have_been_made
+    end
+
+    it "returns the expected votes" do
+      options = ['8b4736b1ae3dfb5a28088530f036f9e5']
+      vote = subject.poll_vote post_id: 5, poll_name: 'poll', options: options
+      expect(vote.body).to be_a Hash
+      expect(vote.body['poll']['options']).to be_an Array
+      expect(vote.body['vote']).to eq(['8b4736b1ae3dfb5a28088530f036f9e5'])
+    end
+
+  describe "#poll toggle_status" do
+    before do
+      path = "http://localhost:3000/polls/toggle_status"
+      stub_put(path)
+          .to_return(body: fixture("polls_toggle_status.json"), headers: { content_type: "application/json" })
+
+    end
+
+    it "toggles the poll status to closed" do
+      subject.toggle_poll_status post_id: 5, poll_name: 'poll', status: 'closed'
+      expect(a_put("http://localhost:3000/polls/toggle_status")).to have_been_made
+    end
+
+    it "returns the expected results of closed poll" do
+      returned_poll_status = subject.toggle_poll_status post_id: 5, poll_name: 'poll', status: 'closed'
+      expect(returned_poll_status.body).to be_a Hash
+      returned_poll_status.body['poll']['options'].each { |g| expect(g).to be_a Hash }
+    end
+
+  end
+
+  describe "#poll voters" do
+    before do
+      stub_get("http://localhost:3000/polls/voters.json?post_id=5&poll_name=poll")
+          .to_return(body: fixture("polls_voters.json"), headers: { content_type: "application/json" })
+    end
+
+    it "requests the correct resource" do
+      subject.poll_voters post_id: 5, poll_name: 'poll'
+      expect(a_get("http://localhost:3000/polls/voters.json?post_id=5&poll_name=poll")).to have_been_made
+    end
+
+    it "returns the expected votes" do
+      voters = subject.poll_voters post_id: 5, poll_name: 'poll'
+      expect(voters).to be_a Hash
+      voters.each { |g| expect(g).to be_an Array }
+      expect(voters['voters']['e539a9df8700d0d05c69356a07b768cf']).to be_an Array
+      expect(voters['voters']['e539a9df8700d0d05c69356a07b768cf'][0]['id']).to eq(356)
+    end
+   end
+
+  end
+end
diff --git a/spec/fixtures/polls_toggle_status.json b/spec/fixtures/polls_toggle_status.json
new file mode 100644
index 0000000..289ca4b
--- /dev/null
+++ b/spec/fixtures/polls_toggle_status.json
@@ -0,0 +1,54 @@
+{
+  "poll": {
+    "name": "poll",
+    "type": "regular",
+    "status": "closed",
+    "public": true,
+    "results": "always",
+    "options": [
+      {
+        "id": "8b4736b1ae3dfb5a28088530f036f9e5",
+        "html": "I will attend the Apr 25, 2019 Launchpad meeting.",
+        "votes": 1
+      },
+      {
+        "id": "e09884fbf9b72d83e804050078847643",
+        "html": "I want to lead the meeting.",
+        "votes": 0
+      },
+      {
+        "id": "e539a9df8700d0d05c69356a07b768cf",
+        "html": "I’ll not attend this meeting.",
+        "votes": 2
+      }
+    ],
+    "voters": 3,
+    "preloaded_voters": {
+      "8b4736b1ae3dfb5a28088530f036f9e5": [
+        {
+          "id": 17,
+          "username": "Kim_Miller",
+          "name": "Kim Miller",
+          "avatar_template": "/user_avatar/discourse.gomomentum.org/user_one/{size}/3220_2.png",
+          "title": "New Owners Council"
+        }
+      ],
+      "e539a9df8700d0d05c69356a07b768cf": [
+        {
+          "id": 356,
+          "username": "David_Ashby",
+          "name": "David Ashby",
+          "avatar_template": "/user_avatar/discourse.gomomentum.org/user_two/{size}/2403_2.png",
+          "title": "Team Agape"
+        },
+        {
+          "id": 72,
+          "username": "David_Kirk",
+          "name": "David Kirk",
+          "avatar_template": "/user_avatar/discourse.gomomentum.org/user_three/{size}/236_2.png",
+          "title": "Foundry"
+        }
+      ]
+    }
+  }
+}
\ No newline at end of file
diff --git a/spec/fixtures/polls_vote.json b/spec/fixtures/polls_vote.json
new file mode 100644
index 0000000..1a33bf5
--- /dev/null
+++ b/spec/fixtures/polls_vote.json
@@ -0,0 +1,64 @@
+{
+  "poll": {
+    "name": "poll",
+    "type": "regular",
+    "status": "open",
+    "public": true,
+    "results": "always",
+    "options": [
+      {
+        "id": "8b4736b1ae3dfb5a28088530f036f9e5",

[... diff too long, it was truncated ...]

GitHub sha: 6abe72f5