Class: Lyricli::Sources::Rdio

Inherits:
Object
  • Object
show all
Defined in:
lib/lyricli/sources/rdio.rb

Overview

This is the Source for rdio

Class Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Rdio) initialize

Instantiates everything it needs to run.



29
30
31
32
33
# File 'lib/lyricli/sources/rdio.rb', line 29

def initialize
  @name = 'rdio'
  @config = Configuration.instance
  @rdio = ::Rdio::SimpleRdio.new([@config["rdio_key"], @config["rdio_secret"]], @config["rdio_auth_token"])
end

Class Attribute Details

+ (Object) name

Returns the value of attribute name



8
9
10
# File 'lib/lyricli/sources/rdio.rb', line 8

def name
  @name
end

Class Method Details

+ (Object) create_auth_token

Signs in to rdio with our credentials and requests access for a new auth token.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lyricli/sources/rdio.rb', line 54

def self.create_auth_token
  rdio = ::Rdio::SimpleRdio.new([@config["rdio_key"], @config["rdio_secret"]], @config["rdio_auth_token"])

  # Request Authorization
  puts "Follow this URL to authorize lyricli:"
  auth_url = rdio.begin_authentication('oob')
  puts auth_url
  ::Launchy.open(auth_url)

  # Request Code, Obtain Token
  print "Please type the authorization code: "
  auth_code = gets.chomp
  token = rdio.complete_authentication(auth_code)

  @config["rdio_auth_token"] = token
  token
end

+ (Object) enable

The enable method should run all of the tasks needed to validate the source. In the case of Rdio it has to authenticate with OAuth.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lyricli/sources/rdio.rb', line 15

def self.enable
  # Validation Code
  @config = Configuration.instance
  unless @config["rdio_auth_token"] && !@config["rdio_auth_token"].empty?
    create_auth_token
  end

  puts "***"
  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."
  puts "***"

end

+ (Object) reset

The reset method resets any configurations it may have



47
48
49
50
# File 'lib/lyricli/sources/rdio.rb', line 47

def self.reset
  @config = Configuration.instance
  @config.delete("rdio_auth_token")
end

Instance Method Details

- (Hash) current_track

The current_track method should return the name of the current artist and song.

Returns:

  • (Hash)

    A hash containing the current `:song` and `:artist`.



39
40
41
42
43
44
# File 'lib/lyricli/sources/rdio.rb', line 39

def current_track
  response = @rdio.call('currentUser', {'extras' => 'lastSongPlayed'})
  artist = response["result"]["lastSongPlayed"]["artist"]
  song = response["result"]["lastSongPlayed"]["name"]
  {artist: artist, song: song}
end