class Baichuan::Client

Constants

ACTION_ADD_USERS
ACTION_CREATE_GROUP
ACTION_CUSTMSG_PUSH
ACTION_DELETE_USERS
ACTION_DISMISS_GROUP
ACTION_EXPEL_GROUP
ACTION_GET_ALL_GROUPS
ACTION_GET_APP_CHAT_LOGS
ACTION_GET_CHATLOGS
ACTION_GET_DETAILS
ACTION_GET_GROUP
ACTION_GET_GROUP_LOGS
ACTION_GET_MEMBERS
ACTION_GET_RELATIONS
ACTION_GET_SUMMARY
ACTION_GET_USERS
ACTION_IMMSG_PUSH
ACTION_IMPORT_CHAT_LOGS
ACTION_IMPORT_GROUP_LOGS
ACTION_INVITE_GROUP
ACTION_JOIN_GROUP
ACTION_QUIT_GROUP
ACTION_SEND_GROUP_MSG
ACTION_SET_MANAGER
ACTION_SET_MEMBER_NICK
ACTION_UNSET_MANAGER
ACTION_UPDATE_GROUP
ACTION_UPDATE_USERS

Public Class Methods

new(app_key, secret, sandbox = false) click to toggle source
# File lib/baichuan.rb, line 41
def initialize(app_key, secret, sandbox = false)
  @version = 1.0
  @api_host = sandbox ? 'https://gw.api.tbsandbox.com/router/rest' : 'https://eco.taobao.com/router/rest'
  @app_key = app_key || ENV['app_key']
  @secret = secret || ENV['secret']
end

Public Instance Methods

add_users(userinfos) click to toggle source
# File lib/baichuan.rb, line 104
def add_users(userinfos)
  post( ACTION_ADD_USERS, { userinfos: userinfos.to_json } )
end
common_params(action) click to toggle source
# File lib/baichuan.rb, line 59
def common_params(action)
  {
    method: action,
    app_key: @app_key,
    timestamp: Time.now.strftime('%Y-%m-%d %H:%M:%S'),
    format: 'json',
    v: '2.0',
    sign_method: 'md5'
  }
end
create_group(master, group_name, notice, members, tribe_type = 1) click to toggle source
# File lib/baichuan.rb, line 128
def create_group(master, group_name, notice, members, tribe_type = 1)
  post( ACTION_CREATE_GROUP, { user: master,
                               tribe_name: group_name,
                               notice: notice,
                               members: members,
                               tribe_type: tribe_type
                             }
      )
end
cust_msg_push(cust_msg) click to toggle source
# File lib/baichuan.rb, line 96
def cust_msg_push(cust_msg)
  post( ACTION_CUSTMSG_PUSH, { custmsg: cust_msg.to_json } )
end
delete_users(user_ids) click to toggle source
# File lib/baichuan.rb, line 116
def delete_users(user_ids)
  post( ACTION_DELETE_USERS, { userids: user_ids.join(',') } )
end
dismiss_group(master, group_id) click to toggle source
# File lib/baichuan.rb, line 162
def dismiss_group(master, group_id)
  post( ACTION_DISMISS_GROUP, { tribe_id: group_id, user: master } )
end
expel_group(user, group_id, member) click to toggle source
# File lib/baichuan.rb, line 150
def expel_group(user, group_id, member)
  post( ACTION_EXPEL_GROUP, { tribe_id: group_id,
                              user: user,
                              member: member
                            }
      )
end
get_all_group(user, group_types = [0]) click to toggle source
# File lib/baichuan.rb, line 191
def get_all_group(user, group_types = [0])
  post( ACTION_GET_ALL_GROUP, { user: user, tribe_types: group_types } )
end
get_app_chat_logs(begin_time, end_time, count) click to toggle source
# File lib/baichuan.rb, line 224
def get_app_chat_logs(begin_time, end_time, count)
  post( ACTION_GET_APP_CHAT_LOGS, { beg: begin_time, end: end_time, count: count } )
end
get_details(im_id, start_time, end_time) click to toggle source
# File lib/baichuan.rb, line 216
def get_details(im_id, start_time, end_time)
  post( ACTION_GET_DETAILS, { uid: im_id, starttime: start_time, endtime: end_time } )
end
get_group(master, group_id) click to toggle source
# File lib/baichuan.rb, line 166
def get_group(master, group_id)
  post( ACTION_GET_GROUP, { tribe_id: group_id, user: master } )
end
get_group_logs(group_id, begin_time, end_time, count) click to toggle source
# File lib/baichuan.rb, line 204
def get_group_logs(group_id, begin_time, end_time, count)
  post( ACTION_GET_GROUP_LOGS, { tribe_id: group_id, begin: begin_time, end: end_time, count: count } )
end
get_group_members(member, group_id) click to toggle source
# File lib/baichuan.rb, line 187
def get_group_members(member, group_id)
  post( ACTION_GET_MEMBERS, { user: member, tribe_id: group_id } )
end
get_relations(user, beg_date, end_date) click to toggle source
# File lib/baichuan.rb, line 120
def get_relations(user, beg_date, end_date)
  post( ACTION_GET_RELATIONS, { user: user, beg_date: beg_date, end_date: end_date })
end
get_summary(im_id) click to toggle source
# File lib/baichuan.rb, line 220
def get_summary(im_id)
  post( ACTION_GET_SUMMARY, { uid: im_id } )
end
get_users(user_ids) click to toggle source
# File lib/baichuan.rb, line 108
def get_users(user_ids)
  post( ACTION_GET_USERS, { userids: user_ids.join(',') } )
end
header() click to toggle source
# File lib/baichuan.rb, line 52
def header
  {
    'User-Agent'   => 'BaiChuanSdk/BaiChuan-Ruby-Sdk #{RUBY_VERSION} (#{@version})',
    'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8'
  }
end
http_call(url, data) click to toggle source
# File lib/baichuan.rb, line 70
def http_call(url, data)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'
  http.ssl_version = :TLSv1
  # http.ciphers = ['RC4-SHA']
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  req = Net::HTTP::Post.new(uri.path, initheader = header)
  req.body = URI.encode_www_form(data)
  res = http.request(req)
  response_object(JSON.parse(res.body))
end
im_msg_push(im_msg) click to toggle source
# File lib/baichuan.rb, line 100
def im_msg_push(im_msg)
  post( ACTION_IMMSG_PUSH, { immsg: im_msg.to_json } )
end
import_chat_logs(messages) click to toggle source
# File lib/baichuan.rb, line 212
def import_chat_logs(messages)
  post( ACTION_IMPORT_CHAT_LOGS, { messages: messages } )
end
import_group_logs(group_id, messages) click to toggle source
# File lib/baichuan.rb, line 208
def import_group_logs(group_id, messages)
  post( ACTION_IMPORT_GROUP_LOGS, { tribe_id: group_id, messages: messages } )
end
invite_group(master, group_id, members) click to toggle source
# File lib/baichuan.rb, line 138
def invite_group(master, group_id, members)
  post( ACTION_INVITE_GROUP, { tribe_id: group_id,
                               user: master,
                               members: members
                             }
      )
end
join_group(member, group_id) click to toggle source
# File lib/baichuan.rb, line 146
def join_group(member, group_id)
  post( ACTION_JOIN_GROUP, { user: member, tribe_id: group_id } )
end
make_signature(params) click to toggle source
# File lib/baichuan.rb, line 48
def make_signature(params)
  Digest::MD5.hexdigest("#{@secret}#{params.sort.join}#{@secret}").upcase
end
post(action, params) click to toggle source
# File lib/baichuan.rb, line 90
def post(action, params)
  params = common_params(action).merge(params)
  params.merge!(sign: make_signature(params))
  http_call(@api_host, params)
end
quit_group(member, group_id) click to toggle source
# File lib/baichuan.rb, line 158
def quit_group(member, group_id)
  post( ACTION_QUIT_GROUP, { tribe_id: group_id, user: member } )
end
response_object(body) click to toggle source
# File lib/baichuan.rb, line 83
def response_object(body)
  response = Struct.new(:success, :data).new
  response.success = !body.has_key?('error_response')
  response.data = body
  response
end
send_group_msg(member, group_id, msg) click to toggle source
# File lib/baichuan.rb, line 124
def send_group_msg(member, group_id, msg)
  post( ACTION_SEND_GROUP_MSG, { user: member, tribe_id: group_id, msg: msg })
end
set_manager(master, group_id, member) click to toggle source
# File lib/baichuan.rb, line 179
def set_manager(master, group_id, member)
  post( ACTION_SET_MANAGER, { user: master, tid: group_id, member: member } )
end
set_member_nick(user, group_id, member, nick) click to toggle source
# File lib/baichuan.rb, line 195
def set_member_nick(user, group_id, member, nick)
  post( ACTION_SET_MEMBER_NICK, { user: user,
                                  tribe_id: group_id,
                                  member: member,
                                  nick: nick
                                }
      )
end
unset_manager(master, group_id, member) click to toggle source
# File lib/baichuan.rb, line 183
def unset_manager(master, group_id, member)
  post( ACTION_UNSET_MANAGER, { user: master, tid: group_id, member: member } )
end
update_group(master, group_id, group_name, notice) click to toggle source
# File lib/baichuan.rb, line 170
def update_group(master, group_id, group_name, notice)
  post( ACTION_UPDATE_GROUP, { tribe_id: group_id,
                               tribe_name: group_name,
                               notice: notice,
                               user: master
                             }
      )
end
update_users(userinfos) click to toggle source
# File lib/baichuan.rb, line 112
def update_users(userinfos)
  post( ACTION_UPDATE_USERS, { userinfos: userinfos.to_json } )
end