Skip to content

Instantly share code, notes, and snippets.

@xdite
Created December 15, 2009 13:46
Show Gist options
  • Save xdite/256944 to your computer and use it in GitHub Desktop.
Save xdite/256944 to your computer and use it in GitHub Desktop.
Plurk Ruby API example
require 'open-uri'
require 'cgi'
require 'rubygems'
require 'json'
API_KEY = "dKkIdUCoHo7vUDPjd3zE0bRvdm5a9sQi" # NEED REPLACE
API_HOST = 'http://www.plurk.com'
#--- Requests ----------------------------------------------
def login(username,password)
path = "/API/Users/login"
params = { "username" => username, "password" => password , "api_key" => API_KEY }
req = open("#{API_HOST}#{path}?#{hash_to_querystring(params)}")
@cookie = req.meta['set-cookie'].split('; ',2)[0]
return JSON.parse(req.read)
end
def add_plurk(content="", qualifier="says",lang="en")
path = "/API/Timeline/plurkAdd"
params = {
"content" => content,
"qualifier" => qualifier,
"lang" => lang,
"api_key" => API_KEY
}
req = open("#{API_HOST}#{path}?#{hash_to_querystring(params)}", "Cookie" => @cookie)
return JSON.parse(req.read)
end
def hash_to_querystring(hash)
qstr = ""
hash.each do |key,val|
qstr += "#{key}=#{CGI.escape(val.to_s)}&" unless val.nil?
end
qstr
end
# == Author & License
# Author: Yi-Ting Cheng a.k.a. xdite (mailto:xdite@veryxd.com)
# Licensed under the MIT: http://www.opensource.org/licenses/mit-license.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment