Skip to content

Commit

Permalink
Merge 31c7c4a into 6e8598a
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndr committed Nov 30, 2014
2 parents 6e8598a + 31c7c4a commit e712e7a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 41 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ gem 'bitters'
gem 'bugsnag'
gem 'google-search'
gem 'htmlentities'
gem 'lyriki'
gem 'neat'
gem 'newrelic_rpm', '>= 3.5.3.25'
gem 'nokogiri'
Expand Down Expand Up @@ -39,6 +40,7 @@ end
group :development do
gem 'awesome_print'
gem 'better_errors'
gem 'binding_of_caller'
gem 'guard'
gem 'guard-rails'
gem 'guard-rspec'
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ GEM
coderay (>= 1.0.0)
erubis (>= 2.6.6)
rack (>= 0.9.0)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
bitters (0.10.1)
bourbon (>= 3.2)
sass (>= 3.2)
Expand Down Expand Up @@ -91,6 +93,7 @@ GEM
thor
crack (0.4.2)
safe_yaml (~> 1.0.0)
debug_inspector (0.0.2)
diff-lcs (1.2.5)
docile (1.1.5)
erubis (2.7.0)
Expand Down Expand Up @@ -136,6 +139,9 @@ GEM
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
lumberjack (1.0.9)
lyriki (0.0.1)
json
nokogiri
mail (2.6.3)
mime-types (>= 1.16, < 3)
method_source (0.8.2)
Expand Down Expand Up @@ -281,6 +287,7 @@ DEPENDENCIES
arel
awesome_print
better_errors
binding_of_caller
bitters
brakeman
bugsnag
Expand All @@ -296,6 +303,7 @@ DEPENDENCIES
guard-rails
guard-rspec
htmlentities
lyriki
neat
newrelic_rpm (>= 3.5.3.25)
nokogiri
Expand Down
37 changes: 6 additions & 31 deletions app/models/concerns/lyrics_wiki.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,20 @@ module LyricsWiki

def fetch_data_for_artist(name)
raise 'no name given' unless name.present?

api_response = Net::HTTP.get(URI("http://lyrics.wikia.com/api.php?func=getArtist&artist=#{LyricsWiki.url_encode(name)}&fmt=realjson"))

raise 'invalid response' unless valid_response?(api_response)
raise 'no albums found' unless has_album_data?(api_response)

JSON.parse(api_response)
Rails.logger.info "Using Lyriki version: #{Lyriki::VERSION}"
Lyriki::Legacy::ArtistData.new(name).response_data
end

def fetch_song_data(artist, song_name)
raise 'need artist and song name' unless artist.present? && song_name.present?

song_data = Net::HTTP.get(URI("http://lyrics.wikia.com/api.php?artist=#{LyricsWiki.url_encode(artist)}&song=#{LyricsWiki.url_encode(song_name)}&fmt=realjson"))
raise 'invalid response' unless valid_response?(song_data)

JSON.parse(song_data)
Rails.logger.info "Using Lyriki version: #{Lyriki::VERSION}"
Lyriki::Legacy::SongData.new(artist: artist, song: song_name).response_data
end

def fetch_lyrics(artist, song_name) # returns array of strings, or nil
# no quality checking
song_data = fetch_song_data(artist, song_name)
raise 'invalid response' unless valid_response?(song_data) && song_data['url']

Nokogiri::HTML(Net::HTTP.get(URI(song_data['url']))).css('div.lyricbox/text()').map(&:text)
Rails.logger.info "Using Lyriki version: #{Lyriki::VERSION}"
Lyriki::Legacy::SongLyrics.new(artist: artist, song: song_name).response_data.presence
end

# "private"

def self.url_encode(str)
ERB::Util.url_encode(str)
end

def valid_response?(response)
response # TODO fix
end

def has_album_data?(response)
response &&
response['albums'] &&
response['albums'].present?
end
end
3 changes: 2 additions & 1 deletion spec/models/artist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,11 @@
describe 'when artist is found' do

before do
allow(subject).to receive(:fetch_data_for_artist).and_return({
fake_artist_data = double("ArtistData", response_data: {
'artist' => 'Foo',
'albums' => %w(bar baz qux),
})
allow(Lyriki::Legacy::ArtistData).to receive(:new).and_return(fake_artist_data)
subject.send :setup
end

Expand Down
9 changes: 0 additions & 9 deletions spec/models/concerns/lyrics_wiki_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,4 @@
end
end

describe '#has_album_data?' do
let(:something_with_lyrics_wiki) { Class.new{ include LyricsWiki }.new }
describe 'when albums are there' do
it 'is true' do
expect(something_with_lyrics_wiki.has_album_data?({'albums' => %w(foo bar)})).to be true
end
end
end

end

0 comments on commit e712e7a

Please sign in to comment.