Skip to content

Commit

Permalink
Use Pirate Weather with the Weather Agent (huginn#3317)
Browse files Browse the repository at this point in the history
* Swap Dark Sky weather client for Pirate Weather client

* Swap Dark Sky references for Pirate Weather

* Add validation to instruct users to use Pirate Weather instead of Dark Sky

* Update test fixture

* Update specs to use Pirate Weather references

* Change agent spec to use Pirate Weather reference in same style as old "darksky" version

* only change WeatherAgent readme text
  • Loading branch information
alexcochran committed Nov 20, 2023
1 parent 2d5fcaf commit ac59e7a
Show file tree
Hide file tree
Showing 9 changed files with 1,918 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ end
# Optional libraries. To conserve RAM, comment out any that you don't need,
# then run `bundle` and commit the updated Gemfile and Gemfile.lock.
gem 'erector', github: 'dsander/erector', branch: 'rails6'
gem 'forecast_io', '~> 2.0.0' # WeatherAgent
gem 'pirate_weather_forecast_ruby' # WeatherAgent
gem 'hipchat', '~> 1.2.0' # HipchatAgent
gem 'hypdf', bitbucket: 'knu/hypdf_gem', branch: 'uploadio_namespace' # PDFInfoAgent
gem 'mini_racer' # JavaScriptAgent
Expand Down
10 changes: 5 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,6 @@ GEM
rake
font-awesome-sass (4.7.0)
sass (>= 3.2)
forecast_io (2.0.2)
faraday
hashie
multi_json
formatador (1.1.0)
fugit (1.8.1)
et-orbi (~> 1, >= 1.2.7)
Expand Down Expand Up @@ -589,6 +585,10 @@ GEM
parser (3.2.2.1)
ast (~> 2.4.1)
pg (1.5.3)
pirate_weather_forecast_ruby (0.1.0)
faraday
hashie
multi_json
polyglot (0.3.5)
pry (0.14.2)
coderay (~> 1.1)
Expand Down Expand Up @@ -868,7 +868,6 @@ DEPENDENCIES
feedjira (~> 3.1)
ffi (>= 1.9.4)
font-awesome-sass (~> 4.7.0)
forecast_io (~> 2.0.0)
foreman (~> 0.87.2)!
geokit (~> 1.13)
geokit-rails (~> 2.3)
Expand Down Expand Up @@ -909,6 +908,7 @@ DEPENDENCIES
omniauth-tumblr
omniauth-twitter
pg (~> 1.1)
pirate_weather_forecast_ruby
puma
rack-livereload
rails (~> 6.1.7)
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ See [private development instructions](https://github.com/huginn/huginn/wiki/Pri

#### Enable the WeatherAgent

In order to use the WeatherAgent you need an [API key with Wunderground](http://www.wunderground.com/weather/api/). Signup for one and then change the value of `api_key: your-key` in your seeded WeatherAgent.

Note, Wunderground no longer offers free API keys. You can still use the WeatherAgent by setting the service key to darksky, and getting an [API key from DarkSky](https://darksky.net/dev).
In order to use the WeatherAgent you need an [Weather Data API key from Pirate Weather](https://pirate-weather.apiable.io/products/weather-data). Sign up for one and then change the value of `api_key: your-key` in your seeded WeatherAgent.

#### Disable SSL

Expand Down
17 changes: 11 additions & 6 deletions app/models/agents/weather_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class WeatherAgent < Agent
You also must select when you would like to get the weather forecast for using the `which_day` option, where the number 1 represents today, 2 represents tomorrow and so on. Weather forecast inforation is only returned for at most one week at a time.
The weather forecast information is provided by Dark Sky.
The weather forecast information is provided by Pirate Weather, a drop-in replacement for the Dark Sky API (which no longer has a free tier).
The `location` must be a comma-separated string of map co-ordinates (longitude, latitude). For example, San Francisco would be `37.7771,-122.4196`.
You must set up an [API key for Dark Sky](https://darksky.net/dev/) in order to use this Agent.
You must set up an [API key for Pirate Weather](https://pirate-weather.apiable.io/) in order to use this Agent.
Set `expected_update_period_in_days` to the maximum amount of time that you'd expect to pass between Events being created by this Agent.
MD
Expand Down Expand Up @@ -96,6 +96,10 @@ def wunderground?
interpolated["service"].presence && interpolated["service"].presence.downcase == "wunderground"
end

def darksky?
interpolated["service"].presence && interpolated["service"].presence.downcase == "darksky"
end

VALID_COORDS_REGEX = /^\s*-?\d{1,3}\.\d+\s*,\s*-?\d{1,3}\.\d+\s*$/

def validate_location
Expand All @@ -110,21 +114,22 @@ def validate_location
errors.add(
:base,
"Location #{location} is malformed. Location for " +
'Dark Sky must be in the format "-00.000,-00.00000". The ' +
'Pirate Weather must be in the format "-00.000,-00.00000". The ' +
"number of decimal places does not matter."
)
end
end

def validate_options
errors.add(:base,
"The Weather Underground API has been disabled since Jan 1st 2018, please switch to DarkSky") if wunderground?
"The Weather Underground API has been disabled since Jan 1st 2018, please switch to Pirate Weather") if wunderground?
errors.add(:base, "The Dark Sky API has been disabled since March 31, 2023, please switch to Pirate Weather") if darksky?
validate_location
errors.add(:base, "api_key is required") unless interpolated['api_key'].present?
errors.add(:base, "which_day selection is required") unless which_day.present?
end

def dark_sky
def pirate_weather
if key_setup?
ForecastIO.api_key = interpolated['api_key']
lat, lng = coordinates
Expand All @@ -133,7 +138,7 @@ def dark_sky
end

def model(which_day)
value = dark_sky[which_day - 1]
value = pirate_weather[which_day - 1]
if value
timestamp = Time.at(value.time)
{
Expand Down
1,880 changes: 1,879 additions & 1 deletion spec/data_fixtures/weather.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions spec/models/agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def receive(events)

describe ".receive!" do
before do
stub_request(:any, /darksky/).to_return(body: File.read(Rails.root.join("spec/data_fixtures/weather.json")),
stub_request(:any, /pirateweather/).to_return(body: File.read(Rails.root.join("spec/data_fixtures/weather.json")),
status: 200)
end

Expand Down Expand Up @@ -931,7 +931,7 @@ def @agent.receive_webhook(params)

describe ".drop_pending_events" do
before do
stub_request(:any, /darksky/).to_return(body: File.read(Rails.root.join("spec/data_fixtures/weather.json")),
stub_request(:any, /pirateweather/).to_return(body: File.read(Rails.root.join("spec/data_fixtures/weather.json")),
status: 200)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/models/agents/email_agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def get_message_part(mail, content_type)
end

it "can receive complex events and send them on" do
stub_request(:any, /darksky/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/weather.json")), :status => 200)
stub_request(:any, /pirateweather/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/weather.json")), :status => 200)
@checker.sources << agents(:bob_weather_agent)

Agent.async_check(agents(:bob_weather_agent).id)
Expand Down
2 changes: 1 addition & 1 deletion spec/models/agents/email_digest_agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def get_message_part(mail, content_type)
end

it "can receive complex events and send them on" do
stub_request(:any, /darksky/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/weather.json")), :status => 200)
stub_request(:any, /pirateweather/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/weather.json")), :status => 200)
@checker.sources << agents(:bob_weather_agent)

Agent.async_check(agents(:bob_weather_agent).id)
Expand Down
34 changes: 17 additions & 17 deletions spec/models/agents/weather_agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
end
end

let :dark_sky_agent do
let :pirate_weather_agent do
Agents::WeatherAgent.create(
name: "weather from dark sky",
name: "weather from Pirate Weather",
options: {
:location => "37.779329,-122.41915",
:service => "darksky",
:service => "pirateweather",
:which_day => 1,
:api_key => "test"
}
Expand All @@ -44,23 +44,23 @@
expect(agent.working?).to be_falsey
end

context "dark sky" do
context "pirate weather" do
it "validates the location properly" do
expect(dark_sky_agent.options["location"]).to eq "37.779329,-122.41915"
expect(dark_sky_agent).to be_valid
dark_sky_agent.options["location"] = "37.779329, -122.41915" # with a space
expect(dark_sky_agent).to be_valid
dark_sky_agent.options["location"] = "94103" # a zip code
expect(dark_sky_agent).to_not be_valid
dark_sky_agent.options["location"] = "37.779329,-122.41915"
expect(dark_sky_agent.options["location"]).to eq "37.779329,-122.41915"
expect(dark_sky_agent).to be_valid
expect(pirate_weather_agent.options["location"]).to eq "37.779329,-122.41915"
expect(pirate_weather_agent).to be_valid
pirate_weather_agent.options["location"] = "37.779329, -122.41915" # with a space
expect(pirate_weather_agent).to be_valid
pirate_weather_agent.options["location"] = "94103" # a zip code
expect(pirate_weather_agent).to_not be_valid
pirate_weather_agent.options["location"] = "37.779329,-122.41915"
expect(pirate_weather_agent.options["location"]).to eq "37.779329,-122.41915"
expect(pirate_weather_agent).to be_valid
end
it "fails cases that pass the first test but are invalid" do
dark_sky_agent.options["location"] = "137.779329, -122.41915" # too high latitude
expect(dark_sky_agent).to_not be_valid
dark_sky_agent.options["location"] = "37.779329, -522.41915" # too low longitude
expect(dark_sky_agent).to_not be_valid
pirate_weather_agent.options["location"] = "137.779329, -122.41915" # too high latitude
expect(pirate_weather_agent).to_not be_valid
pirate_weather_agent.options["location"] = "37.779329, -522.41915" # too low longitude
expect(pirate_weather_agent).to_not be_valid
end
end

Expand Down

0 comments on commit ac59e7a

Please sign in to comment.