Class: WeChat::Bot::ContactList

Inherits:
CachedList show all
Defined in:
lib/wechat/bot/contact_list.rb

Overview

微信联系人列表

Instance Method Summary collapse

Methods inherited from CachedList

#each, #initialize

Constructor Details

This class inherits a constructor from WeChat::Bot::CachedList

Instance Method Details

#batch_sync(list) ⇒ ContactList

批量同步联系人数据

更多查看 #sync 接口

Parameters:

  • list (Array<Hash>)

    联系人数组

Returns:



11
12
13
14
15
16
17
# File 'lib/wechat/bot/contact_list.rb', line 11

def batch_sync(list)
  list.each do |item|
    sync(item)
  end

  self
end

#find(**args) ⇒ Contact

查找用户

Parameters:

  • args (Hash)

    接受两个参数:

    • :nickname 昵称

    • :username 用户ID

Returns:



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/wechat/bot/contact_list.rb', line 61

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

#sizeObject



19
20
21
# File 'lib/wechat/bot/contact_list.rb', line 19

def size
  @cache.size
end

#sync(data) ⇒ Contact

创建用户或更新用户数据

Parameters:

  • data (Hash)

    微信接口返回的单个用户数据

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wechat/bot/contact_list.rb', line 27

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