Class | Response::Base::ServerRsp |
In: |
lib/violet/response.rb
|
Parent: | Object |
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).
xml | [R] | It‘s possible to access the REXML::Document object if needed, but try to use virtual accessors and get_all if possible. |
return true if the response is an error, false otherwhise.
# File lib/violet/response.rb, line 88 88: def bad? 89: self.is_a? BadServerRsp 90: end
get all xml‘s element that match name.
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.
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"]
# 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