]> git.r.bdr.sh - rbdr/lyricli.rb/blame - lib/lyricli/lyricli.rb
Add dangling files
[rbdr/lyricli.rb] / lib / lyricli / lyricli.rb
CommitLineData
823e558b 1module Lyricli
34d0bf15
BB
2
3 # This class has the basic logic for extracting the lyrics and controlling the
4 # application
823e558b
BB
5 class Lyricli
6
34d0bf15 7 # Constructor, initializes `@source_manager`
823e558b
BB
8 def initialize
9 @source_manager = SourceManager.new
10 end
11
76e96cc0 12 # Raises an InvalidLyricsError which means we did not get any valid
34d0bf15
BB
13 # artist/song from any of the sources
14 #
76e96cc0 15 # @raise [Lyricli::Exceptions::InvalidLyricsError] because we found nothing
823e558b 16 def exit_with_error
76e96cc0 17 raise Exceptions::InvalidLyricsError
823e558b
BB
18 end
19
34d0bf15
BB
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
b2ac6893 24 def get_lyrics(show_title=false)
5d701d44
BB
25
26 begin
27 set_current_track
28 check_params
29 rescue Exceptions::InvalidLyricsError
30 return "No Artist/Song could be found :("
31 end
823e558b
BB
32
33 engine = LyricsEngine.new(@current_track[:artist], @current_track[:song])
34
35 begin
b2ac6893
BB
36 lyrics_output = engine.get_lyrics
37
38 if show_title
39 lyrics_title = "#{@current_track[:artist]} - #{@current_track[:song]}"
40 lyrics_output = "#{lyrics_title}\n\n#{lyrics_output}"
41 end
42
43 return lyrics_output
76e96cc0 44 rescue Exceptions::LyricsNotFoundError
5d701d44 45 return "Lyrics not found :("
823e558b
BB
46 end
47 end
48
34d0bf15
BB
49 # Set the `@current_track` instance variable by asking the SourceManager for
50 # its current track
823e558b
BB
51 def set_current_track
52 @current_track = @source_manager.current_track
53 end
54
34d0bf15 55 # Exits with error when there is an empty field from the current track.
823e558b 56 def check_params
5d701d44 57 self.exit_with_error unless @current_track
823e558b
BB
58 self.exit_with_error if @current_track[:artist].nil? or @current_track[:artist].empty?
59 self.exit_with_error if @current_track[:song].nil? or @current_track[:song].empty?
60 end
61 end
62end