Class: ChatSDK::Chat

Inherits:
Object
  • Object
show all
Defined in:
lib/chat_sdk/chat.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_name:, adapters:, state:, **options) ⇒ Chat

Returns a new instance of Chat.



7
8
9
10
11
12
13
14
15
16
# File 'lib/chat_sdk/chat.rb', line 7

def initialize(user_name:, adapters:, state:, **options)
  @config = Config.new(user_name: user_name, adapters: adapters, state: state, **options)
  @state = state
  @adapters = adapters
  @registry = EventRegistry.new
  @webhooks = {}
  @dispatcher = Dispatcher.new(chat: self, config: @config, state: @state, registry: @registry)

  ChatSDK::Log.level = @config.log_level
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/chat_sdk/chat.rb', line 5

def config
  @config
end

#stateObject (readonly)

Returns the value of attribute state.



5
6
7
# File 'lib/chat_sdk/chat.rb', line 5

def state
  @state
end

Instance Method Details

#adapter(name) ⇒ Object

Adapter access



48
49
50
# File 'lib/chat_sdk/chat.rb', line 48

def adapter(name)
  @adapters.fetch(name) { raise ChatSDK::ConfigurationError, "Unknown adapter: #{name}" }
end

#channel(id, adapter_name: nil) ⇒ Object

Channel/DM access



53
54
55
56
# File 'lib/chat_sdk/chat.rb', line 53

def channel(id, adapter_name: nil)
  adp = adapter_name ? adapter(adapter_name) : @adapters.values.first
  Channel.new(id: id, adapter: adp, chat: self)
end

#dispatch(event, adapter_name:) ⇒ Object

Event dispatch (called by webhook endpoints)



70
71
72
73
# File 'lib/chat_sdk/chat.rb', line 70

def dispatch(event, adapter_name:)
  adp = adapter(adapter_name)
  @dispatcher.dispatch(event, adapter: adp, adapter_name: adapter_name)
end

#on_action(action_id, &block) ⇒ Object



39
40
41
# File 'lib/chat_sdk/chat.rb', line 39

def on_action(action_id, &block)
  @registry.register(:action, matcher: action_id, &block)
end

#on_direct_message(&block) ⇒ Object



31
32
33
# File 'lib/chat_sdk/chat.rb', line 31

def on_direct_message(&block)
  @registry.register(:direct_message, &block)
end

#on_new_mention(&block) ⇒ Object

Event registration



19
20
21
# File 'lib/chat_sdk/chat.rb', line 19

def on_new_mention(&block)
  @registry.register(:mention, &block)
end

#on_new_message(pattern = nil, &block) ⇒ Object



27
28
29
# File 'lib/chat_sdk/chat.rb', line 27

def on_new_message(pattern = nil, &block)
  @registry.register(:mention, matcher: pattern, &block)
end

#on_reaction(emojis = nil, &block) ⇒ Object



35
36
37
# File 'lib/chat_sdk/chat.rb', line 35

def on_reaction(emojis = nil, &block)
  @registry.register(:reaction, matcher: emojis, &block)
end

#on_slash_command(command, &block) ⇒ Object



43
44
45
# File 'lib/chat_sdk/chat.rb', line 43

def on_slash_command(command, &block)
  @registry.register(:slash_command, matcher: command, &block)
end

#on_subscribed_message(&block) ⇒ Object



23
24
25
# File 'lib/chat_sdk/chat.rb', line 23

def on_subscribed_message(&block)
  @registry.register(:subscribed_message, &block)
end

#open_dm(user_id, adapter_name: nil) ⇒ Object



58
59
60
61
62
# File 'lib/chat_sdk/chat.rb', line 58

def open_dm(user_id, adapter_name: nil)
  adp = adapter_name ? adapter(adapter_name) : @adapters.values.first
  channel_id = adp.open_dm(user_id)
  Channel.new(id: channel_id, adapter: adp, chat: self)
end

#webhooksObject

Webhook endpoints (Rack apps)



65
66
67
# File 'lib/chat_sdk/chat.rb', line 65

def webhooks
  @webhook_accessor ||= WebhookAccessor.new(self, @adapters)
end