Class Response::Base::ServerRsp
In: lib/violet/response.rb
Parent: Object
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

base class used to handle Violet server‘s responses.

We want to access to all xml elements easily, like the powerful ActiveRecord ‘find’ function. This class provide virtual accessor and predicate for elements. If you have a ServerRsp, rsp.has_message? will return true if rsp has a ‘message’ element, and rsp.has_many_messages? will return true if rsp has more than one ‘message’ element. rsp.message will return the first message element and rsp.messages will return an Array that contains all message elements of rsp (see doc of Response module for examples).

Methods

bad?   get_all   good?   new  

Attributes

xml  [R]  It‘s possible to access the REXML::Document object if needed, but try to use virtual accessors and get_all if possible.

Public Class methods

create a ServerRsp with the raw argument. raw must be the xml text of the server‘s response. if the xml is malformed, a REXML::ParseException will be raised.

[Source]

    # File lib/violet/response.rb, line 74
74:       def initialize raw
75:         @xml = REXML::Document.new raw
76:       end

Public Instance methods

return true if the response is an error, false otherwhise.

[Source]

    # File lib/violet/response.rb, line 88
88:       def bad?
89:         self.is_a? BadServerRsp
90:       end

Summary

get all xml‘s element that match name.

Arguments

name : name of the element you want to fetch (see examples) block : a block of code that take a REXML::Element in parameter. if no block is given, it return an Array of REXML::Element.

Examples

Side effect

    >> rsp = Response.parse('<?xml version="1.0" encoding="UTF-8"?><rsp><langListUser nb="4"/><myLang lang="fr"/><myLang lang="us"/><myLang lang="uk"/><myLang lang="de"/></rsp>')
    => #<Response::LangListUser:0x2b16c5e17510 @xml=<UNDEFINED> ... </>>
    >> rsp.get_all(:myLang) do |e|
    >>   puts "you can use '#{e.attribute('lang').value}'"
    >> end
    you can use 'fr'
    you can use 'us'
    you can use 'uk'
    you can use 'de'
    => [nil, nil, nil, nil]

usage of returned value

    >> langs = rsp.get_all(:myLang) { |e| e.attribute('lang').value }
    => ["fr", "us", "uk", "de"]

[Source]

     # File lib/violet/response.rb, line 117
117:       def get_all name
118:         # REXML::XPath.match(@xml, element).collect do |e|  <= this one is for a recursive search.
119:         @xml.root.elements.collect(name.to_s) do |e|
120:           if block_given? then yield(e) else e end
121:         end
122:       end

return true if the response is not an error, false otherwhise.

[Source]

    # File lib/violet/response.rb, line 83
83:       def good?
84:         self.is_a? GoodServerRsp
85:       end

[Validate]