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