Skip to content

Instantly share code, notes, and snippets.

@zhangyuan
Created February 21, 2013 09:05
Show Gist options
  • Save zhangyuan/5003372 to your computer and use it in GitHub Desktop.
Save zhangyuan/5003372 to your computer and use it in GitHub Desktop.
QQ API Demo for Rails ( http://open.qq.com )
# Please add faraday, em-http-request, oj(optional) to Gemfile
CONFIG = {
qq: {
'appid' => 'your appid',
'appkey' => 'your appkey',
'host' => '119.147.19.43' # debug host. change it to openapi.tencentyun.com before release the app
}
}.freeze
# locate the class in lib/qq/command/ext/config.rb
module Qq::Command::Ext
module Config
extend ActiveSupport::Concern
included do
cattr_accessor :config
cattr_accessor :appid, :appkey, :host
@@config = CONFIG[:qq]
%w(appid appkey host).each do |name|
self.send("#{name}=", config[name])
end
end
end
end
# locate the class in lib/qq/command/ext/parameters.rb
module Qq::Command::Ext
module Parameters
extend ActiveSupport::Concern
included do
attr_accessor :openid, :openkey, :pf, :path
attr_writer :params, :http_method
end
def default_params
{
openid: openid,
openkey: openkey,
appid: appid,
pf: pf,
format: format
}
end
def params_before_sign
parameters = default_params.merge(params)
parameters.delete_if {|name, value| value.nil?}
parameters
end
def params
@params ||= {}
end
def params_after_sign
encoded_uri = URI.encode(path)
params_before_sign.merge(:sig => self.class.sign(path, http_method, params_before_sign))
end
def format
@format ||= 'json'
end
def http_method
@http_method ||= 'GET'
end
module ClassMethods
def sign(path, http_method, options = {})
encoded_uri = CGI.escape(path)
params_str = options.keys.sort.map do |name|
"#{name}=#{options[name]}"
end.join('&')
encoded_str = CGI.escape(params_str)
text = [http_method.upcase, encoded_uri, encoded_str].join('&')
secret = "#{appkey}&"
require 'openssl' unless defined?(OpenSSL)
Base64.strict_encode64 OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new, secret, text)
end
end
end
end
# locate the class in lib/qq/command/client.rb
module Qq::Command
class Client
include Qq::Command::Ext::Config
include Qq::Command::Ext::Parameters
def initialize(attributes = {})
attributes.each_pair do |name, value|
send("#{name}=", value)
end
end
def attributes
@attributes ||= {}
end
def request_params
params_after_sign
end
def base_url
"http://#{host}#{path}"
end
def request_url
if http_method.upcase == 'GET'
base_url + '?' + request_params.to_query
else
base_url
end
end
def fetch
conn = Faraday.new do |faraday|
faraday.adapter :em_http # make requests with Net::HTTP
faraday.response :logger if Rails.env.development?
end
response = if http_method.upcase == 'GET'
conn.get(request_url)
else
conn.post request_url, request_params
end
ActiveSupport::JSON.decode response.body
end
end
end
# Usage:
# http://wiki.open.qq.com/wiki/v3/user/get_info
# user_info = Qq::Command::Client.new(path: '/v3/user/get_info', params: {openid: params[:openid], openkey: params[:openkey], pf: params[:pf]}).fetch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment