Class: Nicoalert::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/nicoalert/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mail, password) ⇒ Client

Returns a new instance of Client.



11
12
13
14
# File 'lib/nicoalert/client.rb', line 11

def initialize(mail, password)
  @mail = mail
  @password = password
end

Instance Attribute Details

#mailObject (readonly)

Returns the value of attribute mail.



9
10
11
# File 'lib/nicoalert/client.rb', line 9

def mail
  @mail
end

Instance Method Details

#addrObject



24
25
26
# File 'lib/nicoalert/client.rb', line 24

def addr
  @addr ||= status.css('ms addr').text
end

#communitiesObject



36
37
38
# File 'lib/nicoalert/client.rb', line 36

def communities
  @communities ||= status.css('communities community_id').map(&:text)
end

#connect(&block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/nicoalert/client.rb', line 40

def connect(&block)
  TCPSocket.open(addr, port) do |socket|
    socket <<
      %{<thread thread="#{thread}" version="20061206" res_from="-1" />\0}
    while line = socket.gets("\0")
      item = Nokogiri::XML.fragment(line.sub("\0", ''))
      next unless chat = item.css('chat')
      id, community, user = chat.text.split(',')
      next unless id && community && user
      block.call Live.new(id, community)
    end
  end
end

#portObject



28
29
30
# File 'lib/nicoalert/client.rb', line 28

def port
  @port ||= status.css('ms port').text
end

#threadObject



32
33
34
# File 'lib/nicoalert/client.rb', line 32

def thread
  @thread ||= status.css('ms thread').text
end

#user_hashObject



20
21
22
# File 'lib/nicoalert/client.rb', line 20

def user_hash
  @user_hash ||= status.css('user_hash').text
end

#user_idObject



16
17
18
# File 'lib/nicoalert/client.rb', line 16

def user_id
  @user_id ||= status.css('user_id').text
end