Class: ActionMessageTexter::Base

Inherits:
AbstractController::Base
  • Object
show all
Includes:
AbstractController::Caching, AbstractController::Callbacks, AbstractController::Logger, AbstractController::Translation, DeliveryMethods, Rescuable
Defined in:
lib/action_message_texter/base.rb

Overview

所有Texter的父類別

Direct Known Subclasses

ApplicationTexter

Defined Under Namespace

Classes: NullMessage

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DeliveryMethods

#wrap_delivery_behavior!

Methods included from Rescuable

#handle_exceptions

Constructor Details

#initializeBase

Returns a new instance of Base.



109
110
111
112
113
# File 'lib/action_message_texter/base.rb', line 109

def initialize
  super()
  @_message_was_called = false
  @_message = Message.new
end

Class Attribute Details

.texter_nameObject



70
71
72
# File 'lib/action_message_texter/base.rb', line 70

def texter_name
  @texter_name ||= anonymous? ? 'anonymous' : name.underscore
end

Class Method Details

.default(value) ⇒ Object Also known as: default_options=

可在 Texter 中更改設定

Parameters:

  • value (Hash)


61
62
63
64
# File 'lib/action_message_texter/base.rb', line 61

def default(value)
  self.default_params = default_params.merge(value).freeze if value
  default_params
end

.deliver_message(message) ⇒ Object

寄送訊息 發出Log Notification



77
78
79
80
81
82
83
84
85
# File 'lib/action_message_texter/base.rb', line 77

def deliver_message(message)
  ActiveSupport::Notifications.instrument('deliver.action_message_texter') do |payload|
    payload[:content] = message.content
    payload[:to] = message.to
    payload[:deliver_at] = message.deliver_at || Time.now
    payload[:texter] = name
    yield # Let Message do the delivery actions
  end
end

.register_interceptor(interceptor) ⇒ Object

註冊攔截器Interceptor

Interceptor 需要包含一個Class Method delivering_message(message) 此Interceptor被註冊後 將於簡訊寄出前觸發delivering_message並取得 message instance

@params [Class|Symbel|String] interceptor


46
47
48
# File 'lib/action_message_texter/base.rb', line 46

def register_interceptor(interceptor)
  Message.register_interceptor(observer_class_for(interceptor))
end

.register_observer(observer) ⇒ Object

註冊觀察者Observer

Observer 需要包含一個Class Method delivered_message(message) 此方法會在訊息傳送後被呼叫 並取得 message instance



29
30
31
# File 'lib/action_message_texter/base.rb', line 29

def register_observer(observer)
  Message.register_observer(observer_class_for(observer))
end

.unregister_interceptor(interceptor) ⇒ Object

移除已被註冊的 Interceptor



53
54
55
# File 'lib/action_message_texter/base.rb', line 53

def unregister_interceptor(interceptor)
  Message.unregister_observer(observer_class_for(interceptor))
end

.unregister_observer(observer) ⇒ Object

移除已被註冊的 Observer



36
37
38
# File 'lib/action_message_texter/base.rb', line 36

def unregister_observer(observer)
  Message.unregister_observer(observer_class_for(observer))
end

Instance Method Details

#process(method_name, *args) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/action_message_texter/base.rb', line 115

def process(method_name, *args)
  payload = {
    messenger: self.class.name,
    action: method_name,
    args: args
  }

  # Notification
  ActiveSupport::Notifications.instrument('process.action_message_texter', payload) do
    super
    @_message = NullMessage.new unless @_message_was_called
  end
end

#text(headers = {}) ⇒ Object

return Message instance



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/action_message_texter/base.rb', line 133

def text(headers = {})
  @_message_was_called = true

  headers = self.class.default_params.merge(headers)

  message.content = render_content(headers)

  message.to = recipient(headers)

  message.deliver_at = headers.delete(:at)

  message.other_options = headers

  wrap_delivery_behavior!(self.class.delivery_method)

  message
end