lib/lifen/flow.rb in lifen-0.2.1 vs lib/lifen/flow.rb in lifen-1.0.0
- old
+ new
@@ -2,91 +2,88 @@
class Flow < Base
attribute :uuid, String
attribute :title, String
attribute :user, Lifen::User
- attribute :active_users, Array[Lifen::User]
+ attribute :users, Array[Lifen::User]
def create
- params = {title: title}
+ users_to_attach = users
- users_to_attach = []
- if !active_users.empty?
- users_to_attach = active_users
- params[:users] = active_users.map(&:uuid)
- end
+ params = {title: title, users: extract_users_uuids(users_to_attach)}
json = client.post("central/api/chats?rel=activeUsers", params)
json_flow = json.first
flow = self.class.new(json_flow)
self.user = user
self.uuid = flow.uuid
+ self.title = flow.title
- self.active_users = []
- Array(json_flow["activeUsers"]).each do |element|
- element[:first_name] = element["firstName"]
- element[:last_name] = element["lastName"]
+ build_users(json_flow)
- self.active_users << Lifen::User.new(element)
- end
+ check_if_users_were_attached!(users_to_attach)
- users_to_attach.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.title = flow.title
-
self
end
- def attach_users!(users)
- users = Array(users)
+ def attach_users(users_to_attach)
+ params = extract_users_uuids(users_to_attach)
- params = users.map(&:uuid).compact.uniq
-
json = client.post("central/api/chats/#{uuid}/attach_users?rel=activeUsers", params)
- Array(json["activeUsers"]).each do |element|
- element[:first_name] = element["firstName"]
- element[:last_name] = element["lastName"]
+ build_users(json)
- self.active_users << Lifen::User.new(element)
- end
+ check_if_users_were_attached!(users_to_attach)
- 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)
+ def detach_users(users_to_detach)
+ params = extract_users_uuids(users_to_detach)
- params = users.map(&:uuid).compact.uniq
-
json = client.post("central/api/chats/#{uuid}/detach_users?rel=activeUsers", params)
- self.active_users = []
+ build_users(json)
- Array(json["activeUsers"]).each do |element|
- self.active_users << Lifen::User.new(element)
- end
+ check_if_users_were_detached!(users_to_detach)
- 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
+
+ def build_users(json)
+ self.users = []
+
+ Array(json["activeUsers"]).each do |element|
+ element[:first_name] = element["firstName"]
+ element[:last_name] = element["lastName"]
+
+ self.users << Lifen::User.new(element)
+ end
+ end
+
+ def extract_users_uuids(users)
+ Array(users).map(&:uuid).compact.uniq
+ end
+
+ def check_if_users_were_attached!(users_to_attach)
+ missing_users = Array(users_to_attach).map(&:uuid) - users.map(&:uuid)
+
+ raise Lifen::Error, "Users #{missing_users.join(', ')} were not attached to this flow" if !missing_users.empty?
+ end
+
+ def check_if_users_were_detached!(users_to_detach)
+ extra_users = users.map(&:uuid) & Array(users_to_detach).map(&:uuid)
+
+ raise Lifen::Error, "Users #{extra_users.join(', ')} were not detached from this flow" if !extra_users.empty?
end
end
end
\ No newline at end of file