Module Response
In: lib/violet/response.rb
GoodServerRsp EarPositionSend NabCastSend CommandSend Signature PositionEar VoiceListTts LinkPreview MessageSend LangListUser RabbitName TtsSend ListReceivedMsg ListFriend RabbitSleep Blacklist Timezone RabbitVersion WebRadioSend ChorSend BadServerRsp NoGoodTokenOrSerial ChorNotSend NabCastNotSend MessageNotSend AbuseSending NoCorrectParameters TtsNotSend NotV2Rabbit WebRadioNotSend EarPositionNotSend StandardError ProtocolExcepion ServerRsp EmptyServerRsp Base Response dot/f_6.png

Methods

parse  

Classes and Modules

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

Public Class methods

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

[Source]

     # File lib/violet/response.rb, line 393
393:   def Response.parse raw
394:     tmp = Base::ServerRsp.new raw # we shouldn't create ServerRsp instances, but act as if you didn't see ;)
395:     klassname = if raw =~ %r|<rsp>\s*</rsp>|i
396:                   'EmptyServerRsp'
397:                 elsif tmp.has_message?
398:                   /^#{tmp.message}$/i
399:                 else
400:                   /^#{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
401:                 end
402: 
403:     klass = nil
404:     begin
405:       klass = Helpers.constantize "#{self}::#{Response.constants.grep(klassname).first}"
406:       raise if klass.nil?
407:     rescue
408:       raise ProtocolExcepion.new("unknown server's response : #{raw}")
409:     end
410: 
411:     klass.new raw
412:   end

[Validate]