SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    May 2005
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with REXML and XPath

    I'm putting together a Ruby script to parse a last.fm 'Recent Tracks' XML feed and grab the first result it comes to.

    Unfortunately I seem to be biting off more than I can chew.

    I've found some code that seems to 'almost' do the job, as I've modified it to grab the appropriate feed and then to attempt to take only the first track rather than all listed in the feed.

    The code is as follows:

    Code:
    require 'rexml/document'
    require 'net/http'
    include REXML
    
    class SongController < ApplicationController
    
    def update
        @user = 'lastfmusername'
        @path = '/1.0/user/'+@user+'/recenttracks.xml'
      
        @xml = Net::HTTP.start('ws.audioscrobbler.com') do |http|
          req = Net::HTTP::Get.new(@path)
          http.request(req).body
        end
        
        #This is the original scripts loop control which parsed the whole feed
        #REXML::Document.new(@xml).elements.each('//track') do |el|
        
        #The two lines below are my broken replacement attempt to grab only
        #data from the first track...
        @feed = REXML::Document.new(@xml)
        XPath.first( @feed, '//track' ) do |el|
          @songs = Song.new
          @songs.title = el.elements["name"].text
          @songs.last_played = el.elements["date"].text
          @songs.save
        end
        
    end
    
    end
    I've checked thoroughly that the feed is available and the script works with the original line in, so I'm completely sure that the fault lies in the two lines that now open the loop.

    There's no apparent error (aside from 'no rhtml template', which is expected), the data is just not loading into the database which leads me to suspect it's not being read correctly by the loop.

    I'm sure this is probably completely obvious to those more experienced than myself.

  2. #2
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't have enough time to try and debug this for you now but I do have two tips that might help you debug it.

    The first thing I'd recommend is wrapping up this code in a model or at the very least a class of its own in the lib folder.

    I'd then take advantage of Rails builtin unit testing to test your new class - if you have experience writing classes test-first, this would be even better. Using unit tests you'll be able to break up your code into easily testable chunks which should help you isolate the problem.

    Hope that helps, and sorry I couldn't be more useful at this time in directly debugging your code!

  3. #3
    SitePoint Member
    Join Date
    May 2005
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's cool, I've not touched the unit testing stuff, so sounds worth a go and I'll learn something new. Thanks Luke.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •