]> git.r.bdr.sh - rbdr/lyricli.rb/blob - lib/lyricli/sources/rdio.rb
afb736d606069e9812092031c442fa86a39754a2
[rbdr/lyricli.rb] / lib / lyricli / sources / rdio.rb
1 module Lyricli
2 module Sources
3 module Rdio
4
5 # Returns the name of the source, in snake_case
6 def self.name
7 "rdio"
8 end
9
10 # The enable method should run all of the tasks needed to validate
11 # the source. In the case of Rdio it has to authenticate with OAuth.
12 def self.enable
13 # Validation Code
14 @config = Lyricli::Config
15 unless @config[:rdio_auth_token] && !@config[:rdio_auth_token].empty?
16 create_auth_token
17 end
18
19 end
20
21 # Instantiates everything it needs to run.
22 def self.start
23 @rdio = Rdio::SimpleRdio.new([@config[:rdio_key], @config[:rdio_secret]], @config[:rdio_auth_token])
24 end
25
26 # The current_track method should return the name of the current
27 # artist and song.
28 # @return [Hash] A hash containing the current `:song` and `:artist`.
29 def self.current_track
30 u = @rdio.call('currentUser', {'extras' => 'lastSongPlayed'})
31 artist = u["result"]["lastSongPlayed"]["artist"]
32 song = u["result"]["lastSongPlayed"]["name"]
33 {artist: artist, song: song}
34 end
35
36 # The reset method resets any configurations it may have
37 def self.reset
38 # Reset Code
39 end
40
41 private
42
43 # Signs in to rdio with our credentials and requests access for a new auth
44 # token.
45 def create_auth_token
46 rdio = Rdio::SimpleRdio.new([@config])
47
48 # Request Authorization
49 puts "Follow this URL to authorize lyricli:"
50 auth_url = rdio.begin_authentication('oob')
51 puts auth_url
52 Launchy.open(auth_url)
53
54 # Request Code, Obtain Token
55 print "Please type the authorization code: "
56 auth_code = gets.chomp
57 token = rdio.complete_authentication(auth_code)
58
59 @config[:rdio_auth_token] = token
60 token
61 end
62
63 end
64 end
65 end