Class: SlackBot::Callback

Inherits:
Object
  • Object
show all
Defined in:
lib/slack_bot/callback.rb

Constant Summary collapse

CALLBACK_KEY_PREFIX =
"slack-bot-callback".freeze
CALLBACK_RECORD_EXPIRES_IN =
15.minutes.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, class_name: nil, user: nil, channel_id: nil, payload: nil, config: nil, expires_in: nil, user_scope: nil, view_id: nil) ⇒ Callback

Returns a new instance of Callback.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/slack_bot/callback.rb', line 41

def initialize(id: nil, class_name: nil, user: nil, channel_id: nil, payload: nil, config: nil, expires_in: nil, user_scope: nil, view_id: nil)
  @id = id
  @data = {
    class_name: class_name,
    user_id: user&.id,
    channel_id: channel_id,
    view_id: view_id,
    payload: payload
  }
  @args = SlackBot::Args.new
  @config = config || SlackBot::Config.current_instance
  @expires_in = expires_in || CALLBACK_RECORD_EXPIRES_IN
  @user_scope = user_scope.nil? ? true : user_scope
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



134
135
136
137
138
139
# File 'lib/slack_bot/callback.rb', line 134

def method_missing(method_name, *args, &block)
  return data[method_name.to_sym] if data.key?(method_name.to_sym)
  return data[:payload][method_name.to_s] if data[:payload].is_a?(Hash) && data[:payload].key?(method_name.to_s)

  super
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



40
41
42
# File 'lib/slack_bot/callback.rb', line 40

def args
  @args
end

#configObject (readonly)

Returns the value of attribute config.



40
41
42
# File 'lib/slack_bot/callback.rb', line 40

def config
  @config
end

#dataObject (readonly)

Returns the value of attribute data.



40
41
42
# File 'lib/slack_bot/callback.rb', line 40

def data
  @data
end

#expires_inObject (readonly)

Returns the value of attribute expires_in.



40
41
42
# File 'lib/slack_bot/callback.rb', line 40

def expires_in
  @expires_in
end

#idObject (readonly)

Returns the value of attribute id.



40
41
42
# File 'lib/slack_bot/callback.rb', line 40

def id
  @id
end

#user_scopeObject (readonly)

Returns the value of attribute user_scope.



40
41
42
# File 'lib/slack_bot/callback.rb', line 40

def user_scope
  @user_scope
end

Class Method Details

.create(class_name:, user:, id: nil, channel_id: nil, config: nil, payload: nil, expires_in: nil, user_scope: nil) ⇒ Object



26
27
28
29
30
31
# File 'lib/slack_bot/callback.rb', line 26

def self.create(class_name:, user:, id: nil, channel_id: nil, config: nil, payload: nil, expires_in: nil, user_scope: nil)
  callback =
    new(id: id, class_name: class_name, user: user, channel_id: channel_id, payload: payload, config: config, expires_in: expires_in, user_scope: user_scope)
  callback.save
  callback
end

.find(id, user: nil, config: nil) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/slack_bot/callback.rb', line 9

def self.find(id, user: nil, config: nil)
  return if id.blank?

  callback = new(id: id, user: user, config: config)
  callback.reload
rescue SlackBot::Errors::CallbackNotFound
  nil
end

.find_by_view_id(view_id, user: nil, config: nil) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/slack_bot/callback.rb', line 18

def self.find_by_view_id(view_id, user: nil, config: nil)
  callback = new(view_id: view_id, user: user, config: config)
  callback_id = callback.read_view_callback_id
  return if callback_id.blank?

  find(callback_id, user: user, config: config)
end

.find_or_create(id:, class_name:, user:, channel_id: nil, config: nil, payload: nil, expires_in: nil, user_scope: nil) ⇒ Object



33
34
35
36
37
38
# File 'lib/slack_bot/callback.rb', line 33

def self.find_or_create(id:, class_name:, user:, channel_id: nil, config: nil, payload: nil, expires_in: nil, user_scope: nil)
  callback = find(id, user: user, config: config)
  return callback if callback.present?

  create(id: id, class_name: class_name, user: user, channel_id: channel_id, payload: payload, config: config, expires_in: expires_in, user_scope: user_scope)
end

Instance Method Details

#channel_id=(channel_id) ⇒ Object



109
110
111
# File 'lib/slack_bot/callback.rb', line 109

def channel_id=(channel_id)
  @data[:channel_id] = channel_id
end

#class_name=(class_name) ⇒ Object



105
106
107
# File 'lib/slack_bot/callback.rb', line 105

def class_name=(class_name)
  @data[:class_name] = class_name
end

#destroyObject



87
88
89
90
91
# File 'lib/slack_bot/callback.rb', line 87

def destroy
  return if id.blank?

  delete_data
end

#handler_classObject



128
129
130
131
132
# File 'lib/slack_bot/callback.rb', line 128

def handler_class
  return if class_name.blank?

  config.find_handler_class(class_name)
end

#handler_class=(handler_class) ⇒ Object



121
122
123
124
125
126
# File 'lib/slack_bot/callback.rb', line 121

def handler_class=(handler_class)
  new_class_name = handler_class&.name
  config.find_handler_class(class_name)

  self.class_name = new_class_name
end

#payload=(payload) ⇒ Object



113
114
115
# File 'lib/slack_bot/callback.rb', line 113

def payload=(payload)
  @data[:payload] = payload
end

#read_view_callback_idObject



141
142
143
144
145
# File 'lib/slack_bot/callback.rb', line 141

def read_view_callback_id
  return if view_id.blank?

  config.callback_storage_instance.read(view_storage_key)
end

#reloadObject



56
57
58
59
60
61
62
63
64
# File 'lib/slack_bot/callback.rb', line 56

def reload
  cached_data = read_data
  SlackBot::DevConsole.log_check("SlackBot::Callback#read_data: #{id} | #{cached_data}")
  raise SlackBot::Errors::CallbackNotFound if cached_data.nil?

  @data = cached_data
  parse_args
  self
end

#saveObject



66
67
68
69
70
71
72
# File 'lib/slack_bot/callback.rb', line 66

def save
  @id = generate_id if id.blank?
  serialize_args

  SlackBot::DevConsole.log_check("SlackBot::Callback#write_data: #{id} | #{data}")
  write_data(data)
end

#update(payload) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/slack_bot/callback.rb', line 74

def update(payload)
  return if id.blank?
  return if data.blank?

  @data[:payload] = if @data[:payload].is_a?(Hash)
    @data[:payload].merge(payload)
  else
    payload
  end

  save
end

#userObject



93
94
95
96
97
98
# File 'lib/slack_bot/callback.rb', line 93

def user
  @user ||= begin
    user_id = data&.dig(:user_id)
    config.callback_user_finder_method.call(user_id) if user_id.present?
  end
end

#user=(user) ⇒ Object



100
101
102
103
# File 'lib/slack_bot/callback.rb', line 100

def user=(user)
  @user = user
  @data[:user_id] = user&.id
end

#view_id=(view_id) ⇒ Object



117
118
119
# File 'lib/slack_bot/callback.rb', line 117

def view_id=(view_id)
  @data[:view_id] = view_id
end