xml - Ruby Hash parsed_response error -
background
i using httparty parse xml hash response. unfortunately, when hash response has 1 entry(?), resulting hash not indexable. have confirmed resulting xml syntax same single , multiple entry(?). have confirmed code works when there multiple entries(?) in hash.
question
how accommodate single hash entry case and/or there easier way accomplish trying do?
code
require 'httparty' class rest include httparty format :xml end def test_redeye # rooms , devices roomid = hash.new deviceid = hash.new { |h,k| h[k] = hash.new } rooms = rest.get(@reip["theater"] + "/redeye/rooms/").parsed_response["rooms"] puts "rooms #{rooms}" rooms["room"].each |room| puts "room #{room}" roomid[room["name"].downcase.strip] = "/redeye/rooms/" + room["roomid"] puts "roomid #{roomid}" devices = rest.get(@reip["theater"] + roomid[room["name"].downcase.strip] + "/devices/").parsed_response["devices"] puts "devices #{devices}" devices["device"].each |device| puts "device #{device}" deviceid[room["name"].downcase.strip][device["displayname"].downcase.strip] = "/devices/" + device["deviceid"] puts "deviceid #{deviceid}" end end "done" end
xml - single entry
<?xml version="1.0" encoding="utf-8" ?> <devices> <device manufacturername="philips" description="" porttype="infrared" devicetype="0" modelname="" displayname="tv" deviceid="82" /> </devices>
xml - multiple entry
<?xml version="1.0" encoding="utf-8" ?> <devices> <device manufacturername="denon" description="" porttype="infrared" devicetype="6" modelname="avr-3311ci" displayname="avr" deviceid="77" /> <device manufacturername="philips" description="" porttype="infrared" devicetype="0" modelname="" displayname="tv" deviceid="82" /> </devices>
resulting error
[info - plugin manager] matches, executing block rooms {"room"=>[{"name"=>"home theater", "currentactivityid"=>"78", "roomid"=>"-1", "description"=>""}, {"name"=>"living", "currentactivityid"=>"-1", "roomid"=>"81", "description"=>"2nd floor"}, {"name"=>"theater", "currentactivityid"=>"-1", "roomid"=>"80", "description"=>"1st floor"}]} room {"name"=>"home theater", "currentactivityid"=>"78", "roomid"=>"-1", "description"=>""} roomid {"home theater"=>"/redeye/rooms/-1"} devices {"device"=>[{"manufacturername"=>"denon", "description"=>"", "porttype"=>"infrared", "devicetype"=>"6", "modelname"=>"avr-3311ci", "displayname"=>"avr", "deviceid"=>"77"}, {"manufacturername"=>"philips", "description"=>"", "porttype"=>"infrared", "devicetype"=>"0", "modelname"=>"", "displayname"=>"tv", "deviceid"=>"82"}]} device {"manufacturername"=>"denon", "description"=>"", "porttype"=>"infrared", "devicetype"=>"6", "modelname"=>"avr-3311ci", "displayname"=>"avr", "deviceid"=>"77"} deviceid {"home theater"=>{"avr"=>"/devices/77"}} device {"manufacturername"=>"philips", "description"=>"", "porttype"=>"infrared", "devicetype"=>"0", "modelname"=>"", "displayname"=>"tv", "deviceid"=>"82"} deviceid {"home theater"=>{"avr"=>"/devices/77", "tv"=>"/devices/82"}} room {"name"=>"living", "currentactivityid"=>"-1", "roomid"=>"81", "description"=>"2nd floor"} roomid {"home theater"=>"/redeye/rooms/-1", "living"=>"/redeye/rooms/81"} devices {"device"=>{"manufacturername"=>"philips", "description"=>"", "porttype"=>"infrared", "devicetype"=>"0", "modelname"=>"", "displayname"=>"tv", "deviceid"=>"82"}} device ["manufacturername", "philips"] /usr/local/rvm/gems/ruby-1.9.3-p374@siriproxy/gems/siriproxy-0.3.2/plugins/siriproxy-redeye/lib/siriproxy-redeye.rb:145:in `[]': can't convert string integer (typeerror)
there couple of options see. if command endpoint, modify xml beingness sent accomodate httparty's underlying xml parser, crack putting type="array"
attribute on devices
xml element.
otherwise, check see class device before indexing it:
case devices["device"] when array # deed on collection else # deed on single element end
it's much less ideal whenever have type-checking in dynamic language, if find doing more 1 time may worth introducing polymorphism or @ to the lowest degree extracting method this.
ruby xml hash
No comments:
Post a Comment