Class: Hallon::Scrobbler

Inherits:
Object
  • Object
show all
Defined in:
lib/hallon/scrobbler.rb

Overview

The Hallon::Scrobbler is responsible for controlling play scrobbling.

You can construct the scrobbler with different providers to control scrobbling for each one individually. The scrobbler includes a list of social providers, methods to adjust the scrobbling of libspotify, and methods to retrieve the current scrobbling state.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider) ⇒ Scrobbler

Note:

it appears that in libspotify v12.1.56, the only valid provider is :facebook — all other providers return errors

Initialize the scrobbler with a social provider.

Parameters:

  • provider (Symbol)

Raises:

  • (ArgumentError)

    if the given provider is invalid



24
25
26
27
# File 'lib/hallon/scrobbler.rb', line 24

def initialize(provider)
  provider_to_i = Spotify.enum_value!(provider, "social provider")
  @provider = Spotify.enum_type(:social_provider)[provider_to_i]
end

Instance Attribute Details

#providerSymbol (readonly)

Returns social provider.

Returns:

  • (Symbol)

    social provider



15
16
17
# File 'lib/hallon/scrobbler.rb', line 15

def provider
  @provider
end

Class Method Details

.providersArray<Symbol>

Returns list of available scrobbling providers.

Returns:

  • (Array<Symbol>)

    list of available scrobbling providers



10
11
12
# File 'lib/hallon/scrobbler.rb', line 10

def self.providers
  Spotify.enum_type(:social_provider).symbols
end

Instance Method Details

#credentials=(credentials) ⇒ Object

Sets the scrobbling credentials.

Examples:

setting username and password

scrobbling.credentials = 'kim', 'password'

Parameters:

  • credentials (Array<Username, Password>)


59
60
61
62
# File 'lib/hallon/scrobbler.rb', line 59

def credentials=(credentials)
  username, password = Array(credentials)
  Spotify.try(:session_set_social_credentials, session.pointer, provider, username, password)
end

#enabled=(scrobble) ⇒ Object

Enables or disables the local scrobbling setting.

Parameters:

  • scrobble (Boolean)

    true if you want scrobbling to be enabled



67
68
69
70
# File 'lib/hallon/scrobbler.rb', line 67

def enabled=(scrobble)
  state = scrobble ? :local_enabled : :local_disabled
  Spotify.try(:session_set_scrobbling, session.pointer, provider, state)
end

#enabled?Boolean

Returns true if scrobbling (global or local) is enabled.

Returns:

  • (Boolean)

    true if scrobbling (global or local) is enabled.



73
74
75
76
77
78
79
# File 'lib/hallon/scrobbler.rb', line 73

def enabled?
  FFI::Buffer.alloc_out(:int) do |buffer|
    Spotify.session_is_scrobbling(session.pointer, provider, buffer)
    state = read_state(buffer.read_uint)
    return !! (state =~ /enabled/)
  end
end

#possible?Boolean

Note:

if this returns false, it usually means libspotify either has no scrobbling credentials, or the user has disallowed spotify from scrobbling to the given provider

Note:

this method only works for the :facebook provider; for all other providers it will always return true

Returns true if scrobbling is possible.

Returns:

  • (Boolean)

    true if scrobbling is possible



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hallon/scrobbler.rb', line 37

def possible?
  case provider
  when :spotify, :lastfm
    # libspotify v12.1.56 has a bug with all providers except for :facebook
    # where the return value is always :invalid_indata; however, the devs
    # also mentioned the function would always return true for all other
    # providers anyway
    true
  else
    FFI::Buffer.alloc_out(:bool) do |buffer|
      Spotify.try(:session_is_scrobbling_possible, session.pointer, provider, buffer)
      return ! buffer.read_uchar.zero?
    end
  end
end

#read_state(state) ⇒ Symbol (protected)

Convert an integer state to an actual state symbol.

Parameters:

  • state (Integer)

Returns:

  • (Symbol)

    state as a symbol



94
95
96
# File 'lib/hallon/scrobbler.rb', line 94

def read_state(state)
  Spotify.enum_type(:scrobbling_state)[state]
end

#resetScrobbler

Sets the local scrobbling state to the global state.

Returns:



84
85
86
# File 'lib/hallon/scrobbler.rb', line 84

def reset
  tap { Spotify.try(:session_set_scrobbling, session.pointer, provider, :use_global_setting) }
end

#sessionHallon::Session (protected)

Returns:



99
100
101
# File 'lib/hallon/scrobbler.rb', line 99

def session
  Session.instance
end