Merge pull request #1058 from TildeWill/weather_agent_spec

Prevent confusion between Huginn services and strings

Andrew Cantino 8 years ago
parent
commit
b61ee2b4e2
2 changed files with 41 additions and 13 deletions
  1. 13 13
      app/models/agents/weather_agent.rb
  2. 28 0
      spec/models/agents/weather_agent_spec.rb

+ 13 - 13
app/models/agents/weather_agent.rb

@@ -71,8 +71,16 @@ module Agents
71 71
         'expected_update_period_in_days' => '2'
72 72
       }
73 73
     end
74
+    
75
+    def check
76
+      if key_setup?
77
+        create_event :payload => model(weather_provider, which_day).merge('location' => location)
78
+      end
79
+    end
74 80
 
75
-    def service
81
+    private
82
+    
83
+    def weather_provider
76 84
       interpolated["service"].presence || "wunderground"
77 85
     end
78 86
 
@@ -85,8 +93,7 @@ module Agents
85 93
     end
86 94
 
87 95
     def validate_options
88
-      errors.add(:base, "service is required") unless service.present?
89
-      errors.add(:base, "service must be set to 'forecastio' or 'wunderground'") unless ["forecastio", "wunderground"].include?(service)
96
+      errors.add(:base, "service must be set to 'forecastio' or 'wunderground'") unless ["forecastio", "wunderground"].include?(weather_provider)
90 97
       errors.add(:base, "location is required") unless location.present?
91 98
       errors.add(:base, "api_key is required") unless key_setup?
92 99
       errors.add(:base, "which_day selection is required") unless which_day.present?
@@ -104,10 +111,10 @@ module Agents
104 111
       end
105 112
     end
106 113
 
107
-    def model(service,which_day)
108
-      if service == "wunderground"
114
+    def model(weather_provider,which_day)
115
+      if weather_provider == "wunderground"
109 116
         wunderground[which_day]
110
-      elsif service == "forecastio"
117
+      elsif weather_provider == "forecastio"
111 118
         forecastio.each do |value|
112 119
           timestamp = Time.at(value.time)
113 120
           if (timestamp.to_date - Time.now.to_date).to_i == which_day
@@ -174,12 +181,5 @@ module Agents
174 181
         end
175 182
       end
176 183
     end
177
-
178
-    def check
179
-      if key_setup?
180
-        create_event :payload => model(service, which_day).merge('location' => location)
181
-      end
182
-    end
183
-
184 184
   end
185 185
 end

+ 28 - 0
spec/models/agents/weather_agent_spec.rb

@@ -0,0 +1,28 @@
1
+require 'spec_helper'
2
+
3
+describe Agents::WeatherAgent do
4
+  let(:agent) do
5
+    Agents::WeatherAgent.create(
6
+      name: 'weather',
7
+      options: { 
8
+        :location => 94103, 
9
+        :lat => 37.779329, 
10
+        :lng => -122.41915, 
11
+        :api_key => 'test' 
12
+      }
13
+    ).tap do |agent|
14
+      agent.user = users(:bob)  
15
+      agent.save!
16
+    end
17
+  end
18
+  
19
+  it "creates a valid agent" do
20
+    expect(agent).to be_valid
21
+  end
22
+  
23
+  describe "#service" do
24
+    it "doesn't have a Service object attached" do
25
+      expect(agent.service).to be_nil
26
+    end
27
+  end
28
+end