Class: Soracom::EventHandler

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/soracom/event_handler.rb

Overview

EventHandlerクラス 初期化方法1. 新規に作成 初期化方法2. 特定ハンドラのIDを指定して初期化(handlerId:HANDLERID) → APIで現時点の内容を取得 初期化方法3. hashを指定して初期化(list_event_handlersの戻り値を利用など)

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}, client = Soracom::Client.new) ⇒ EventHandler

Returns a new instance of EventHandler.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/soracom/event_handler.rb', line 7

def initialize(hash = {}, client = Soracom::Client.new)
  @log = Logger.new(STDERR)
  @log.level = ENV['SORACOM_DEBUG'] ? Logger::DEBUG : Logger::WARN
  if hash.keys.length == 0 # 新規作成
    hash = Hash[%w(handlerId targetImsi targetOperatorId targetTag name description ruleConfig actionConfigList).map { |k| [k, nil] }]
  end
  if hash.keys.length == 1 && hash.keys[0] == 'handlerId' # IDのみを指定して作成
    hash = client.list_event_handlers(handler_id: hash['handlerId'])[0]
  end
  super(hash)
  @client = client
end

Instance Method Details

#saveObject

現在の内容でEventHandlerを作成または更新



26
27
28
29
30
31
32
33
34
35
# File 'lib/soracom/event_handler.rb', line 26

def save
  result = validate
  fail result unless result == true
  @log.debug(to_json)
  if handlerId
    @client.update_event_handler(handlerId, to_json)
  else
    @client.create_event_handler(to_json)
  end
end

#to_jsonObject

新規作成または更新用のpayloadをJSON形式で取得



21
22
23
# File 'lib/soracom/event_handler.rb', line 21

def to_json
  to_h.select { |k, v| true if !v.nil? && k != :handlerId }.to_json
end

#validateObject



37
38
39
40
41
42
# File 'lib/soracom/event_handler.rb', line 37

def validate
  return 'No target is defined' if [targetImsi, targetOperatorId, targetTag].compact.length == 0
  return 'No rule is defined' if ruleConfig.nil? || ruleConfig.length == 0
  return 'No action is defined' if actionConfigList.nil? || actionConfigList.length == 0
  true
end