]>
Commit | Line | Data |
---|---|---|
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 InvalidLyricsError which means we did not get any valid | |
13 | # artist/song from any of the sources | |
14 | # | |
15 | # @raise [Lyricli::Exceptions::InvalidLyricsError] because we found nothing | |
16 | def exit_with_error | |
17 | raise Exceptions::InvalidLyricsError | |
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 | ||
26 | begin | |
27 | set_current_track | |
28 | check_params | |
29 | rescue Exceptions::InvalidLyricsError | |
30 | return "No Artist/Song could be found :(" | |
31 | end | |
32 | ||
33 | engine = LyricsEngine.new(@current_track[:artist], @current_track[:song]) | |
34 | ||
35 | begin | |
36 | return engine.get_lyrics | |
37 | rescue Exceptions::LyricsNotFoundError | |
38 | return "Lyrics not found :(" | |
39 | end | |
40 | end | |
41 | ||
42 | # Set the `@current_track` instance variable by asking the SourceManager for | |
43 | # its current track | |
44 | def set_current_track | |
45 | @current_track = @source_manager.current_track | |
46 | end | |
47 | ||
48 | # Exits with error when there is an empty field from the current track. | |
49 | def check_params | |
50 | self.exit_with_error unless @current_track | |
51 | self.exit_with_error if @current_track[:artist].nil? or @current_track[:artist].empty? | |
52 | self.exit_with_error if @current_track[:song].nil? or @current_track[:song].empty? | |
53 | end | |
54 | end | |
55 | end |