class BotFramework::Server

Public Class Methods

call(env) click to toggle source
# File lib/bot_framework/server.rb, line 3
def self.call(env)
  new.call(env)
end

Public Instance Methods

call(env) click to toggle source
# File lib/bot_framework/server.rb, line 7
def call(env)
  @request = Rack::Request.new env
  @response = Rack::Response.new
  if @request.post?
    if verify
      receive
    else
      raise InvalidToken
    end
  end
  @response.finish
end
headers() click to toggle source
# File lib/bot_framework/server.rb, line 28
def headers
  env = @request.env
  Hash[*env.select { |k, _v| k.start_with? 'HTTP_' }
           .collect { |k, v| [k.sub(/^HTTP_/, ''), v] }
           .collect { |k, v| [k.split('_').collect(&:capitalize).join('-'), v] }
       .sort
       .flatten]
end
receive() click to toggle source

TODO: reply in separate thread t avoid timeout

# File lib/bot_framework/server.rb, line 21
def receive
  # Thread.new {
  activity = Activity.new.build_from_hash JSON.parse(@request.body.read)
  Bot.receive(activity)
  # }
end
verify() click to toggle source

Use logger instead of puts

# File lib/bot_framework/server.rb, line 38
def verify
  validator = TokenValidator.new(headers)
  if validator.valid?
    return true
  else
    BotFramework.logger.error "Errors: #{validator.errors}"
    return false
  end
rescue JWT::DecodeError
  [401, { 'Content-Type' => 'text/plain' }, ['A token must be passed.']]
rescue JWT::ExpiredSignature
  [403, { 'Content-Type' => 'text/plain' }, ['The token has expired.']]
rescue JWT::InvalidIssuerError
  [403, { 'Content-Type' => 'text/plain' }, ['The token does not have a valid issuer.']]
rescue JWT::InvalidIatError
  [403, { 'Content-Type' => 'text/plain' }, ['The token does not have a valid "issued at" time.']]
end