Class: Rype::Api

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



15
16
17
# File 'lib/rype/api.rb', line 15

def thread
  @thread
end

Class Method Details

.instanceObject



10
11
12
# File 'lib/rype/api.rb', line 10

def instance
  @instance ||= new
end

Instance Method Details

#attach(application_name = "rype") ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rype/api.rb', line 17

def attach(application_name="rype")
  raise "Already attached." if attached?

  @application_name = application_name

  # Say hi to Skype.
  status, = api.Invoke "NAME #{@application_name}"

  if status == 'CONNSTATUS OFFLINE'
    raise Rype::Offline
  elsif status != 'OK'
    raise Rype::Denied
  end

  api.Invoke "PROTOCOL 7"

  run_notification_thread
end

#invoke(message, &block) ⇒ Object



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

def invoke(message, &block)
  raise "Not attached to Skype. Call Rype::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



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

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



54
55
56
57
58
59
# File 'lib/rype/api.rb', line 54

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