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.



23
24
25
# File 'lib/rype/api.rb', line 23

def thread
  @thread
end

Class Method Details

.instanceObject



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

def instance
  @instance ||= new
end

Instance Method Details

#attach(application_name = "rype") ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rype/api.rb', line 25

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

  # 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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rype/api.rb', line 42

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



67
68
69
70
71
72
73
74
# File 'lib/rype/api.rb', line 67

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



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

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