Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions examples/polls.rb
Original file line number Diff line number Diff line change
@@ -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']
31 changes: 31 additions & 0 deletions lib/discourse_api/api/polls.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions lib/discourse_api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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'
Expand All @@ -34,6 +35,7 @@ class Client
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
Expand Down Expand Up @@ -118,6 +120,8 @@ def connection
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
Expand Down
70 changes: 70 additions & 0 deletions spec/discourse_api/api/polls_spec.rb
Original file line number Diff line number Diff line change
@@ -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
54 changes: 54 additions & 0 deletions spec/fixtures/polls_toggle_status.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
}
64 changes: 64 additions & 0 deletions spec/fixtures/polls_vote.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"poll": {
"name": "poll",
"type": "regular",
"status": "open",
"public": true,
"results": "always",
"options": [
{
"id": "8b4736b1ae3dfb5a28088530f036f9e5",
"html": "I will attend the Apr 25, 2019 Launchpad meeting.",
"votes": 2
},
{
"id": "e09884fbf9b72d83e804050078847643",
"html": "I want to lead the meeting.",
"votes": 0
},
{
"id": "e539a9df8700d0d05c69356a07b768cf",
"html": "I’ll not attend this meeting.",
"votes": 2
}
],
"voters": 4,
"preloaded_voters": {
"8b4736b1ae3dfb5a28088530f036f9e5": [
{
"id": 17,
"username": "Kim_Miller",
"name": "Kim Miller",
"avatar_template": "/user_avatar/discourse.gomomentum.org/kim_miller/{size}/3220_2.png",
"title": "New Owners Council"
},
{
"id": 150,
"username": "KM_Admin",
"name": "Kim Miller",
"avatar_template": "/user_avatar/discourse.gomomentum.org/km_admin/{size}/1232_2.png",
"title": "Admin"
}
],
"e539a9df8700d0d05c69356a07b768cf": [
{
"id": 356,
"username": "David_Ashby",
"name": "David Ashby",
"avatar_template": "/user_avatar/discourse.gomomentum.org/david_ashby/{size}/2403_2.png",
"title": "Team Agape"
},
{
"id": 72,
"username": "David_Kirk",
"name": "David Kirk",
"avatar_template": "/user_avatar/discourse.gomomentum.org/david_kirk/{size}/236_2.png",
"title": "Foundry"
}
]
}
},
"vote": [
"8b4736b1ae3dfb5a28088530f036f9e5"
]
}
20 changes: 20 additions & 0 deletions spec/fixtures/polls_voters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"voters": {
"e539a9df8700d0d05c69356a07b768cf": [
{
"id": 356,
"username": "David_Ashby",
"name": "David Ashby",
"avatar_template": "/user_avatar/discourse.gomomentum.org/david_ashby/{size}/2403_2.png",
"title": "Team Agape"
},
{
"id": 72,
"username": "David_Kirk",
"name": "David Kirk",
"avatar_template": "/user_avatar/discourse.gomomentum.org/david_kirk/{size}/236_2.png",
"title": "Foundry"
}
]
}
}