Class: Kibosh::Request

Inherits:
Object
  • Object
show all
Includes:
Exceptions
Defined in:
lib/kibosh/request.rb

Constant Summary collapse

Session =
Kibosh::Session

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml, router) ⇒ Request

Returns a new instance of Request.



50
51
52
53
54
55
56
# File 'lib/kibosh/request.rb', line 50

def initialize xml, router
  @body = xml.root
  @router = router
  if @body.node_name != "body"
    raise Error.new BadRequest, "root element is #{@body.node_name} not body"
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



48
49
50
# File 'lib/kibosh/request.rb', line 48

def body
  @body
end

Class Method Details

.handle(env, sessions, router) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kibosh/request.rb', line 15

def self.handle env, sessions, router
  request = Rack::Request.new env
  response = Kibosh::Response.new(env['async.callback'], env['async.close'])
  begin
    raise Error.new BadRequest, "HTTP verb is not POST" if !request.post?

    begin
      xml = env["rack.input"].read
    rescue Exception => e
      raise Error.new BadRequest, "Could not fetch request data: " + e
    end 

    begin
      doc = Nokogiri::XML::Document.parse(xml)
    rescue Exception => e
      raise Error.new BadRequest, "Could not parse XML: " + e
    end

    puts "] #{doc.to_xml}"
    
    new(doc,router).handle(response, sessions)
  rescue Error => e
    $stderr.puts e
    $stderr.puts e.backtrace.join("\n")
    e.extend(response)
  rescue Exception => e
    $stderr.puts e
    $stderr.puts e.backtrace.join("\n")
    Error.new(UndefinedCondition, e.to_s).extend(response)
  end
  response.rack
end

Instance Method Details

#[](s) ⇒ Object



70
71
72
# File 'lib/kibosh/request.rb', line 70

def [] s
  @body[s]
end

#driver(session, to, route) ⇒ Object



82
83
84
# File 'lib/kibosh/request.rb', line 82

def driver session, to, route
  @router.driver session, to, route
end

#handle(response, sessions) ⇒ Object



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

def handle response, sessions
  if sid = self["sid"]
    response = (response.session = sessions[sid]).handle(self, response)
  else
    s = session.new self, response do |r|
      response = r
    end
    sessions << s
  end
  response
end

#sessionObject



74
75
76
77
78
79
80
# File 'lib/kibosh/request.rb', line 74

def session
  if @body.attribute_with_ns "version", "urn:xmpp:xbosh"
    Kibosh::XMPP::Session
  else
    Kibosh::Session
  end
end