Class: Application

Inherits:
Object
  • Object
show all
Defined in:
lib/lyrics/cli/application.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Application

Returns a new instance of Application.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lyrics/cli/application.rb', line 8

def initialize(options = {})
  Plugins.load_plugins( options[:sites] || Plugins.find_plugins() )

  @featuring_fix = options[:featuring_fix] || true
  @submit_plugin = nil
  @logger = Logger.new( "#{ENV["HOME"]}/.wikilyrics.log" )
  Plugins.all_plugins().each() do |plugin|
    plugin.cleanup_lyrics = options[:cleanup_lyrics] || true
    plugin.logger = @logger
    if plugin.plugin_name() == options[:submit]
      @submit_plugin = plugin
      @submit_plugin.set_submit_settings( options[:username],
                                          options[:password],
                                          options[:review] || true,
                                          options[:prompt_autogen] || false,
                                          options[:prompt_no_lyrics] || false)
    end
  end
end

Class Method Details

.notify(message) ⇒ Object



32
33
34
# File 'lib/lyrics/cli/application.rb', line 32

def Application.notify( message )
  $stderr.puts "Wiki-Lyrics: " + message.gsub( /\/?<[^>]+>/, "" )
end

Instance Method Details

#fetch(artist, title, album = nil, year = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/lyrics/cli/application.rb', line 73

def fetch(artist, title, album = nil, year = nil)
  if @featuring_fix
    artist = Strings.cleanup_artist( artist, title )
    title  = Strings.cleanup_title( title )
  end

  request = Lyrics::Request.new( artist, title, album, year )
  response, response_plugin = fetch_lyrics( request )
  response
end

#fetch_lyrics(request) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lyrics/cli/application.rb', line 48

def fetch_lyrics( request )

  used_plugins = Plugins.all_plugins()

  response = nil
  response_plugin = nil
  submit_plugin_searched = false

  used_plugins.each() do |plugin|
    begin
      submit_plugin_searched = true if plugin == @submit_plugin
      response = plugin.lyrics_full_search( request )
      if response.lyrics
        response_plugin = plugin
        break
      end
    rescue TimeoutError
      notify( I18n.get( "cli.application.plugintimeout", plugin.plugin_name(), plugin.site_host() ) )
    end
  end

  return response, response_plugin

end

#finalizeObject



28
29
30
# File 'lib/lyrics/cli/application.rb', line 28

def finalize()
  @logger.finalize() if @logger
end

#notify(message) ⇒ Object



36
37
38
# File 'lib/lyrics/cli/application.rb', line 36

def notify( message )
  self.class.notify( message )
end

#process(artist, title, album, year) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/lyrics/cli/application.rb', line 84

def process( artist, title, album, year )
  notify( I18n.get( "cli.application.searchinglyrics", title, artist ) )

  response = fetch(artist, title, album, year)

  if response.lyrics
    artist = response.artist ? response.artist : response.request.artist
    title = response.title ? response.title : response.request.title
    puts "\n#{response.lyrics}\n\n"
  else
    notify( I18n.get( "cli.application.nolyricsfound", request.title, request.artist ) )
  end

end

#restore_session(session_file) ⇒ Object



40
41
42
# File 'lib/lyrics/cli/application.rb', line 40

def restore_session( session_file )
  @submit_plugin.restore_session( session_file ) if @submit_plugin
end

#save_session(session_file) ⇒ Object



44
45
46
# File 'lib/lyrics/cli/application.rb', line 44

def save_session( session_file )
  @submit_plugin.save_session( session_file ) if @submit_plugin
end