Sha256: 221dfe65e457760fb27dfe219ec71aeb09640f001acbcfa16aeddf42cd176e52

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require 'multi_json'

module Lita
  module Adapters
    class Glip < Adapter
      class MessageHandler
        attr_reader :robot

        def initialize(robot, glip_sdk)
          @robot = robot
          @glip_sdk = glip_sdk
          @logger_prefix = " -- #{self.class.name}: "
        end

        def update(message)
          m = message

          Lita.logger.debug("#{@logger_prefix}Glip Lita: Glip Message Received: #{MultiJson.encode(m)}")

          unless m.is_a?(Hash) && m.key?('event') && m['event'].index('/glip/posts').is_a?(Integer)
            return
          end

          unless m.key?('body') && m['body'].key?('messageType') && m['body']['messageType'] == "PostAdded"
            return
          end

          user_id = message['body']['post']['creatorId']
          user = UserCreator.find_or_create_user user_id, @glip_sdk

          room_id = message['body']['post']['groupId']
          room = RoomCreator.find_or_create_room room_id, @glip_sdk

          source = Lita::Source.new user: user, room: room
          post = message['body']['post']['text'].to_s
          msg = Lita::Message.new @robot, post, source

          Lita.logger.debug("#{@logger_prefix}Glip Lita: Robot Message Built")
          @robot.receive msg
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lita-glip-0.0.4 lib/lita/adapters/glip/message_handler.rb