Sha256: 43ab5211c3ae82f8e724b0522c6bc728bd80a9e9a41a24c689f53ec5d63b0672

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

module Lifen
  class Flow < Base

    attribute :uuid, String
    attribute :title, String
    attribute :user, Lifen::User
    attribute :active_users, Array[Lifen::User]

    def create
      json = client.post("central/api/chats", {title: title})

      flow = self.class.new(json.first)

      self.user = user
      self.uuid = flow.uuid
      self.active_users = []
      self.title = flow.title

      self
    end

    def attach_users!(users)
      users = Array(users)

      params = users.map(&:uuid).compact.uniq

      json = client.post("central/api/chats/#{uuid}/attach_users?rel=activeUsers", params)

      Array(json["activeUsers"]).each do |element|
        self.active_users << Lifen::User.new(element)
      end

      users.each do |user|
        raise Lifen::Error, "User #{user.uuid} was not attached to this flow" if !active_users.map(&:uuid).include? user.uuid
      end

      self
    end

    def detach_users!(users)
      users = Array(users)

      params = users.map(&:uuid).compact.uniq

      json = client.post("central/api/chats/#{uuid}/detach_users?rel=activeUsers", params)

      self.active_users = []

      json["activeUsers"].each do |element|
        self.active_users << Lifen::User.new(element)
      end

      users.each do |user|
        raise Lifen::Error, "User #{user.uuid} was not detached to this flow" if active_users.map(&:uuid).include? user.uuid
      end

      self
    end

    private

      def client
        @client ||= user.client
      end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lifen-0.1.2 lib/lifen/flow.rb