]> git.r.bdr.sh - rbdr/lyricli.rb/blame_incremental - lib/lyricli/sources/rdio.rb
Enables rdio. :)
[rbdr/lyricli.rb] / lib / lyricli / sources / rdio.rb
... / ...
CommitLineData
1module Lyricli
2 module Sources
3 class Rdio
4
5 class << self
6 attr_accessor :name
7 end
8
9 @name = "rdio"
10
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
15 @config = Configuration.instance
16 unless @config["rdio_auth_token"] && !@config["rdio_auth_token"].empty?
17 create_auth_token
18 end
19
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
24 end
25
26 # Instantiates everything it needs to run.
27 def initialize
28 @name = 'rdio'
29 @config = Configuration.instance
30 @rdio = ::Rdio::SimpleRdio.new([@config["rdio_key"], @config["rdio_secret"]], @config["rdio_auth_token"])
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`.
36 def current_track
37 response = @rdio.call('currentUser', {'extras' => 'lastSongPlayed'})
38 artist = response["result"]["lastSongPlayed"]["artist"]
39 song = response["result"]["lastSongPlayed"]["name"]
40 {artist: artist, song: song}
41 end
42
43 # The reset method resets any configurations it may have
44 def self.reset
45 @config = Configuration.instance
46 @config.delete("rdio_auth_token")
47 end
48
49 # Signs in to rdio with our credentials and requests access for a new auth
50 # token.
51 def self.create_auth_token
52 rdio = ::Rdio::SimpleRdio.new([@config["rdio_key"], @config["rdio_secret"]], @config["rdio_auth_token"])
53
54 # Request Authorization
55 puts "Follow this URL to authorize lyricli:"
56 auth_url = rdio.begin_authentication('oob')
57 puts auth_url
58 ::Launchy.open(auth_url)
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
65 @config["rdio_auth_token"] = token
66 token
67 end
68
69 end
70 end
71end