update WeatherAgent to allow any location that wunderground supports

Andrew Cantino 11 lat temu
rodzic
commit
3efaed7e2a
4 zmienionych plików z 17 dodań i 15 usunięć
  1. 2 2
      Gemfile.lock
  2. 9 7
      app/models/agents/weather_agent.rb
  3. 2 2
      db/seeds.rb
  4. 4 4
      spec/fixtures/agents.yml

+ 2 - 2
Gemfile.lock

@@ -120,8 +120,8 @@ GEM
120 120
     method_source (0.8.1)
121 121
     mime-types (1.23)
122 122
     mini_portile (0.5.1)
123
-    multi_json (1.7.7)
124
-    multi_xml (0.5.4)
123
+    multi_json (1.7.8)
124
+    multi_xml (0.5.5)
125 125
     multipart-post (1.2.0)
126 126
     mysql2 (0.3.13)
127 127
     nokogiri (1.6.0)

+ 9 - 7
app/models/agents/weather_agent.rb

@@ -1,11 +1,14 @@
1 1
 require 'date'
2
+require 'cgi'
2 3
 
3 4
 module Agents
4 5
   class WeatherAgent < Agent
5 6
     cannot_receive_events!
6 7
 
7 8
     description <<-MD
8
-      The WeatherAgent creates an event for the following day's weather at `zipcode`.
9
+      The WeatherAgent creates an event for the following day's weather at a given `location`.
10
+
11
+      The `location` can be a US zipcode, or any location that Wunderground supports.  To find one, search [wunderground.com](http://wunderground.com) and copy the location part of the URL.  For example, a result for San Francisco gives `http://www.wunderground.com/US/CA/San_Francisco.html` and London, England gives `http://www.wunderground.com/q/zmw:00000.1.03772`.  The locations in each are `US/CA/San_Francisco` and `zmw:00000.1.03772`, respectively.
9 12
 
10 13
       You must setup an [API key for Wunderground](http://www.wunderground.com/weather/api/) in order to use this Agent.
11 14
     MD
@@ -14,7 +17,7 @@ module Agents
14 17
       Events look like this:
15 18
 
16 19
           {
17
-            "zipcode": 12345,
20
+            "location": 12345,
18 21
             "date": {
19 22
               "epoch": "1357959600",
20 23
               "pretty": "10:00 PM EST on January 11, 2013"
@@ -52,21 +55,20 @@ module Agents
52 55
     def default_options
53 56
       {
54 57
         :api_key => "your-key",
55
-        :zipcode => "94103"
58
+        :location => "94103"
56 59
       }
57
-
58 60
     end
59 61
 
60 62
     def validate_options
61
-      errors.add(:base, "zipcode is required") unless options[:zipcode].present?
63
+      errors.add(:base, "location is required") unless options[:location].present? || options[:zipcode].present?
62 64
       errors.add(:base, "api_key is required") unless options[:api_key].present?
63 65
     end
64 66
 
65 67
     def check
66 68
       if key_setup?
67
-        wunderground.forecast_for(options[:zipcode])["forecast"]["simpleforecast"]["forecastday"].each do |day|
69
+        wunderground.forecast_for(options[:location] || options[:zipcode])["forecast"]["simpleforecast"]["forecastday"].each do |day|
68 70
           if is_tomorrow?(day)
69
-            create_event :payload => day.merge(:zipcode => options[:zipcode])
71
+            create_event :payload => day.merge(:location => options[:location] || options[:zipcode])
70 72
           end
71 73
         end
72 74
       end

+ 2 - 2
db/seeds.rb

@@ -13,7 +13,7 @@ unless user.agents.where(:name => "SF Weather Agent").exists?
13 13
   Agent.build_for_type("Agents::WeatherAgent", user,
14 14
                        :name => "SF Weather Agent",
15 15
                        :schedule => "10pm",
16
-                       :options => { :zipcode => "94103", :api_key => "your-key" }).save!
16
+                       :options => { :location => "94103", :api_key => "your-key" }).save!
17 17
 end
18 18
 
19 19
 unless user.agents.where(:name => "XKCD Source").exists?
@@ -58,7 +58,7 @@ unless user.agents.where(:name => "Rain Notifier").exists?
58 58
                                           :value => "rain|storm",
59 59
                                           :path => "conditions"
60 60
                                       }],
61
-                           :message => "Just so you know, it looks like '<conditions>' tomorrow in <zipcode>"
61
+                           :message => "Just so you know, it looks like '<conditions>' tomorrow in <location>"
62 62
                        }).save!
63 63
 end
64 64
 

+ 4 - 4
spec/fixtures/agents.yml

@@ -34,7 +34,7 @@ bob_weather_agent:
34 34
   schedule: "midnight"
35 35
   name: "SF Weather"
36 36
   options: <%= {
37
-                 :zipcode => 94102,
37
+                 :location => 94102,
38 38
                  :lat => 37.779329,
39 39
                  :lng => -122.41915,
40 40
                  :api_key => 'test'
@@ -46,7 +46,7 @@ jane_weather_agent:
46 46
   schedule: "midnight"
47 47
   name: "SF Weather"
48 48
   options: <%= {
49
-                 :zipcode => 94103,
49
+                 :location => 94103,
50 50
                  :lat => 37.779329,
51 51
                  :lng => -122.41915,
52 52
                  :api_key => 'test'
@@ -63,7 +63,7 @@ jane_rain_notifier_agent:
63 63
                    :value => "rain",
64 64
                    :path => "conditions"
65 65
                  }],
66
-                 :message => "Just so you know, it looks like '<conditions>' tomorrow in <zipcode>"
66
+                 :message => "Just so you know, it looks like '<conditions>' tomorrow in <location>"
67 67
                }.to_yaml.inspect %>
68 68
 
69 69
 bob_rain_notifier_agent:
@@ -77,7 +77,7 @@ bob_rain_notifier_agent:
77 77
                    :value => "rain",
78 78
                    :path => "conditions"
79 79
                   }],
80
-                 :message => "Just so you know, it looks like '<conditions>' tomorrow in <zipcode>"
80
+                 :message => "Just so you know, it looks like '<conditions>' tomorrow in <location>"
81 81
                }.to_yaml.inspect %>
82 82
 
83 83
 bob_twitter_user_agent: