ruby - How to store the nested hash path of the xml response? -


i have following xml response converted hash savon

{:read_measurements_list_response=>{:read_measurements_list_result=>{:sensor_data_list=>{:sensor_data=>[{:type=>"humidity", :value=>"26.20"}, {:type=>"temperature", :value=>"33.12"}, {:type=>"light", :value=>"5501.0"}, {:type=>"soilmoisture", :value=>"0.223"}, {:type=>"conductivity", :value=>"0.031"}, {:type=>"groundtemperature", :value=>"26.9"}]}}, :@xmlns=>"http://tempuri.org/"}} 

so can dig inside sensor_data key-pairs with

res_body[:read_measurements_list_response][:read_measurements_list_result][:sensor_data_list][:sensor_data] 

how can store path variable can change whenever xml response changes structure? have tried using several things none work. nice able res_body[hash_path].

you're asking 2 things:

  1. how can store path variable?

it depends on environment: store in database, file, hardcode in code, pass default value method, etc.

  1. it nice able res_body[hash_path]

you'll have roll own solution. work:

def hash_search(hash, keys)   return hash.fetch(keys.first, nil) if hash.blank? || keys.size <= 1   return hash_search(hash.fetch(keys[0], {}), keys[1..-1]) end  > hash = {a: {b: {c: 1}}} => {:a=>{:b=>{:c=>1}}} > hash_search(hash, [:a, :b, :c]) => 1 

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -