Class: Whatsapp::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/whatsapp/group.rb

Overview

Represents a WhatsApp group.

Examples:

group = Whatsapp::Group.new(id: "123@g.us", subject: "My Group", size: 5)
group.subject  # => "My Group"
group.size     # => 5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Group

Returns a new instance of Group.

Parameters:

  • data (Hash)

    group data from the bridge



35
36
37
38
39
40
41
42
43
44
# File 'lib/whatsapp/group.rb', line 35

def initialize(data)
  @raw = data
  @id = data[:id]
  @subject = data[:subject]
  @description = data[:description]
  @owner = data[:owner]
  @creation = data[:creation]
  @size = data[:size] || 0
  @participants = data[:participants]
end

Instance Attribute Details

#creationInteger? (readonly)

Returns creation timestamp.

Returns:

  • (Integer, nil)

    creation timestamp



23
24
25
# File 'lib/whatsapp/group.rb', line 23

def creation
  @creation
end

#descriptionString? (readonly)

Returns group description.

Returns:

  • (String, nil)

    group description



17
18
19
# File 'lib/whatsapp/group.rb', line 17

def description
  @description
end

#idString (readonly)

Returns group JID (e.g. "120363001234567890@g.us").

Returns:



11
12
13
# File 'lib/whatsapp/group.rb', line 11

def id
  @id
end

#ownerString? (readonly)

Returns group owner JID.

Returns:

  • (String, nil)

    group owner JID



20
21
22
# File 'lib/whatsapp/group.rb', line 20

def owner
  @owner
end

#participantsArray<Hash>? (readonly)

Returns participants list (+[{ id:, admin: }]+).

Returns:

  • (Array<Hash>, nil)

    participants list (+[{ id:, admin: }]+)



29
30
31
# File 'lib/whatsapp/group.rb', line 29

def participants
  @participants
end

#rawHash (readonly)

Returns raw data from the bridge.

Returns:

  • (Hash)

    raw data from the bridge



32
33
34
# File 'lib/whatsapp/group.rb', line 32

def raw
  @raw
end

#sizeInteger (readonly)

Returns number of participants.

Returns:

  • (Integer)

    number of participants



26
27
28
# File 'lib/whatsapp/group.rb', line 26

def size
  @size
end

#subjectString (readonly)

Returns group subject (name).

Returns:

  • (String)

    group subject (name)



14
15
16
# File 'lib/whatsapp/group.rb', line 14

def subject
  @subject
end

Instance Method Details

#adminsArray<String>

List of admin participant JIDs.

Returns:

  • (Array<String>)


49
50
51
52
53
54
55
# File 'lib/whatsapp/group.rb', line 49

def admins
  return [] unless participants

  participants
    .select { |p| p[:admin] }
    .map { |p| p[:id] }
end

#inspectObject



79
80
81
# File 'lib/whatsapp/group.rb', line 79

def inspect
  "#<Whatsapp::Group id=#{id.inspect} subject=#{subject.inspect} size=#{size}>"
end

#participant_idsArray<String>

List of all participant JIDs.

Returns:

  • (Array<String>)


60
61
62
63
64
# File 'lib/whatsapp/group.rb', line 60

def participant_ids
  return [] unless participants

  participants.map { |p| p[:id] }
end

#to_hHash

Returns serialized group info.

Returns:

  • (Hash)

    serialized group info



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/whatsapp/group.rb', line 67

def to_h
  {
    id: id,
    subject: subject,
    description: description,
    owner: owner,
    creation: creation,
    size: size,
    participants: participants
  }
end