I am using the Last.fm API and I am trying to get a lot of information about a specific user and return it to xml. So here is the challenge in my opinion:
<%= form_tag fetch_user_path, :remote => true, :'data-type' => 'xml', :id => 'search' do %>
<%= text_field_tag :q %>
<% end %>
So, as you can see, it expects XML, and I correctly handle the callback using jQuery. Then in my controller:
def user
username = params[:q].gsub(' ','+')
get_info_url = "http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user=#{username}&api_key=#{API_KEY}"
get_friends_url = "http://ws.audioscrobbler.com/2.0/?method=user.getfriends&user=#{username}&api_key=#{API_KEY}"
respond_to do |format|
format.xml {
begin
@info = Nokogiri::XML(open(get_info_url))
@friends = Nokogiri::XML(open(get_friends_url))
rescue Exception => e
if e.message == '400 Bad Request'
render xml: { :error => 'User not found.' }, :status => 400
else
render xml: { :error => 'Connection to Last.fm failed.' }, :status => 500
end
else
render xml: @info
end
}
end
Thus, I am correctly returning the xml returned get_info_url. However, I want to join this xml for the xml returned get_friends_url. How can i do this?
After Ben Miller's answer, I get a parserror on my callback. I think this is due to both xml files containing xml version. And maybe the combined file is not working? I see that xml files are concatenated, this is how they look with console.log:
Error: Invalid XML: <?xml version="1.0"?>
<Combined>
<UserInfo>
<?xml version="1.0" encoding="utf-8"??>
<lfm status="ok">
<user>
# lots of info about the user
</user>
</lfm>
</UserInfo>
<FriendInfo>
<?xml version="1.0" encoding="utf-8"??>
<lfm status="ok">
<friends for="user" page="1" perpage="50" totalpages="2" total="96">
# lots of info about the user friends
</friends>
</lfm>
</FriendInfo>
</Combined>