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 Exception ProtocolExcepion ServerRsp EmptyServerRsp Base Response dot/f_5.png

violet/response.rb

Summary

this module handle servers messages. Main method/class are Response.parse and Response::Base::ServerRsp (well documented).

you should only use Response.parse with the server‘s message (xml) as argument, it returns a ServerRsp instance from the corresponding class (see all the ServerRsp subclass). with a ServerRsp instance you can use :

r.has_x?
return true if r has at least one element of name "x", return false otherwhise.
r.has_many_xs?
return true if r has more than one element of name "x", return false otherwhise.
r.x
find the first xml element of name x and return it‘s text if any, or a hash of it‘s options
r.x
find all xml element of name x and return an Array of their text if any, or their options.
r.good?
return true if the response is not an error, false otherwhise.
r.bad?
return true if the response is an error, false otherwhise.

Examples :

    >> rsp = Response.parse('<?xml version="1.0" encoding="UTF-8"?><rsp><blacklist nb="2"/><pseudo name="toto"/><pseudo name="titi"/></rsp>')
    => #<Response::Blacklist:0x2acd8c08f2f8 @xml=<UNDEFINED> ... </>>
    >> rsp.good?
    => true
    >> rsp.has_message?
    => false
    >> rsp.message
    NameError: undefined local variable or method message for #<Response::Blacklist:0x2b1f056afdc0 @xml=<UNDEFINED> ... </>>
    >> rsp.has_blacklist?
    => true
    >> rsp.blacklist
    => {:nb=>"2"}
    >> rsp.pseudo
    => {:name=>"toto"}
    >> rsp.has_many_pseudos?
    => true
    >> rsp.pseudos
    => [{:name=>"toto"}, {:name=>"titi"}]

Low Level

if you want to access to the REXML::Document object of a ServerRsp you can either use rsp.xml or use ServerRsp#get_all method.

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

[Validate]