]> git.r.bdr.sh - rbdr/lyricli.rb/blob - lib/lyricli/sources/rdio.rb
83548b28af680c85f8794f50e4dc9f398ccb2b55
[rbdr/lyricli.rb] / lib / lyricli / sources / rdio.rb
1 module 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 = Lyricli::Configuration.instance
16 unless @config["rdio_auth_token"] && !@config["rdio_auth_token"].empty?
17 create_auth_token
18 end
19
20 end
21
22 # Instantiates everything it needs to run.
23 def initialize
24 @name = 'rdio'
25 @config = Lyricli::Configuration.instance
26 @rdio = Rdio::SimpleRdio.new([@config["rdio_key"], @config["rdio_secret"]], @config["rdio_auth_token"])
27 end
28
29 # The current_track method should return the name of the current
30 # artist and song.
31 # @return [Hash] A hash containing the current `:song` and `:artist`.
32 def current_track
33 response = @rdio.call('currentUser', {'extras' => 'lastSongPlayed'})
34 artist = response["result"]["lastSongPlayed"]["artist"]
35 song = response["result"]["lastSongPlayed"]["name"]
36 {artist: artist, song: song}
37 end
38
39 # The reset method resets any configurations it may have
40 def self.reset
41 # Reset Code
42 end
43
44 # Signs in to rdio with our credentials and requests access for a new auth
45 # token.
46 def self.create_auth_token
47 @rdio = Rdio::SimpleRdio.new([@config["rdio_key"], @config["rdio_secret"]], @config["rdio_auth_token"])
48
49 # Request Authorization
50 puts "Follow this URL to authorize lyricli:"
51 auth_url = rdio.begin_authentication('oob')
52 puts auth_url
53 Launchy.open(auth_url)
54
55 # Request Code, Obtain Token
56 print "Please type the authorization code: "
57 auth_code = gets.chomp
58 token = rdio.complete_authentication(auth_code)
59
60 @config["rdio_auth_token"] = token
61 token
62 end
63
64 end
65 end
66 end