-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliquid_voting_api.rb
139 lines (121 loc) · 4.45 KB
/
liquid_voting_api.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
require 'rubygems'
require 'bundler/setup'
require 'graphql/client'
require 'graphql/client/http'
module LiquidVotingApi
module Client
URL = ENV.fetch('LIQUID_VOTING_API_URL', 'https://api.liquidvoting.io')
AUTH_KEY = ENV.fetch('LIQUID_VOTING_API_AUTH_KEY', '62309201-d2f0-407f-875b-9f836f94f2ca')
HTTP = GraphQL::Client::HTTP.new(URL) do
def headers(context)
{ "Authorization": "Bearer #{AUTH_KEY}" }
end
end
SCHEMA = GraphQL::Client.load_schema(HTTP)
CLIENT = GraphQL::Client.new(schema: SCHEMA, execute: HTTP)
CreateVoteMutation = CLIENT.parse <<-GRAPHQL
mutation($voter_email: String, $proposal_url: String!, $yes: Boolean!) {
createVote(participantEmail: $voter_email, proposalUrl: $proposal_url, yes: $yes) {
yes
weight
participant {
email
}
votingResult {
in_favor
against
}
}
}
GRAPHQL
## Example:
##
## create_vote(yes: true, proposal_url: "https://my.decidim.com/proposal", voter_email: "[email protected]")
## => vote
## vote.yes => true
## vote.voting_result.in_favor => 1
## vote.voting_result.against => 0
##
## On failure it will raise an exception with the errors returned by the API
def self.create_vote(yes:, proposal_url:, voter_email:)
variables = { yes: yes, proposal_url: proposal_url, voter_email: voter_email}
response = CLIENT.query(CreateVoteMutation, variables: variables)
if response.errors.any?
raise response.errors[:data].join(", ")
else
response.data.create_vote
end
end
DeleteVoteMutation = CLIENT.parse <<-GRAPHQL
mutation($voter_email: String, $proposal_url: String!) {
deleteVote(participantEmail: $voter_email, proposalUrl: $proposal_url) {
participant {
email
}
votingResult {
in_favor
against
}
}
}
GRAPHQL
def self.delete_vote(proposal_url:, voter_email:)
variables = { proposal_url: proposal_url, voter_email: voter_email}
response = CLIENT.query(DeleteVoteMutation, variables: variables)
if response.errors.any?
raise response.errors[:data].join(", ")
else
response.data.delete_vote
end
end
CreateDelegationMutation = CLIENT.parse <<-GRAPHQL
mutation($proposal_url: String!, $delegator_email: String!, $delegate_email: String!) {
createDelegation(proposalUrl: $proposal_url, delegatorEmail: $delegator_email, delegateEmail: $delegate_email) {
id
}
}
GRAPHQL
## Example:
##
## create_delegation(proposal_url: "https://my.decidim.com/proposal", delegator_email: "[email protected]", delegate_email: "[email protected]")
## => true
##
## On failure it will raise an exception with the errors returned by the API
def self.create_delegation(proposal_url:, delegator_email:, delegate_email:)
variables = { proposal_url: proposal_url, delegator_email: delegator_email, delegate_email: delegate_email }
response = CLIENT.query(CreateDelegationMutation, variables: variables)
if response.errors.any?
raise response.errors[:data].join(", ")
else
true
end
end
DeleteDelegationMutation = CLIENT.parse <<-GRAPHQL
mutation($proposal_url: String!, $delegator_email: String!, $delegate_email: String!) {
deleteDelegation(proposalUrl: $proposal_url, delegatorEmail: $delegator_email, delegateEmail: $delegate_email) {
votingResult {
in_favor
against
}
}
}
GRAPHQL
## Example:
##
## delete_delegation(proposal_url: "https://my.decidim.com/proposal", delegator_email: "[email protected]", delegate_email: "[email protected]")
## => deleted_delegation
## deleted_delegation.voting_result.in_favor => 0
## deleted_delegation.voting_result.against => 0
##
## On failure it will raise an exception with the errors returned by the API
def self.delete_delegation(proposal_url:, delegator_email:, delegate_email:)
variables = { proposal_url: proposal_url, delegator_email: delegator_email, delegate_email: delegate_email }
response = CLIENT.query(DeleteDelegationMutation, variables: variables)
if response.errors.any?
raise response.errors[:data].join(", ")
else
response.data.delete_delegation
end
end
end
end