Class: Hahamut::Handler

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

Overview

Handling HTTP Request

Constant Summary collapse

SIGNATURE_HEADER =
'HTTP_X_BAHA_DATA_SIGNATURE'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Handler

Returns a new instance of Handler.



13
14
15
# File 'lib/hahamut/handler.rb', line 13

def initialize(env)
  @request = Rack::Request.new(env)
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



11
12
13
# File 'lib/hahamut/handler.rb', line 11

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



11
12
13
# File 'lib/hahamut/handler.rb', line 11

def response
  @response
end

Instance Method Details

#bodyObject



26
27
28
# File 'lib/hahamut/handler.rb', line 26

def body
  @body ||= JSON.parse(raw)
end

#clientObject



30
31
32
# File 'lib/hahamut/handler.rb', line 30

def client
  Manager.find(body['botid'])
end

#processObject



17
18
19
20
21
22
23
24
# File 'lib/hahamut/handler.rb', line 17

def process
  return invalid_client if client.nil?
  return invalid_signature unless valid_signature?

  Manager.handle(client, Event.new(body))

  Rack::Response.new([], 204)
end

#rawObject



34
35
36
37
38
39
40
# File 'lib/hahamut/handler.rb', line 34

def raw
  return @raw unless @raw.nil?

  @raw = @request.body.read
  @request.body.rewind
  @raw
end