Skip to content

Instantly share code, notes, and snippets.

@new2
Created May 14, 2011 02:33
Show Gist options
  • Save new2/971740 to your computer and use it in GitHub Desktop.
Save new2/971740 to your computer and use it in GitHub Desktop.
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