Class: ITunesObserver::Observer

Inherits:
OSX::NSObject
  • Object
show all
Defined in:
lib/itunes_observer.rb

Overview

Instance Method Summary collapse

Constructor Details

#initializeObserver

Returns a new instance of Observer.



52
53
54
55
56
57
58
59
# File 'lib/itunes_observer.rb', line 52

def initialize
  @callbacks = {}

  notification_centor.addObserver_selector_name_object_(self,
    'onPlayerInfo:',
    'com.apple.iTunes.playerInfo',
    'com.apple.iTunes.player')
end

Instance Method Details

#add_callback(state, &callback) ⇒ Object



73
74
75
76
# File 'lib/itunes_observer.rb', line 73

def add_callback(state, &callback)
  @callbacks[state] ||= []
  @callbacks[state] << callback
end

#finishObject



78
79
80
81
82
# File 'lib/itunes_observer.rb', line 78

def finish
  notification_centor.removeObserver_name_object_(self,
    'com.apple.iTunes.playerInfo',
    'com.apple.iTunes.player')
end

#notification_centorObject



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

def notification_centor
  OSX::NSDistributedNotificationCenter.defaultCenter
end

#onPlayerInfo(info) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/itunes_observer.rb', line 61

def onPlayerInfo(info)
  result = Result.new(info.userInfo)

  STATES.each do |k, state|
    if info.userInfo['Player State'] == state
      (@callbacks[state] || []).each do |callback|
        callback.call(result)
      end
    end
  end
end