Class: Homophone::Application

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

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



12
13
14
# File 'lib/homophone/application.rb', line 12

def initialize
  @show_genre = false
end

Instance Method Details

#formatterObject



57
58
59
60
# File 'lib/homophone/application.rb', line 57

def formatter
  opts = {:show_genre => show_genre?}
  @formatter ||= Homophone::Formatter::ConsoleFormatter.new(opts)
end

#ohai(msg) ⇒ Object



62
63
64
65
# File 'lib/homophone/application.rb', line 62

def ohai(msg)
  puts msg
  exit 0
end

#onoe(exit_code, msg) ⇒ Object



67
68
69
70
# File 'lib/homophone/application.rb', line 67

def onoe(exit_code, msg)
  STDERR.puts msg
  exit exit_code
end

#run(argv) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/homophone/application.rb', line 16

def run(argv)
  opts = Slop.parse do |o|
    o.banner = 'Usage: homophone [-g] ARTIST'
    o.separator ''
    o.separator 'Options:'

    o.bool '-g', '--genre', 'Show artist genres'

    o.on '-h', '--help' do
      ohai o
    end

    o.on '--version' do
      ohai "homophone v#{Homophone::VERSION}"
    end
  end

  musician_name = opts.arguments.shift
  onoe 127, 'No musician name provided' unless musician_name

  @show_genre = opts.genre?

  musician = spotify_service.musician(musician_name)
  onoe 126, %Q{No musician with name "#{musician_name}" found} if musician.nil?

  puts "#{formatter.format(musician)}:\n\n" if show_genre?

  artists = musician.related_artists.sort_by(&:name)
  artists.each do |artist|
    puts formatter.format(artist)
  end
end

#show_genre?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/homophone/application.rb', line 53

def show_genre?
  !!@show_genre
end

#spotify_serviceObject



49
50
51
# File 'lib/homophone/application.rb', line 49

def spotify_service
  @service ||= (cucumber? ? Homophone::Service::DummySpotify : Homophone::Service::Spotify).new
end