Sha256: c12164783de2e6c333708e6d192515eaa45b06262d97521da45061f635b32e9f

Contents?: true

Size: 1.48 KB

Versions: 2

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)

      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

2 entries across 2 versions & 1 rubygems

Version Path
lifen-0.1.1 lib/lifen/flow.rb
lifen-0.1.0 lib/lifen/flow.rb