Class: Telegram::TelegramContact

Inherits:
TelegramBase show all
Defined in:
lib/telegram/models.rb

Instance Attribute Summary collapse

Attributes inherited from TelegramBase

#client, #id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TelegramBase

#send_image, #send_image_url, #send_message, #send_sticker

Constructor Details

#initialize(client, contact) ⇒ TelegramContact

Returns a new instance of TelegramContact.



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/telegram/models.rb', line 82

def initialize(client, contact)
  @client = client
  @contact = contact

  @id = contact['id']
  @type = 'user'
  @username = contact.has_key?('username') ? contact['username'] : ''
  @name = contact['print_name']
  @phone = contact.has_key?('phone') ? contact['phone'] : ''

  @client.contacts << self unless @client.contacts.include?(self)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



72
73
74
# File 'lib/telegram/models.rb', line 72

def name
  @name
end

#phoneObject (readonly)

Returns the value of attribute phone.



73
74
75
# File 'lib/telegram/models.rb', line 73

def phone
  @phone
end

#typeObject (readonly)

Returns the value of attribute type.



74
75
76
# File 'lib/telegram/models.rb', line 74

def type
  @type
end

Class Method Details

.pick_or_new(client, contact) ⇒ Object



76
77
78
79
80
# File 'lib/telegram/models.rb', line 76

def self.pick_or_new(client, contact)
  ct = client.contacts.find { |c| c.id == contact['id'] }
  return ct unless ct.nil?
  TelegramContact.new(client, contact)
end

Instance Method Details

#chatsObject



95
96
97
# File 'lib/telegram/models.rb', line 95

def chats
  @client.chats.select { |c| c.member.include?(self) }
end

#to_sObject



103
104
105
# File 'lib/telegram/models.rb', line 103

def to_s
  "<TelegramContact #{@name}(#{@id}) username=#{@username}>"
end

#to_tgObject



99
100
101
# File 'lib/telegram/models.rb', line 99

def to_tg
  "#{@type}\##{@id}"
end