Sha256: 8e0fb71bfd5623832dd0492100772c66803635b55f6750a4e06f6eb763c78102

Contents?: true

Size: 901 Bytes

Versions: 1

Compression:

Stored size: 901 Bytes

Contents

# frozen_string_literal: true

module Talkbird
  module Entity
    # SendBird message
    class Message

      attr_reader :sender
      attr_reader :receiver
      attr_reader :body

      DEFAULTS = {
        type: 'MESG'
      }.freeze

      class << self

        def build(response)
          response
        end

      end

      def initialize(from, to, body, options = {})
        @sender = User.find_or_create(from)
        @receiver = User.find_or_create(to)
        @body = body

        @options = options
      end

      def deliver
        return false if !sender || !receiver

        channel = Entity::Channel.find_or_create(sender.id, receiver.id)
        channel.update(self)
      end

      def to_h
        options.merge(
          user_id: sender.id,
          message: body
        )
      end

      def options
        DEFAULTS.merge(@options)
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
talkbird-0.0.5 lib/talkbird/entity/message.rb