Class: Mac::Skype::Agent

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/mac-skype/agent.rb

Instance Method Summary collapse

Constructor Details

#initializeAgent

Returns a new instance of Agent.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mac-skype/agent.rb', line 10

def initialize
  on_receive = Proc.new do |response|
    if response =~ /^#/
      _, id, response_body = response.match(/^#([^\s]+) (.+)/m).to_a

      if callbacks[id]
        callbacks[id].call(response_body)
        callbacks.delete(id)
      end
    else
      messages.push(response)
    end
  end

  api.callback = on_receive
end

Instance Method Details

#connect(stop_after = 0) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/mac-skype/agent.rb', line 43

def connect(stop_after = 0)
  api.connect if !connected?

  while !connected?
    api.run_loop(stop_after)
    sleep 0.1
  end
end

#connected?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/mac-skype/agent.rb', line 39

def connected?
  api.attach == 1
end

#disconnectObject



52
53
54
55
# File 'lib/mac-skype/agent.rb', line 52

def disconnect
  api.disconnect
  api.attach = 0
end

#nameObject



35
36
37
# File 'lib/mac-skype/agent.rb', line 35

def name
  api.name
end

#on_message(&callback) ⇒ Object



27
28
29
# File 'lib/mac-skype/agent.rb', line 27

def on_message(&callback)
  message_callbacks << callback if callback
end

#run_foreverObject



86
87
88
89
90
91
92
93
# File 'lib/mac-skype/agent.rb', line 86

def run_forever
  loop do
    api.run_loop(0)
    consume_messages

    sleep 0.1
  end
end

#send_command_async(command_str, &callback) ⇒ Object



57
58
59
60
61
62
# File 'lib/mac-skype/agent.rb', line 57

def send_command_async(command_str, &callback)
  id = generate_id

  callbacks[id] = callback
  api.send_command('#%s %s' % [id, command_str])
end

#send_command_sync(command_str) ⇒ Object Also known as: send_command



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mac-skype/agent.rb', line 64

def send_command_sync(command_str)
  response = nil

  callback = Proc.new do |res|
    response = res
  end

  send_command_async(command_str, &callback)

  begin
    timeout(5) do
      while response.nil?
        api.run_loop(0)
        sleep 0.1
      end
    end
  rescue Timeout::Error
  end

  response
end

#skype_running?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/mac-skype/agent.rb', line 31

def skype_running?
  api.running?
end