Class: Agents::Elks46SmsAgent

Inherits:
Agent
  • Object
show all
Defined in:
lib/huginn_elks46_sms_agent/elks46_sms_agent.rb

Instance Method Summary collapse

Instance Method Details

#default_optionsObject

There is a form_configurable for extra options, this can be used instead of the default options. It’s located in app/concerns/form_configurable.rb



24
25
26
27
28
29
30
31
32
# File 'lib/huginn_elks46_sms_agent/elks46_sms_agent.rb', line 24

def default_options
  {
    'api_username' => 'u6xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'api_password' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'from' => 'Huginn',
    'to' => ['+46700000000'],
    'message' => 'Hello this message from your friend Huginn.',
  }
end

#receive(incoming_events) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/huginn_elks46_sms_agent/elks46_sms_agent.rb', line 56

def receive(incoming_events)
  incoming_events.each do |event|
      interpolate_with event do
        send_sms(interpolated)
      end
  end
end

#send_sms(payload) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/huginn_elks46_sms_agent/elks46_sms_agent.rb', line 64

def send_sms(payload)
  payload['to'].each do |to_recipient|
    uri = URI('https://api.46elks.com/a1/sms')
    req = Net::HTTP::Post.new(uri)
    req.basic_auth payload['api_username'], payload['api_password']
    req.set_form_data(
      :from => payload['from'],
      :to => to_recipient,
      :message => payload['message']
    )

    res = Net::HTTP.start(
        uri.host,
        uri.port,
        :use_ssl => uri.scheme == 'https') do |http|
      http.request req
    end

    unless res.is_a?(Net::HTTPSuccess)
      error("Error: #{res.body}")
    end
  end
end

#validate_optionsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/huginn_elks46_sms_agent/elks46_sms_agent.rb', line 34

def validate_options
  unless options['api_username'].present?
    errors.add(:base, '`api_username` is required.')
  end
  unless options['api_password'].present?
    errors.add(:base, '`api_password` is required.')
  end
  unless interpolated['from'].present?
    errors.add(:base, '`from` is required.')
  end
  unless interpolated['to'].present?
    errors.add(:base, '`to` is required.')
  end
  unless interpolated['message'].present?
    errors.add(:base, '`message` is required.')
  end
end

#working?Boolean



52
53
54
# File 'lib/huginn_elks46_sms_agent/elks46_sms_agent.rb', line 52

def working?
  !recent_error_logs?
end