Module: Scrapescrobbler::CLI

Extended by:
CLI
Included in:
CLI
Defined in:
lib/scrapescrobbler/cli.rb,
lib/scrapescrobbler/commands/listen.rb,
lib/scrapescrobbler/commands/stations.rb,
lib/scrapescrobbler/commands/authenticate.rb

Constant Summary collapse

USAGE =
<<-EOF

Scrapescrobbler - Old-Fashioned Radio Station Scrobbling

Usage: #{File.basename $0} COMMAND [OPTIONS] [ARGS...]

where COMMAND is one of:
  * listen - start scrobbling a radio station's playlist
usage: ss listen [STATION]
  * stations - list all available stations
usage: ss stations
  * authenticate - re-authenticate with last.fm
usage: ss authenticate
  * configure - write out a config file. print path to config file.

OTHER OPTIONS
-h, --help              Display this help

Submit bugs and feature requests to http://github.com/peplin/scrapescrobbler/issues
EOF

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argsObject

Returns the value of attribute args.



3
4
5
# File 'lib/scrapescrobbler/cli.rb', line 3

def args
  @args
end

Instance Method Details

#authenticateObject



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/scrapescrobbler/commands/authenticate.rb', line 3

def authenticate
  lastfm = Lastfm.new Config['api_key'], Config['api_secret']
  if not Config['token']
    token = lastfm.auth.get_token
    say "Go to http://www.last.fm/api/auth/?api_key=#{Config['api_key']}&token=#{token} to allow scrapescrobbler to access your last.fm account."
    Config.update! 'token' => token, 'session' => nil
    exit
  elsif not Config['session']
    Config.update! 'session' => lastfm.auth.get_session(Config['token'])
  end
end

#commandsObject



35
36
37
# File 'lib/scrapescrobbler/cli.rb', line 35

def commands
  Scrapescrobbler::CLI::USAGE.scan(/\* \w+/).map{|s| s.gsub(/\* /, '')}
end

#invokeObject



31
32
33
# File 'lib/scrapescrobbler/cli.rb', line 31

def invoke
  args['-h'] ? say(USAGE) : invoke_command_if_valid
end

#invoke_command_if_validObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/scrapescrobbler/cli.rb', line 43

def invoke_command_if_valid
  command = args.unused.shift
  set_global_options
  case (valid = commands.select{|name| name =~ %r|^#{command}|}).size
  when 0 then say "Invalid command: #{command}"
  when 1 then send valid[0]
  else
    say "Ambiguous command: #{command}" if command
    say(USAGE)
  end
end

#listenObject



3
4
5
6
7
8
9
10
# File 'lib/scrapescrobbler/commands/listen.rb', line 3

def listen
  listener = Listener.new unused_args
  listener.listen
  while true
    listener.update
    sleep 60
  end
end

#parse(arguments) ⇒ Object



27
28
29
# File 'lib/scrapescrobbler/cli.rb', line 27

def parse arguments
  args.parse arguments
end

#say(*something) ⇒ Object



39
40
41
# File 'lib/scrapescrobbler/cli.rb', line 39

def say *something
  puts *something
end

#set_global_optionsObject

currently just sets whether output should be rounded to 15 min intervals



56
57
58
# File 'lib/scrapescrobbler/cli.rb', line 56

def set_global_options
  Scrapescrobbler::Entry.round = true if args['-r']
end

#stationsObject



3
4
5
# File 'lib/scrapescrobbler/commands/stations.rb', line 3

def stations
  say "FOO"
end