Skip to content

Commit

Permalink
searches wikipedia to find canonical name of artist
Browse files Browse the repository at this point in the history
[fixes #39]
  • Loading branch information
alxndr committed Nov 16, 2013
1 parent a20fa33 commit 811b1b1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
14 changes: 9 additions & 5 deletions app/models/artist.rb
Expand Up @@ -6,17 +6,17 @@ def initialize(input)
input.gsub! '-', ' '
input.strip!

artist_name = Artist.find_artist_name(input)
@artist_name = Artist.find_artist_name(input)

raise 'no artist_name' unless artist_name && artist_name.present?
raise 'no artist_name' unless @artist_name && @artist_name.present?

@artist_data = fetch_data_for_artist(artist_name)
@artist_data = fetch_data_for_artist(@artist_name)

raise 'artist not found' unless @artist_data
end

def display_name
@display_name ||= @artist_data['artist']
@artist_name
end

def slug
Expand Down Expand Up @@ -67,12 +67,16 @@ def lyrem(config)

def self.find_artist_name(input)
result = Google::Search::Web.new(query: "#{input} musician site:en.wikipedia.org").first
return '' unless result && result.title
unless result && result.title
raise 'artist name not found'
end
result.title.chomp(' - Wikipedia, the free encyclopedia')
end


private


def fetch_new_song_lyrics
lyrics, i = [], 0
until lyrics.present? || i > 5
Expand Down
42 changes: 23 additions & 19 deletions spec/models/artist_spec.rb
Expand Up @@ -2,27 +2,21 @@

describe Artist do

before do
Artist.stub(:find_artist_name) { 'frank zappa' }
Artist.any_instance.stub(fetch_data_for_artist: {
'artist' => 'Frank Zappa',
'albums' => [
{
'album' => 'Hot Rats',
'year' => '1969',
'songs' => ['Peaches en Regalia', 'Willie the Pimp']
}
]
})
end

describe '#display_name' do
it 'finds a good name from LyricsWiki' do
before do
Artist.stub(:find_artist_name) { 'Frank Zappa' }
Artist.any_instance.stub(fetch_data_for_artist: { })
end
it 'finds a good name' do
Artist.new('frank zappa').display_name.should == 'Frank Zappa'
end
end

describe '#slug' do
before do
Artist.stub(:find_artist_name) { 'Frank Zappa' }
Artist.any_instance.stub(fetch_data_for_artist: { })
end
it 'slugifies display_name' do
Artist.any_instance.stub(:display_name).and_return('Frank Zappa')
Artist.new('fz').slug.should == 'frank-zappa'
Expand All @@ -35,6 +29,10 @@
end

describe '#random_lyric' do
before do
Artist.stub(:find_artist_name) { 'Frank Zappa' }
Artist.any_instance.stub(fetch_data_for_artist: { })
end
describe 'when there are no stored lyrics' do
before do
Artist.any_instance.stub(:rand).and_return(0)
Expand Down Expand Up @@ -74,6 +72,10 @@
end

describe '#lyrem' do
before do
Artist.stub(:find_artist_name) { 'Frank Zappa' }
Artist.any_instance.stub(fetch_data_for_artist: { })
end
let(:fz) { Artist.new('frank zappa') }
let(:phrases) { [
'fringe. I mean that, man.',
Expand All @@ -89,7 +91,7 @@
] }

before do
fz.stub(:fetch_new_song_lyrics).and_return(phrases)
fz.stub(:fetch_new_song_lyrics).and_return(phrases)
end

describe ':phrases' do
Expand Down Expand Up @@ -188,9 +190,11 @@
end

describe '.find_artist_name' do
it 'seaches' do
Google::Search::Web.should_receive(:new).and_return([13])
Artist.find_artist_name 'fz'
it 'seaches wikipedia' do
fake_result = OpenStruct.new
fake_result.title = 'Frank Zappa - Wikipedia, the free encyclopedia'
Google::Search::Web.stub(:new).and_return([fake_result])
Artist.find_artist_name('fz').should == 'Frank Zappa'
end
end

Expand Down

0 comments on commit 811b1b1

Please sign in to comment.