Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow authenticated user to vote/remove vote on card, add idMembersVoted to card #208

Merged
merged 4 commits into from
Aug 11, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 32 additions & 0 deletions lib/trello/card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,38 @@ def remove_member(member)
client.delete("/cards/#{id}/members/#{member.id}")
end

# Current authenticated user upvotes a card
def upvote
begin
client.post("/cards/#{id}/membersVoted", {
value: get_authenticated_user_id
})
rescue Trello::Error => e
fail e unless e.message =~ /has already voted/i
end

self
end

# Recind upvote. Noop if authenticated user hasn't previously voted
def remove_upvote
begin
client.delete("/cards/#{id}/membersVoted/#{get_authenticated_user_id}")
rescue Trello::Error => e
fail e unless e.message =~ /has not voted/i
end

self
end

# FIXME: this doesn't belong here
# Is there a possibility of an application (not a user) that can be
# authenticated?
private def get_authenticated_user_id
mem = Member.from_response client.get('/members/me')
mem && mem.id
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, this is the wrong place to put this. Would adding a Member.me class method be a good idea? The concept of "me" exists in the API and is documented here: https://developers.trello.com/advanced-reference/member#get-1-members-idmember-or-username


# Add a label
def add_label(label)
unless label.valid?
Expand Down
49 changes: 48 additions & 1 deletion spec/card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ module Trello

expect(card).to be_a Card
end

it 'creates a duplicate card with source due date and checklists and saves it on Trello', refactor: true do
payload = {
source_card_id: cards_details.first['id'],
Expand Down Expand Up @@ -402,6 +402,53 @@ module Trello
end
end

context "votes" do
let(:authenticated_member) { double(id: '4ee7df3ce582acdec80000b2') }

before do
allow(card)
.to receive(:get_authenticated_user_id)
.and_return(authenticated_member.id)
end

it 'upvotes a card with the currently authenticated member' do
expect(client)
.to receive(:post)
.with("/cards/abcdef123456789123456789/membersVoted", {
value: authenticated_member.id
})

card.upvote
end

it 'returns the card even if the user has already upvoted' do
expect(client)
.to receive(:post)
.with("/cards/abcdef123456789123456789/membersVoted", {
value: authenticated_member.id
})
.and_raise(Trello::Error, 'member has already voted')
expect(card.upvote).to be_kind_of Trello::Card
end

it 'removes an upvote from a card' do
expect(client)
.to receive(:delete)
.with("/cards/abcdef123456789123456789/membersVoted/#{authenticated_member.id}")

card.remove_upvote
end

it 'returns card after remove_upvote even if the user has not previously upvoted it' do
expect(client)
.to receive(:delete)
.with("/cards/abcdef123456789123456789/membersVoted/#{authenticated_member.id}")
.and_raise(Trello::Error, 'member has not voted on the card')

card.remove_upvote
end
end

context "comments" do
it "posts a comment" do
expect(client)
Expand Down