]> git.r.bdr.sh - rbdr/lyricli.rb/blame - lib/lyricli/sources/rdio.rb
Add a README
[rbdr/lyricli.rb] / lib / lyricli / sources / rdio.rb
CommitLineData
b8498f5c
BB
1module Lyricli
2 module Sources
d3e32008 3 class Rdio
b8498f5c 4
d3e32008
BB
5 class << self
6 attr_accessor :name
b8498f5c
BB
7 end
8
d3e32008
BB
9 @name = "rdio"
10
b8498f5c
BB
11 # The enable method should run all of the tasks needed to validate
12 # the source. In the case of Rdio it has to authenticate with OAuth.
13 def self.enable
14 # Validation Code
4f3dbb13 15 @config = Configuration.instance
d3e32008 16 unless @config["rdio_auth_token"] && !@config["rdio_auth_token"].empty?
b8498f5c
BB
17 create_auth_token
18 end
19
4f3dbb13
BB
20 puts "***"
21 puts "Hello, rdio tends to be a bit aggressive and tends to have trouble with other sources. If you're having trouble, you can disable it temporarily. You will not have to reauthenticate."
22 puts "***"
23
b8498f5c
BB
24 end
25
26 # Instantiates everything it needs to run.
d3e32008
BB
27 def initialize
28 @name = 'rdio'
4f3dbb13
BB
29 @config = Configuration.instance
30 @rdio = ::Rdio::SimpleRdio.new([@config["rdio_key"], @config["rdio_secret"]], @config["rdio_auth_token"])
b8498f5c
BB
31 end
32
33 # The current_track method should return the name of the current
34 # artist and song.
35 # @return [Hash] A hash containing the current `:song` and `:artist`.
d3e32008
BB
36 def current_track
37 response = @rdio.call('currentUser', {'extras' => 'lastSongPlayed'})
38 artist = response["result"]["lastSongPlayed"]["artist"]
39 song = response["result"]["lastSongPlayed"]["name"]
b8498f5c
BB
40 {artist: artist, song: song}
41 end
42
43 # The reset method resets any configurations it may have
44 def self.reset
4f3dbb13
BB
45 @config = Configuration.instance
46 @config.delete("rdio_auth_token")
b8498f5c
BB
47 end
48
b8498f5c
BB
49 # Signs in to rdio with our credentials and requests access for a new auth
50 # token.
d3e32008 51 def self.create_auth_token
4f3dbb13 52 rdio = ::Rdio::SimpleRdio.new([@config["rdio_key"], @config["rdio_secret"]], @config["rdio_auth_token"])
b8498f5c
BB
53
54 # Request Authorization
55 puts "Follow this URL to authorize lyricli:"
56 auth_url = rdio.begin_authentication('oob')
57 puts auth_url
4f3dbb13 58 ::Launchy.open(auth_url)
b8498f5c
BB
59
60 # Request Code, Obtain Token
61 print "Please type the authorization code: "
62 auth_code = gets.chomp
63 token = rdio.complete_authentication(auth_code)
64
d3e32008 65 @config["rdio_auth_token"] = token
b8498f5c
BB
66 token
67 end
68
69 end
70 end
71end