Class: WeChat::Bot::Contact

Inherits:
Object
  • Object
show all
Defined in:
lib/wechat/bot/contact.rb

Overview

微信联系人

可以是用户、公众号、群组等

Defined Under Namespace

Modules: Kind

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot) ⇒ Contact

Returns a new instance of Contact.



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

def initialize(bot)
  @bot = bot
  @data = {}
end

Class Method Details

.parse(obj, bot) ⇒ Object



14
15
16
# File 'lib/wechat/bot/contact.rb', line 14

def self.parse(obj, bot)
  self.new(bot).parse(obj)
end

Instance Method Details

#cityObject

城市



64
65
66
# File 'lib/wechat/bot/contact.rb', line 64

def city
  attr(:city)
end

#displaynameObject

群聊显示名



39
40
41
# File 'lib/wechat/bot/contact.rb', line 39

def displayname
  attr(:displayname)
end

#find_member(args) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/wechat/bot/contact.rb', line 93

def find_member(args)
  if args[:username]
    members.find{|contact| args[:username] == contact.username }
  elsif args[:nickname]
    members.find{|contact| args[:nickname] == contact.nickname }
  end
end

#group?Boolean

是否群聊

Returns:

  • (Boolean)


74
75
76
# File 'lib/wechat/bot/contact.rb', line 74

def group?
  kind == Kind::Group
end

#kindObject

用户类型



54
55
56
# File 'lib/wechat/bot/contact.rb', line 54

def kind
  attr(:kind)
end

#membersHash

群组成员列表

只有群组才有内容,根据 #kind#group? 来判断 不是群组类型的返回空数组

Returns:

  • (Hash)


89
90
91
# File 'lib/wechat/bot/contact.rb', line 89

def members
  attr(:members)
end

#mp?Boolean

是否公众号

Returns:

  • (Boolean)


79
80
81
# File 'lib/wechat/bot/contact.rb', line 79

def mp?
  kind == Kind::MP
end

#nicknameObject

用户昵称



29
30
31
# File 'lib/wechat/bot/contact.rb', line 29

def nickname
  attr(:nickname)
end

#parse(raw, update = false) ⇒ Contact

联系人解析

Parameters:

  • raw (Hash<Object, Object>)

Returns:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/wechat/bot/contact.rb', line 105

def parse(raw, update = false)
  @raw = raw

  parse_kind
  parse_members

  @raw.each do |key, value|
    if attribute = mapping[key]
      next if value.to_s.empty? && update

      sync(attribute, value)
    end
  end

  self
end

#provinceObject

省份



59
60
61
# File 'lib/wechat/bot/contact.rb', line 59

def province
  attr(:province)
end

#remarknameObject

备注名



34
35
36
# File 'lib/wechat/bot/contact.rb', line 34

def remarkname
  attr(:remarkname)
end

#sexObject

性别



44
45
46
# File 'lib/wechat/bot/contact.rb', line 44

def sex
  attr(:sex)
end

#signatureObject

个人签名



49
50
51
# File 'lib/wechat/bot/contact.rb', line 49

def signature
  attr(:signature)
end

#special?Boolean

是否特殊账户

Returns:

  • (Boolean)


69
70
71
# File 'lib/wechat/bot/contact.rb', line 69

def special?
  kind == Kind::Special
end

#to_sObject Also known as: inspect



127
128
129
# File 'lib/wechat/bot/contact.rb', line 127

def to_s
  "#<#{self.class}:#{object_id.to_s(16)} username='#{username}' nickname='#{nickname}' kind='#{kind}' members=#{members.size}>"
end

#update(raw) ⇒ Object



122
123
124
125
# File 'lib/wechat/bot/contact.rb', line 122

def update(raw)
  @raw = raw
  parse(@raw, true)
end

#usernameObject

用户唯一 ID



24
25
26
# File 'lib/wechat/bot/contact.rb', line 24

def username
  attr(:username)
end