Class: Xrc::Client

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

Constant Summary collapse

DEFAULT_PORT =
5222
PING_INTERVAL =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Examples:

client = Xrc::Client.new(jid: "[email protected]")

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :port (String)

    Port number to connect server (default: 5222)

  • :jid (String)

    Jabber ID of your account (required)

  • :nickname (String)

    Pass nickname for the Room (optional)

  • :password (String)

    Password to connect server (optional)

  • :room_jid (String)

    Room Jabber ID to join in after authentication (optional)



17
18
19
# File 'lib/xrc/client.rb', line 17

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#usersXrc::Roster (readonly)

Returns Users information existing in the server.

Returns:

  • (Xrc::Roster)

    Users information existing in the server



8
9
10
# File 'lib/xrc/client.rb', line 8

def users
  @users
end

Instance Method Details

#connectnil

Connects to the JID’s server and waits for message

Returns:

  • (nil)

    Returns nothing



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

def connect
  connection.connect
  nil
end

#join(jids) ⇒ Object



137
138
139
140
141
# File 'lib/xrc/client.rb', line 137

def join(jids)
  Array(jids).each do |room_jid|
    post(Elements::Join.new(from: jid.strip, to: "#{room_jid}/#{nickname}"))
  end
end

#mention_nameString

Returns Mention name of this account.

Examples:

client.mention_name #=> "alice"

Returns:

  • (String)

    Mention name of this account



133
134
135
# File 'lib/xrc/client.rb', line 133

def mention_name
  users[jid].try(:mention_name)
end

#on_connection_established { ... } ⇒ Proc

Registers a callback called when a client connected to server

Examples:

client.on_connection_established do
  puts "connection established!"
end

Yields:

  • Executes a given callback in the Client’s context

Returns:

  • (Proc)

    Returns given block



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

def on_connection_established(&block)
  @on_connection_established_block = block
end

#on_event {|element| ... } ⇒ Proc

Registers a callback called when client received a new XML element from server

Examples:

client.on_event do |element|
  puts "Received #{element}"
end

Yields:

  • Executes a given callback in the Client’s context

Yield Parameters:

  • element (REXML::Element)

    Represents a new XML element

Returns:

  • (Proc)

    Returns given block



88
89
90
# File 'lib/xrc/client.rb', line 88

def on_event(&block)
  @on_event_block = block
end

#on_invite(&block) ⇒ Proc

Registers a callback for invitation message

Returns:

  • (Proc)

    Returns given block



94
95
96
# File 'lib/xrc/client.rb', line 94

def on_invite(&block)
  @on_invite_block = block
end

#on_private_message {|element| ... } ⇒ Proc

Registers a callback called when client received a new private message (a.k.a. 1vs1 message)

Examples:

client.on_private_message do |message|
  puts "#{message.from}: #{message.body}"
end

Yields:

  • Executes a given callback in the Client’s context

Yield Parameters:

  • element (Xrc::Message)

    Represents a given message

Returns:

  • (Proc)

    Returns given block



49
50
51
# File 'lib/xrc/client.rb', line 49

def on_private_message(&block)
  @on_private_message_block = block
end

#on_room_message {|element| ... } ⇒ Proc

Registers a callback called when client received a new room message

Examples:

client.on_room_message do |message|
  puts "#{message.from}: #{message.body}"
end

Yields:

  • Executes a given callback in the Client’s context

Yield Parameters:

  • element (Xrc::Message)

    Represents a given message

Returns:

  • (Proc)

    Returns given block



62
63
64
# File 'lib/xrc/client.rb', line 62

def on_room_message(&block)
  @on_room_message_block = block
end

#on_subject {|element| ... } ⇒ Proc

Registers a callback called when client received a new message with subject

Examples:

client.on_subject do |element|
  puts "Subject: #{element.subject}"
end

Yields:

  • Executes a given callback in the Client’s context

Yield Parameters:

  • element (Xrc::Message)

    Represents a given message

Returns:

  • (Proc)

    Returns given block



75
76
77
# File 'lib/xrc/client.rb', line 75

def on_subject(&block)
  @on_subject_block = block
end

#reply(options) ⇒ REXML::Element

Replies to given message

Examples:

client.reply(body: "Thanks", to: message)

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :to (Xrc::Messages::Base)

    A message object given from server

  • :body (String)

    A text to be sent to server

Returns:

  • (REXML::Element)

    Returns an element sent to server



104
105
106
107
108
109
110
111
# File 'lib/xrc/client.rb', line 104

def reply(options)
  say(
    body: options[:body],
    from: options[:to].to,
    to: options[:to].from,
    type: options[:to].type,
  )
end

#say(options) ⇒ REXML::Element

Send a message

Examples:

client.say(body: "Thanks", from: "[email protected]", to: "[email protected]", type: "chat")

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :body (String)

    Message body

  • :from (String)

    Sender’s JID

  • :to (String)

    Address JID

  • :type (String)

    Message type (e.g. chat, groupchat)

Returns:

  • (REXML::Element)

    Returns an element sent to server



121
122
123
124
125
126
127
128
# File 'lib/xrc/client.rb', line 121

def say(options)
  post Elements::Message.new(
    body: options[:body],
    from: options[:from],
    to: options[:to],
    type: options[:type],
  )
end