]> git.r.bdr.sh - rbdr/lyricli.rb/blob - lib/lyricli/sources/rdio.rb
Add dangling files
[rbdr/lyricli.rb] / lib / lyricli / sources / rdio.rb
1 module Lyricli
2 module Sources
3
4 # This is the Source for rdio
5 class Rdio
6
7 class << self
8 attr_accessor :name
9 end
10
11 @name = "rdio"
12
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
17 @config = Configuration.instance
18 unless @config["rdio_auth_token"] && !@config["rdio_auth_token"].empty?
19 create_auth_token
20 end
21
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
26 end
27
28 # Instantiates everything it needs to run.
29 def initialize
30 @name = 'rdio'
31 @config = Configuration.instance
32 @rdio = ::Rdio::SimpleRdio.new([@config["rdio_key"], @config["rdio_secret"]], @config["rdio_auth_token"])
33 end
34
35 # The current_track method should return the name of the current
36 # artist and song.
37 #
38 # @return [Hash] A hash containing the current `:song` and `:artist`.
39 def current_track
40 response = @rdio.call('currentUser', {'extras' => 'lastSongPlayed'})
41 artist = response["result"]["lastSongPlayed"]["artist"]
42 song = response["result"]["lastSongPlayed"]["name"]
43 {:artist => artist, :song => song}
44 end
45
46 # The reset method resets any configurations it may have
47 def self.reset
48 @config = Configuration.instance
49 @config.delete("rdio_auth_token")
50 end
51
52 # Signs in to rdio with our credentials and requests access for a new auth
53 # token.
54 def self.create_auth_token
55 rdio = ::Rdio::SimpleRdio.new([@config["rdio_key"], @config["rdio_secret"]], @config["rdio_auth_token"])
56
57 # Request Authorization
58 puts "Follow this URL to authorize lyricli:"
59 auth_url = rdio.begin_authentication('oob')
60 puts auth_url
61 ::Launchy.open(auth_url)
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
68 @config["rdio_auth_token"] = token
69 token
70 end
71 end
72 end
73 end