]>
Commit | Line | Data |
---|---|---|
b8498f5c BB |
1 | module Lyricli |
2 | module Sources | |
d3e32008 | 3 | class Rdio |
b8498f5c | 4 | |
d3e32008 BB |
5 | class << self |
6 | attr_accessor :name | |
b8498f5c BB |
7 | end |
8 | ||
d3e32008 BB |
9 | @name = "rdio" |
10 | ||
b8498f5c BB |
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 | |
d3e32008 BB |
15 | @config = Lyricli::Configuration.instance |
16 | unless @config["rdio_auth_token"] && !@config["rdio_auth_token"].empty? | |
b8498f5c BB |
17 | create_auth_token |
18 | end | |
19 | ||
20 | end | |
21 | ||
22 | # Instantiates everything it needs to run. | |
d3e32008 BB |
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"]) | |
b8498f5c BB |
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`. | |
d3e32008 BB |
32 | def current_track |
33 | response = @rdio.call('currentUser', {'extras' => 'lastSongPlayed'}) | |
34 | artist = response["result"]["lastSongPlayed"]["artist"] | |
35 | song = response["result"]["lastSongPlayed"]["name"] | |
b8498f5c BB |
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 | ||
b8498f5c BB |
44 | # Signs in to rdio with our credentials and requests access for a new auth |
45 | # token. | |
d3e32008 BB |
46 | def self.create_auth_token |
47 | @rdio = Rdio::SimpleRdio.new([@config["rdio_key"], @config["rdio_secret"]], @config["rdio_auth_token"]) | |
b8498f5c BB |
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 | ||
d3e32008 | 60 | @config["rdio_auth_token"] = token |
b8498f5c BB |
61 | token |
62 | end | |
63 | ||
64 | end | |
65 | end | |
66 | end |