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

#mention_nameString

Returns Mention name of this account.

Examples:

client.mention_name #=> "alice"

Returns:

  • (String)

    Mention name of this account



115
116
117
# File 'lib/xrc/client.rb', line 115

def mention_name
  users[jid].try(:mention_name)
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



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

def on_event(&block)
  @on_event_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



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

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



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

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



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

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



86
87
88
89
90
91
92
93
# File 'lib/xrc/client.rb', line 86

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



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

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