class BotMob::InboundMessage
Structured data provided to a bot
Attributes
body[R]
network[R]
user_name[R]
Public Class Methods
default_network()
click to toggle source
# File lib/bot_mob/inbound_message.rb, line 68 def default_network :roaming end
default_user_name()
click to toggle source
# File lib/bot_mob/inbound_message.rb, line 64 def default_user_name ENV['MOB_NAME'] || 'nomad' end
message_attributes()
click to toggle source
# File lib/bot_mob/inbound_message.rb, line 60 def message_attributes @message_attrs || [] end
message_attrs(*attrs)
click to toggle source
# File lib/bot_mob/inbound_message.rb, line 55 def message_attrs(*attrs) @message_attrs = attrs attr_reader(*@message_attrs) end
new(**options)
click to toggle source
# File lib/bot_mob/inbound_message.rb, line 8 def initialize(**options) @body ||= options[:body] @network ||= options[:network] provided_message_attributes(options).each do |attr| instance_variable_set("@#{attr}".to_sym, options[attr.to_sym]) end end
prepare(message, options = {})
click to toggle source
## `prepare`
Prepare accepts three different data types and attempts to coerce it into a contextual inbound message. In the case of an inbound message, it simply assumes the provided message is already correct. In the case of a hash, it passes the hash along as the `initialize` attrs. In the case of a string, it overrides the `body` key of the provided options, then passes the options provided to `prepare` as the `initialize` params
# File lib/bot_mob/inbound_message.rb, line 26 def self.prepare(message, options = {}) return message if message.is_a?(BotMob::InboundMessage) options.symbolize_keys! approach = "prepare_#{message.class.to_s.downcase}_attrs".to_sym attrs = send(approach, message, options) message_delegate(attrs[:network]).new(attrs) end
Private Class Methods
message_delegate(network)
click to toggle source
# File lib/bot_mob/inbound_message.rb, line 90 def message_delegate(network) delegate = BotMob::Networks.const_get(network.to_s.camelize) delegate.const_get('InboundMessage') rescue NameError raise BotMob::InvalidNetworkError end
prepare_hash_attrs(message, options)
click to toggle source
# File lib/bot_mob/inbound_message.rb, line 74 def prepare_hash_attrs(message, options) attrs = message.symbolize_keys.merge(options) attrs.merge( network: attrs[:network] || default_network, user_name: attrs[:user_name] || default_user_name ) end
prepare_string_attrs(message, options)
click to toggle source
# File lib/bot_mob/inbound_message.rb, line 82 def prepare_string_attrs(message, options) options.merge( network: options[:network] || default_network, user_name: options[:user_name] || default_user_name, body: message ) end
Public Instance Methods
human?()
click to toggle source
## `human?`
This should be overrode in the inheriting bot class. Use this to determine if the inbound message was written by a human
# File lib/bot_mob/inbound_message.rb, line 39 def human? ENV['MOB_NAME'] != user_name end
params()
click to toggle source
## `params`
A representation of the relevant data attached to a bot
# File lib/bot_mob/inbound_message.rb, line 46 def params @params ||= { body: body, network: network, user_name: user_name } end
Private Instance Methods
provided_message_attributes(options)
click to toggle source
# File lib/bot_mob/inbound_message.rb, line 100 def provided_message_attributes(options) self.class.message_attributes.select { |attr| options[attr.to_sym] } end