Class: Hallon::Scrobbler
- Inherits:
-
Object
- Object
- Hallon::Scrobbler
- 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
-
#provider ⇒ Symbol
readonly
Social provider.
Class Method Summary collapse
-
.providers ⇒ Array<Symbol>
List of available scrobbling providers.
Instance Method Summary collapse
-
#credentials=(credentials) ⇒ Object
Sets the scrobbling credentials.
-
#enabled=(scrobble) ⇒ Object
Enables or disables the local scrobbling setting.
-
#enabled? ⇒ Boolean
True if scrobbling (global or local) is enabled.
-
#initialize(provider) ⇒ Scrobbler
constructor
Initialize the scrobbler with a social provider.
-
#possible? ⇒ Boolean
True if scrobbling is possible.
-
#read_state(state) ⇒ Symbol
protected
Convert an integer state to an actual state symbol.
-
#reset ⇒ Scrobbler
Sets the local scrobbling state to the global state.
- #session ⇒ Hallon::Session protected
Constructor Details
#initialize(provider) ⇒ Scrobbler
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.
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
#provider ⇒ Symbol (readonly)
Returns social provider.
15 16 17 |
# File 'lib/hallon/scrobbler.rb', line 15 def provider @provider end |
Class Method Details
.providers ⇒ Array<Symbol>
Returns 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.
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.
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.
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
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
this method only works for the :facebook provider; for all other providers it will always return true
Returns 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.
94 95 96 |
# File 'lib/hallon/scrobbler.rb', line 94 def read_state(state) Spotify.enum_type(:scrobbling_state)[state] end |
#reset ⇒ Scrobbler
Sets the local scrobbling state to the global state.
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 |
#session ⇒ Hallon::Session (protected)
99 100 101 |
# File 'lib/hallon/scrobbler.rb', line 99 def session Session.instance end |