Skip to content

Instantly share code, notes, and snippets.

@zhangyuan
Created October 3, 2012 18:06
Show Gist options
  • Save zhangyuan/3828675 to your computer and use it in GitHub Desktop.
Save zhangyuan/3828675 to your computer and use it in GitHub Desktop.
a simple renren api client for posting status text
# encoding: utf-8
require 'digest/md5'
require "faraday"
module Renren
class Client
def self.sign(params)
str = params.map {|k, v| "#{k}=#{v}"}.sort.join
str << secret
Digest::MD5.hexdigest(str)
end
def self.secret
"" # TODO add renren api secret here
end
def self.server_url
"http://api.renren.com/restserver.do"
end
def run_request(method, params = {})
connection.send method, self.class.server_url, params.merge(:sig => self.class.sign(params))
end
def get(params = {})
run_request(:get, params)
end
def post(params = {})
run_request(:post, params)
end
def connection
@connect ||= begin
Faraday.new(:url => self.class.server_url) do |faraday|
faraday.request :url_encoded
faraday.response :logger
faraday.adapter Faraday.default_adapter
end
end
end
end
end
client = Renren::Client.new
access_token = "" # TODO access_token
status = "" # TODO status text
response = client.post :method => "status.set", :v => 1.0, :status => status, :format => "JSON",
:access_token => access_token
puts response.body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment