Created
May 14, 2011 02:33
-
-
Save new2/971740 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
attr_accessible :email, :name | |
validates :email, :presence => true | |
validates :name, :presence => true | |
validates_uniqueness_of :email, :scope => :user | |
has_many :song_entries | |
has_many :songs, :through => :song_entries | |
require 'open-uri' | |
class << self | |
def get_data(url) | |
doc = Nokogiri::XML(open(url)) | |
songs = [] | |
doc.xpath('//rss/channel/item').each do |node| | |
songs << { :track => node.xpath('mm:Track/dc:title').text.strip.downcase, | |
:artist => node.xpath('mm:Artist/dc:title').text.strip.downcase, | |
:artwork => node.xpath('albumArtUrl').text.strip } | |
populate(songs.last) | |
end | |
end | |
def populate(songs) | |
song = Song.find_or_create_by_track_and_artist_and_artworkUrl( | |
:track => songs[:track], | |
:artist => songs[:artist], | |
:artworkUrl => songs[:artwork] | |
) | |
song_entries.find_or_create_by_song_id( | |
:song_id => song.id) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment