Class: AppEngine::XMPP::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/appengine-apis/xmpp.rb

Overview

Represents an incoming or outgoing XMPP Message. Also includes support for parsing chat commands. Commands are of the form

/{command} {arg}?

A backslash is also recognized as the first character to support chat client which internally handle / commands.

Constant Summary collapse

ARG_INDEX =
{:to => 0, :body => 1, :from => 2, :type => 3, :xml => 4}
COMMAND_REGEX =
/^[\\\/](\S+)(\s+(.+))?/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Message

call-seq:

Message.new(to, body, from=nil, type=:chat, xml=false)
or
Message.new(options)

Constructor for sending an outgoing XMPP message or parsing an incoming XMPP message.

Args / Options:

:to

Destination JID or array of JIDs for the message.

:body

Body of the message.

:from

Optional custom sender JID. The default is <appid>@appspot.com. Custom JIDs can be of the form <anything>@<appid>.appspotchat.com.

:type

Optional type. Valid types are :chat, :error, :groupchat, :headline, and :normal. See RFC 3921, section 2.1.1. The default is :chat.

:xml

If true specifies that the body should be interpreted as XML. If false, the contents of the body will be escaped and placed inside of a body element inside of the message. If true, the contents will be made children of the message.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/appengine-apis/xmpp.rb', line 95

def initialize(*args)
  if args.size == 1
    options = args[0]
  elsif args[-1].kind_of? Hash
    options = args.pop
  else
    options = {}
  end
  @recipients = fetch_arg(:to, options, args)
  @body = fetch_arg(:body, options, args)
  unless @recipients && @body
    raise ArgumentError, "Recipient and body are required."
  end
  @recipients = [@recipients] unless @recipients.kind_of? Array
  
  @sender = fetch_arg(:from, options, args)
  @type = fetch_arg(:type, options, args) || :chat
  @xml = !!fetch_arg(:xml, options, args)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



70
71
72
# File 'lib/appengine-apis/xmpp.rb', line 70

def body
  @body
end

#recipientsObject (readonly)

Returns the value of attribute recipients.



70
71
72
# File 'lib/appengine-apis/xmpp.rb', line 70

def recipients
  @recipients
end

#senderObject (readonly)

Returns the value of attribute sender.



70
71
72
# File 'lib/appengine-apis/xmpp.rb', line 70

def sender
  @sender
end

#typeObject (readonly)

Returns the value of attribute type.



70
71
72
# File 'lib/appengine-apis/xmpp.rb', line 70

def type
  @type
end

Instance Method Details

#argObject

If this message contains a chat command, returns the command argument. Otherwise, returns the message body.



127
128
129
130
# File 'lib/appengine-apis/xmpp.rb', line 127

def arg
  parse_command
  @arg
end

#commandObject

Returns the command if this message contains a chat command.



120
121
122
123
# File 'lib/appengine-apis/xmpp.rb', line 120

def command
  parse_command
  @command
end

#reply(body, type = :chat, xml = false) ⇒ Object

Convenience method to reply to a message.



133
134
135
136
# File 'lib/appengine-apis/xmpp.rb', line 133

def reply(body, type=:chat, xml=false)
  message = Message.new([sender], body, recipients[0], type, xml)
  XMPP.send_message(message)
end

#xml?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/appengine-apis/xmpp.rb', line 115

def xml?
  @xml
end