Class: Ruboty::SlackTakeTurns::Actions::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ruboty/slack_take_turns/actions/base.rb

Direct Known Subclasses

Assign, Current, Exclude, Force, Include, Members, Next

Defined Under Namespace

Classes: ActionBaseError, CurrentUserNotFound, UserNotFound

Constant Summary collapse

NAMESPACE =
'slack_take_turns'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ Base

Returns a new instance of Base.



9
10
11
# File 'lib/ruboty/slack_take_turns/actions/base.rb', line 9

def initialize(message)
  @message = message
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



7
8
9
# File 'lib/ruboty/slack_take_turns/actions/base.rb', line 7

def message
  @message
end

Instance Method Details

#channelObject



13
14
15
# File 'lib/ruboty/slack_take_turns/actions/base.rb', line 13

def channel
  message.to
end

#channel_dataObject



25
26
27
# File 'lib/ruboty/slack_take_turns/actions/base.rb', line 25

def channel_data
  data[channel] ||= {}
end

#current_user_idObject



29
30
31
# File 'lib/ruboty/slack_take_turns/actions/base.rb', line 29

def current_user_id
  channel_data[:current_user_id]
end

#current_user_id=(id) ⇒ Object



33
34
35
# File 'lib/ruboty/slack_take_turns/actions/base.rb', line 33

def current_user_id=(id)
  channel_data[:current_user_id] = id
end

#current_user_nameObject



49
50
51
52
53
# File 'lib/ruboty/slack_take_turns/actions/base.rb', line 49

def current_user_name
  find_user_by_user_id(current_user_id)['name']
rescue
  raise CurrentUserNotFound.new(chat_message: message)
end

#dataObject



21
22
23
# File 'lib/ruboty/slack_take_turns/actions/base.rb', line 21

def data
  message.robot.brain.data[NAMESPACE] ||= {}
end

#excluded_user_idsObject



45
46
47
# File 'lib/ruboty/slack_take_turns/actions/base.rb', line 45

def excluded_user_ids
  channel_data[:excluded_user_ids] ||= []
end

#find_user_by_user_id(user_id) ⇒ Object



55
56
57
# File 'lib/ruboty/slack_take_turns/actions/base.rb', line 55

def find_user_by_user_id(user_id)
  slack_client.all_users_hash[user_id] || raise(UserNotFound.new(user_id: user_id, chat_message: message))
end

#find_user_id_by_user_name(user_name) ⇒ Object



59
60
61
62
# File 'lib/ruboty/slack_take_turns/actions/base.rb', line 59

def find_user_id_by_user_name(user_name)
  user = slack_client.all_users.find{|u| u['name'] == user_name}
  user ? user['id'] : raise(UserNotFound.new(user_name: user_name, chat_message: message))
end

#sender_idObject



17
18
19
# File 'lib/ruboty/slack_take_turns/actions/base.rb', line 17

def sender_id
  message.from
end

#slack_clientObject



37
38
39
# File 'lib/ruboty/slack_take_turns/actions/base.rb', line 37

def slack_client
  @slack_client ||= Ruboty::SlackTakeTurns::SlackClient.new(channel)
end

#target_user_idsObject



41
42
43
# File 'lib/ruboty/slack_take_turns/actions/base.rb', line 41

def target_user_ids
  slack_client.channel_user_ids - excluded_user_ids
end