Module: Response

Defined in:
lib/violet/response.rb

Defined Under Namespace

Modules: Base Classes: AbuseSending, Blacklist, ChorNotSend, ChorSend, CommandSend, EarPositionNotSend, EarPositionSend, EmptyServerRsp, LangListUser, LinkPreview, ListFriend, ListReceivedMsg, MessageNotSend, MessageSend, NabCastNotSend, NabCastSend, NoCorrectParameters, NoGoodTokenOrSerial, NotV2Rabbit, PositionEar, ProtocolExcepion, RabbitName, RabbitSleep, RabbitVersion, Signature, Timezone, TtsNotSend, TtsSend, VoiceListTts, WebRadioNotSend, WebRadioSend

Class Method Summary collapse

Class Method Details

.parse(raw) ⇒ Object

Summary

parse given raw (xml text) and return a new ServerRsp from the corresponding class.

Violet messages aren’t easy to identify, because there is not id. So we have to study the xml content if there are no message element (easier to detect the response type).

Arguments

the xml response of the Violet Server.

Exceptions

this method raise a ProtocolExcepion if it’s fail to detect the kind of the server’s response.

Examples

>> rsp = Response.parse('<?xml version="1.0" encoding="UTF-8"?><rsp><rabbitSleep>YES</rabbitSleep></rsp>')
=> #<Response::RabbitSleep:0x2b16c5e476e8 @xml=<UNDEFINED> ... </>>
>> rsp.class
=> Response::RabbitSleep

>> rsp = Response.parse('<?xml version="1.0" encoding="UTF-8"?><rsp><rabbitVersion>V1</rabbitVersion></rsp>')
=> #<Response::RabbitVersion:0x2b16c5e154b8 @xml=<UNDEFINED> ... </>>
>> rsp.class
=> Response::RabbitVersion


393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/violet/response.rb', line 393

def Response.parse raw
  tmp = Base::ServerRsp.new raw # we shouldn't create ServerRsp instances, but act as if you didn't see ;)
  klassname = if raw =~ %r|<rsp>\s*</rsp>|i
                'EmptyServerRsp'
              elsif tmp.has_message?
                /^#{tmp.message}$/i
              else
                /^#{tmp.xml.root.elements[1].name}$/i # REXML::Elements#[] has index 1-based and not 0-based, so we really fetch the first element's name
              end

  klass = nil
  begin
    klass = Helpers.constantize "#{self}::#{Response.constants.grep(klassname).first}"
    raise if klass.nil?
  rescue
    raise ProtocolExcepion.new("unknown server's response : #{raw}")
  end

  klass.new raw
end