Class: QQPengyou

Inherits:
Object
  • Object
show all
Defined in:
lib/qq-pengyou.rb

Constant Summary

VERSION =
'1.0.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id, app_key, app_name) ⇒ QQPengyou

构造函数 参数:

- app_id : 应用的唯一ID
- app_key : 应用的密钥,用于验证应用的合法性
- app_name : 应用的英文名(唯一)


30
31
32
33
34
35
# File 'lib/qq-pengyou.rb', line 30

def initialize(app_id,app_key,app_name)
  @server_name = 'openapi.pengyou.qq.com'
  @app_id = app_id
  @app_key = app_key
  @app_name = app_name
end

Instance Attribute Details

#server_nameObject (readonly)

Pengyou OpenAPI 服务器的域名 默认:openapi.pengyou.qq.com



23
24
25
# File 'lib/qq-pengyou.rb', line 23

def server_name
  @server_name
end

Class Method Details

.valid_open_id?(open_id) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/qq-pengyou.rb', line 38

def valid_open_id?(open_id)
  return true if /^[0-9a-fA-F]{32}$/.match(open_id)
  false
end

Instance Method Details

#api(method, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/qq-pengyou.rb', line 45

def api(method,options = {})
  # 验证信息
  if options[:openid] == nil or options[:openid] == ''
    return {:ret => PYO_ERROR_REQUIRED_PARAMETER_EMPTY, :msg => 'openid is empty'}
  end
  if options[:openkey] == nil or options[:openkey] == ''
    return {:ret => PYO_ERROR_REQUIRED_PARAMETER_EMPTY, :msg => 'openkey is empty'}
  end
  if not QQPengyou.valid_open_id?(options[:openid])
    return {:ret => PYO_ERROR_REQUIRED_PARAMETER_INVALID, :msg => 'openid is invalid'}
  end

  # 添加一些参数
  options[:appid] = @app_id
  options[:appkey] = @app_key
  options[:ref] = @app_name

  # 得到 OpenAPI 的地址
  path = get_api_path(method)

  return make_request(path,options)
end

#get_emotion(openid, openkey, options = {}) ⇒ Object

  • items : Array 用户信息

- openid: 好友QQ号码转化得到的id - content: 好友的校友心情内容



217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/qq-pengyou.rb', line 217

def get_emotion(openid, openkey, options = {})
  fopenids = options[:fopenids]
  if fopenids == nil or fopenids.class != [].class
    return { :ret => PYO_ERROR_REQUIRED_PARAMETER_EMPTY, :msg => 'fopenids is empty or not a Array type.' }
  end
  
  
  return api('xyoapp_get_emotion',
              :openid => openid,
              :openkey => openkey,
              :fopenids => fopenids.join("_"))
end

#get_friend_list(openid, openkey, options = {}) ⇒ Object

  • ret : 返回码 (0:正确返回; (0,1000):部分数据获取错误,相当于容错的返回; [1000,~]错误)

- items : array 用户信息 - openid : 好友QQ号码转化得到的id - nickname : 昵称(infoed==1时返回) - gender : 性别(infoed==1时返回) - figureurl : 头像url(infoed==1时返回)



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/qq-pengyou.rb', line 150

def get_friend_list(openid,openkey, options = {})
  infoed = options[:infoed] || 0
  apped = options[:apped] || 1
  page = options[:page] || 0
  return api('xyoapp_get_relationinfo',
      :openid => openid,
      :openkey => openkey,
      :infoed => infoed,
      :apped => apped,
      :page => page)
end

#get_multi_info(openid, openkey, options = {}) ⇒ Object

  • gender : 性别

- figureurl : 头像url - is_vip : 是否黄钻 (true:黄钻; false:普通用户) - is_year_vip : 是否年费黄钻 (is_vip为true才显示) - vip_level : 黄钻等级 (is_vip为true才显示)



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/qq-pengyou.rb', line 175

def get_multi_info(openid, openkey, options = {})
  fopenids = options[:fopenids]
  if fopenids == nil or fopenids.class != [].class
    return { :ret => PYO_ERROR_REQUIRED_PARAMETER_EMPTY, :msg => 'fopenids is empty or not a Array type.' }
  end
  
  return api('xyoapp_multi_info',
              :openid => openid,
              :openkey => openkey,
              :fopenids => fopenids.join("_"))
end

#get_user_info(openid, openkey) ⇒ Object

  • vip_level : 黄钻等级(如果是黄钻用户才返回此字段)



113
114
115
# File 'lib/qq-pengyou.rb', line 113

def (openid,openkey)
  return api('xyoapp_get_userinfo',:openid => openid, :openkey => openkey)
end

#is_friend(openid, openkey, options = {}) ⇒ Object

返回值(Hash):

- ret  : 返回码 (0:正确返回, [1000,~]错误)
- isFriend  : 是否为好友(0:不是好友; 1:是好友; 2:是同班同学)


126
127
128
129
130
131
132
133
134
135
# File 'lib/qq-pengyou.rb', line 126

def is_friend(openid,openkey, options = {})
  if not QQPengyou.valid_open_id?(options[:fopenid])
    return {:ret => PYO_ERROR_REQUIRED_PARAMETER_INVALID, :msg => 'fopenid is invalid' }
  end
  
  return api('xyoapp_get_isrelation', 
              :openid => openid, 
              :openkey => openkey, 
              :fopenid => options[:fopenid])
end

#setuped?(openid, openkey) ⇒ Boolean

  • setuped : 是否安装(0:没有安装;1:安装)

Returns:

  • (Boolean)


191
192
193
194
195
# File 'lib/qq-pengyou.rb', line 191

def setuped?(openid, openkey)
  return api('xyoapp_get_issetuped',
              :openid => openid,
              :openkey => openkey)
end

#vip?(openid, openkey) ⇒ Boolean

判断用户是否为黄钻 返回值 hash:

- ret : 返回码 (0:正确返回, [1000,~]错误)

- is_vip : 是否黄钻 (true:黄钻; false:普通用户)

Returns:

  • (Boolean)


201
202
203
204
205
# File 'lib/qq-pengyou.rb', line 201

def vip?(openid, openkey)
  return api('xyoapp_pay_showvip',
              :openid => openid,
              :openkey => openkey)
end