Skip to content

Instantly share code, notes, and snippets.

@jlong
Created May 24, 2012 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlong/2781400 to your computer and use it in GitHub Desktop.
Save jlong/2781400 to your computer and use it in GitHub Desktop.
Creating and listing tickets with the UserVoice API
# helper method
def symbolize_hash(hash)
symbolized_hash = {}
# puts hash.inspect
hash.each do |key, value|
symbolized_hash[key.to_sym] = value
end
return symbolized_hash
end
consumer = OAuth::Consumer.new(KEY, SECRET)
puts "Get request token"
response = consumer.request(:get, "#{SITE}/api/v1/oauth/request_token.json")
request_token_hash = JSON.parse(response.body)
puts request_token_hash
request_token = OAuth::RequestToken.from_hash(consumer,
symbolize_hash(request_token_hash["token"]))
# this is just a library we use to create SSO tokens, similar code
# snippets can be found under the SSO docs here
encoder = Sso::Schemes::AesCbc128::Base64Encoder.new(SUBDOMAIN, API_KEY)
# create the SSO token setting the user to be an admin so
# we can list the tickets back later
sso_token = encoder.process({
:guid => "test@test.com",
:expires => "2012-12-31",
:email => "test@test.com",
:avatar_url => "http://external.com/users/1.png",
:display_name => "Testie McTest",
:admin => "accept"
})
# get the access token for this new user
response = consumer.request(:post, "#{SITE}/api/v1/oauth/authorize.json", nil, {}, {
:sso => sso_token, :request_token => request_token_hash["token"]["oauth_token"]
})
user_hash = JSON.parse(response.body)
access_token = OAuth::AccessToken.from_hash(consumer, symbolize_hash(user_hash["token"]))
# create the params for the ticket
ticket = {
"ticket[message]" => "Everything is broken......",
"ticket[subject]" => "Help!!!"
}
# create a ticket
response = access_token.request(:post, "#{SITE}/api/v1/tickets.json", ticket)
body = JSON.parse(response.body)
# get our ticket
response = access_token.request(:get, "#{SITE}/api/v1/tickets.json?per_page=1")
body = JSON.parse(response.body)
puts body["tickets"][0].inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment