Made Pushbullet agent form configurable and validate additional fields

Dominik Sander 9 years ago
parent
commit
eee268a920
2 changed files with 22 additions and 2 deletions
  1. 16 2
      app/models/agents/pushbullet_agent.rb
  2. 6 0
      spec/models/agents/pushbullet_agent_spec.rb

+ 16 - 2
app/models/agents/pushbullet_agent.rb

@@ -1,5 +1,7 @@
1 1
 module Agents
2 2
   class PushbulletAgent < Agent
3
+    include FormConfigurable
4
+
3 5
     cannot_be_scheduled!
4 6
     cannot_create_events!
5 7
 
@@ -48,10 +50,22 @@ module Agents
48 50
       }
49 51
     end
50 52
 
53
+    form_configurable :api_key
54
+    form_configurable :device_id
55
+    form_configurable :type, type: :array, values: ['note', 'link', 'address']
56
+    form_configurable :title
57
+    form_configurable :body, type: :text
58
+    form_configurable :url
59
+    form_configurable :name
60
+    form_configurable :address
61
+
51 62
     def validate_options
52 63
       errors.add(:base, "you need to specify a pushbullet api_key") if options['api_key'].blank?
53 64
       errors.add(:base, "you need to specify a device_id") if options['device_id'].blank?
54 65
       errors.add(:base, "you need to specify a valid message type") if options['type'].blank? or not ['note', 'link', 'address'].include?(options['type'])
66
+      TYPE_TO_ATTRIBUTES[options['type']].each do |attr|
67
+        errors.add(:base, "you need to specify '#{attr.to_s}' for the type '#{options['type']}'") if options[attr].blank?
68
+      end
55 69
     end
56 70
 
57 71
     def working?
@@ -69,8 +83,8 @@ module Agents
69 83
     def query_options(event)
70 84
       mo = interpolated(event)
71 85
       {
72
-        :basic_auth => {username: mo[:api_key], password: ''},
73
-        :body => {device_iden: mo[:device_id], type: mo[:type]}.merge(payload(mo))
86
+        basic_auth: {username: mo[:api_key], password: ''},
87
+        body: {device_iden: mo[:device_id], type: mo[:type]}.merge(payload(mo))
74 88
       }
75 89
     end
76 90
 

+ 6 - 0
spec/models/agents/pushbullet_agent_spec.rb

@@ -37,6 +37,12 @@ describe Agents::PushbulletAgent do
37 37
       @checker.options['device_id'] = nil
38 38
       expect(@checker).not_to be_valid
39 39
     end
40
+
41
+    it "should require fields based on the type" do
42
+      @checker.options['type'] = 'address'
43
+      @checker.options['address'] = nil
44
+      expect(@checker).not_to be_valid
45
+    end
40 46
   end
41 47
 
42 48
   describe "helpers" do