]> git.r.bdr.sh - rbdr/lyricli.rb/blob - lib/lyricli/lyricli.rb
518db2df8d380f7671d58c8986c931eb6b8afa1c
[rbdr/lyricli.rb] / lib / lyricli / lyricli.rb
1 module Lyricli
2
3 # This class has the basic logic for extracting the lyrics and controlling the
4 # application
5 class Lyricli
6
7 # Constructor, initializes `@source_manager`
8 def initialize
9 @source_manager = SourceManager.new
10 end
11
12 # Raises an InvalidLyricsException which means we did not get any valid
13 # artist/song from any of the sources
14 #
15 # @raise [Lyricli::InvalidLyricsException] because we found nothing
16 def exit_with_error
17 raise InvalidLyricsException
18 end
19
20 # Extracts the current track, validates it and requests the lyrics from our
21 # LyricsEngine
22 #
23 # @return [String] the found lyrics, or a string indicating none were found
24 def get_lyrics
25 set_current_track
26 check_params
27
28 engine = LyricsEngine.new(@current_track[:artist], @current_track[:song])
29
30 begin
31 engine.get_lyrics
32 rescue LyricsNotFoundException
33 "Lyrics not found :("
34 end
35 end
36
37 # Set the `@current_track` instance variable by asking the SourceManager for
38 # its current track
39 def set_current_track
40 @current_track = @source_manager.current_track
41 end
42
43 # Exits with error when there is an empty field from the current track.
44 def check_params
45 self.exit_with_error if @current_track[:artist].nil? or @current_track[:artist].empty?
46 self.exit_with_error if @current_track[:song].nil? or @current_track[:song].empty?
47 end
48 end
49 end