Class: ActionJabber::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/actionjabber.rb

Defined Under Namespace

Classes: Request

Instance Method Summary collapse

Constructor Details

#initialize(username, password, controller) ⇒ Server

Returns a new instance of Server.



29
30
31
32
# File 'lib/actionjabber.rb', line 29

def initialize(username, password, controller)
  @jabber = Jabber::Simple.new(username, password)
  @controller = controller # Should be a class.
end

Instance Method Details

#respond_to(request, opts = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/actionjabber.rb', line 59

def respond_to(request, opts = {})
  from = request.from
  status = opts[:status] or 200
  data = opts[:data] or ''
  resp = "#{request.hash}:#{opts[:status]}"
  unless data.to_s.empty?
    resp += (':' + data.to_s)
  end
  @jabber.deliver(Jabber::JID.new(request.from), resp)
end

#run!Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/actionjabber.rb', line 33

def run!
  while @jabber.connected?
    @jabber.received_messages do |message|
      start = Time.now
      
      from = message.from.to_s.split('/').first
      parts = message.body.strip.split(':', 2)
      hash = parts.first
      path_parts = parts.last.split('?', 2)
      request = Request.new(hash, from, path_parts.first, ((path_parts.length == 2) ? path_parts.last : ''))
      #begin
        #process_message(message.from.to_s.strip, message.body.strip)
        response = {:status => 200, :data => ''}.merge @controller.route!(request)
        #response = @controller.route! request
        respond_to request, :status => response[:status], :data => response[:data]
      #rescue
      #  respond_to request, :status => 500
      #  puts "Error responding to #{message.from.to_s.strip}:"
      #  puts $!
      #else
      #  puts "Responded to '#{from}' in #{(Time.now - start).to_s} seconds."
      #end
      puts "\n"
    end
  end
end