Class: Skype::Api

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/skype/api.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



18
19
20
# File 'lib/skype/api.rb', line 18

def instance
  @instance ||= new
end

Instance Method Details

#attach(application_name = "ruby-skype") ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/skype/api.rb', line 23

def attach(application_name="ruby-skype")
  raise "Already attached." if @attached

  # Say hi to Skype.
  api.Invoke "NAME #{application_name}"
  api.Invoke "PROTOCOL 7"

  run_notification_thread

  @attached = true
end

#invoke(message, &block) ⇒ Object



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

def invoke(message, &block)
  raise "Not attached to skype. Call Skype::Api.attach first." unless @attached

  log_outgoing message
  if block_given?
    api.Invoke(message) do |headers, answer|
      log_incoming answer
      block.call(answer)
    end
  else
    answer = api.Invoke(message) do |_, _|
      # Huh? Without passing in a block, sometimes it hangs...??
    end
    log_incoming answer
    answer
  end
end

#notify(message) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/skype/api.rb', line 60

def notify(message)
  log_incoming message
  
  callbacks.keys.each do |key|
    next unless match = Regexp.new("^#{key}").match(message)
    callbacks[key].each{ |callback| callback.call(*match.captures) }
  end
end

#on_notification(scope, proc = nil, &block) ⇒ Object



53
54
55
56
57
58
# File 'lib/skype/api.rb', line 53

def on_notification(scope, proc=nil, &block)
  raise "Need to register callbacks before attaching to Skype." if @attached

  callbacks[scope] ||= []
  callbacks[scope] << (proc ? proc : block)
end