Sha256: 186840e8135787d52b8015cdbb4fd9b2e30c89e361fa7a66c10402b66d9ead6b

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 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 = []

      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 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

4 entries across 4 versions & 1 rubygems

Version Path
lifen-0.2.0 lib/lifen/flow.rb
lifen-0.1.5 lib/lifen/flow.rb
lifen-0.1.4 lib/lifen/flow.rb
lifen-0.1.3 lib/lifen/flow.rb