PushbulletAgent code refactoring

Dominik Sander 9 years ago
parent
commit
b8e12e2413
1 changed files with 11 additions and 16 deletions
  1. 11 16
      app/models/agents/pushbullet_agent.rb

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

@@ -4,6 +4,11 @@ module Agents
4 4
     cannot_create_events!
5 5
 
6 6
     API_URL = 'https://api.pushbullet.com/v2/pushes'
7
+    TYPE_TO_ATTRIBUTES = {
8
+            'note'    => [:title, :body],
9
+            'link'    => [:title, :body, :url],
10
+            'address' => [:name, :address]
11
+    }
7 12
 
8 13
     description <<-MD
9 14
       The Pushbullet agent sends pushes to a pushbullet device
@@ -61,26 +66,16 @@ module Agents
61 66
     end
62 67
 
63 68
     private
64
-
65 69
     def query_options(event)
66 70
       mo = interpolated(event)
67
-      body = {device_iden: mo[:device_id], type: mo[:type]}
68
-      case mo[:type]
69
-      when "note"
70
-        body[:title] = mo[:title]
71
-        body[:body] = mo[:body]
72
-      when "link"
73
-        body[:title] = mo[:title]
74
-        body[:body] = mo[:body]
75
-        body[:url] = mo[:url]
76
-      when "address"
77
-        body[:name] = mo[:name]
78
-        body[:address] = mo[:address]
79
-      end
80 71
       {
81
-        :basic_auth => {:username => mo[:api_key], :password => ''},
82
-        :body => body
72
+        :basic_auth => {username: mo[:api_key], password: ''},
73
+        :body => {device_iden: mo[:device_id], type: mo[:type]}.merge(payload(mo))
83 74
       }
84 75
     end
76
+
77
+    def payload(mo)
78
+      Hash[TYPE_TO_ATTRIBUTES[mo[:type]].map { |k| [k, mo[k]] }]
79
+    end
85 80
   end
86 81
 end