Parse XML Response with One Vs. Multiple results



I am currently parsing latitude and longitude from bing's XML response.


everything works fine when there is only one result but when there is multiple results i want to get the first results


This one only has 1 result. Run in Terminal



cs = "Springfield, Mo"
bing_api_results = HTTParty.get("http://ift.tt/1nyPtsH(/ /, "%20")}?o=xml&key=AvtYnvs3UjKaIPdG5v1YaVBL_5-Rhg_zgUwoQgvTiTS9dMxJSreIanWVLvTzQc86")
geo_data = bing_api_results.parsed_response
lat = geo_data["Response"]["ResourceSets"]["ResourceSet"]["Resources"]["Location"]["Point"]["Latitude"]
lng = geo_data["Response"]["ResourceSets"]["ResourceSet"]["Resources"]["Location"]["Point"]["Longitude"]
name = geo_data["Response"]["ResourceSets"]["ResourceSet"]["Resources"]["Location"]["Name"]


This one has 2 results... and errors



cs = "Monett, Mo"
bing_api_results = HTTParty.get("http://ift.tt/1nyPtsH(/ /, "%20")}?o=xml&key=AvtYnvs3UjKaIPdG5v1YaVBL_5-Rhg_zgUwoQgvTiTS9dMxJSreIanWVLvTzQc86")
geo_data = bing_api_results.parsed_response
lat = geo_data["Response"]["ResourceSets"]["ResourceSet"]["Resources"]["Location"]["Point"]["Latitude"]
lng = geo_data["Response"]["ResourceSets"]["ResourceSet"]["Resources"]["Location"]["Point"]["Longitude"]
name = geo_data["Response"]["ResourceSets"]["ResourceSet"]["Resources"]["Location"]["Name"]


I can get the info if i do this



cs = "Springfield, Mo"
bing_api_results = HTTParty.get("http://ift.tt/1nyPtsH(/ /, "%20")}?o=xml&key=AvtYnvs3UjKaIPdG5v1YaVBL_5-Rhg_zgUwoQgvTiTS9dMxJSreIanWVLvTzQc86")
geo_data = bing_api_results.parsed_response
lat = geo_data["Response"]["ResourceSets"]["ResourceSet"]["Resources"]["Location"].first["Point"]["Latitude"]
lng = geo_data["Response"]["ResourceSets"]["ResourceSet"]["Resources"]["Location"].first["Point"]["Longitude"]
name = geo_data["Response"]["ResourceSets"]["ResourceSet"]["Resources"]["Location"].first["Name"]


but if i use the .first on the ones with only one location i get value of nil.


What logic would work best to use the right set?


I figured out i could do this but is there a better shorter way:



bing_api_results = HTTParty.get("http://ift.tt/1nyPtsH(/ /, "%20")}?o=xml&key=AvtYnvs3UjKaIPdG5v1YaVBL_5-Rhg_zgUwoQgvTiTS9dMxJSreIanWVLvTzQc86")
geo_data = bing_api_results.parsed_response
lat = geo_data["Response"]["ResourceSets"]["ResourceSet"]["Resources"]["Location"]["Point"]["Latitude"]
lat ||= geo_data["Response"]["ResourceSets"]["ResourceSet"]["Resources"]["Location"].first["Point"]["Latitude"]
lng = geo_data["Response"]["ResourceSets"]["ResourceSet"]["Resources"]["Location"]["Point"]["Longitude"]
lng ||= geo_data["Response"]["ResourceSets"]["ResourceSet"]["Resources"]["Location"].first["Point"]["Longitude"]
name = geo_data["Response"]["ResourceSets"]["ResourceSet"]["Resources"]["Location"]["Name"]
name ||= geo_data["Response"]["ResourceSets"]["ResourceSet"]["Resources"]["Location"].first["Name"]
city, state = name.downcase.split(",")
state = state.strip

No comments:

Post a Comment