Class: BotMob::InboundMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/bot_mob/inbound_message.rb

Overview

# BotMob::InboundMessage

Structured data provided to a bot

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ InboundMessage

Returns a new instance of InboundMessage.



8
9
10
11
12
13
14
15
# 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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/bot_mob/inbound_message.rb', line 6

def body
  @body
end

#networkObject (readonly)

Returns the value of attribute network.



6
7
8
# File 'lib/bot_mob/inbound_message.rb', line 6

def network
  @network
end

#user_nameObject (readonly)

Returns the value of attribute user_name.



6
7
8
# File 'lib/bot_mob/inbound_message.rb', line 6

def user_name
  @user_name
end

Class Method Details

.default_networkObject



68
69
70
# File 'lib/bot_mob/inbound_message.rb', line 68

def default_network
  :roaming
end

.default_user_nameObject



64
65
66
# File 'lib/bot_mob/inbound_message.rb', line 64

def default_user_name
  ENV['MOB_NAME'] || 'nomad'
end

.message_attributesObject



60
61
62
# File 'lib/bot_mob/inbound_message.rb', line 60

def message_attributes
  @message_attrs || []
end

.message_attrs(*attrs) ⇒ Object



55
56
57
58
# File 'lib/bot_mob/inbound_message.rb', line 55

def message_attrs(*attrs)
  @message_attrs = attrs
  attr_reader(*@message_attrs)
end

.prepare(message, options = {}) ⇒ Object

## 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



26
27
28
29
30
31
32
33
# 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

Instance Method Details

#human?Boolean

## human?

This should be overrode in the inheriting bot class. Use this to determine if the inbound message was written by a human

Returns:

  • (Boolean)


39
40
41
# File 'lib/bot_mob/inbound_message.rb', line 39

def human?
  ENV['MOB_NAME'] != user_name
end

#paramsObject

## params

A representation of the relevant data attached to a bot



46
47
48
49
50
51
52
# File 'lib/bot_mob/inbound_message.rb', line 46

def params
  @params ||= {
    body: body,
    network: network,
    user_name: user_name
  }
end