class WeChat::Bot::ContactList

微信联系人列表

Public Instance Methods

batch_sync(list) click to toggle source

批量同步联系人数据

更多查看 {#sync} 接口 @param [Array<Hash>] list 联系人数组 @return [ContactList]

# File lib/wechat/bot/contact_list.rb, line 9
def batch_sync(list)
  list.each do |item|
    sync(item)
  end

  self
end
find(**args) click to toggle source

查找用户

@param [Hash] args 接受两个参数:

- :nickname 昵称
- :username 用户ID

@return [Contact]

# File lib/wechat/bot/contact_list.rb, line 44
def find(**args)
  @mutex.synchronize do
    return @cache[args[:username]] if args[:username]

    if args[:nickname]
      @cache.each do |_, contact|
        return contact if contact.nickname == args[:nickname]
      end
    end
  end
end
size() click to toggle source
# File lib/wechat/bot/contact_list.rb, line 17
def size
  @cache.size
end
sync(data) click to toggle source

创建用户或更新用户数据

@param [Hash] data 微信接口返回的单个用户数据 @return [Contact]

# File lib/wechat/bot/contact_list.rb, line 25
def sync(data)
  @mutex.synchronize do
    contact = Contact.parse(data, @bot)
    if @cache[contact.username]
      @cache[contact.username].update(data)
    else
      @cache[contact.username] = contact
    end

    contact
  end
end