Skip to content

Commit

Permalink
Prevent confusion between Huginn services and strings
Browse files Browse the repository at this point in the history
- agent.service was returning an unexpected string when traversing
  all agents.
- helps with issue huginn#968 which deals with import/export
  • Loading branch information
TildeWill committed Oct 4, 2015
1 parent 5fa1824 commit ea2ef01
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
26 changes: 13 additions & 13 deletions app/models/agents/weather_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,16 @@ def default_options
'expected_update_period_in_days' => '2'
}
end

def check
if key_setup?
create_event :payload => model(weather_provider, which_day).merge('location' => location)
end
end

def service
private

def weather_provider
interpolated["service"].presence || "wunderground"
end

Expand All @@ -85,8 +93,7 @@ def location
end

def validate_options
errors.add(:base, "service is required") unless service.present?
errors.add(:base, "service must be set to 'forecastio' or 'wunderground'") unless ["forecastio", "wunderground"].include?(service)
errors.add(:base, "service must be set to 'forecastio' or 'wunderground'") unless ["forecastio", "wunderground"].include?(weather_provider)
errors.add(:base, "location is required") unless location.present?
errors.add(:base, "api_key is required") unless key_setup?
errors.add(:base, "which_day selection is required") unless which_day.present?
Expand All @@ -104,10 +111,10 @@ def forecastio
end
end

def model(service,which_day)
if service == "wunderground"
def model(weather_provider,which_day)
if weather_provider == "wunderground"
wunderground[which_day]
elsif service == "forecastio"
elsif weather_provider == "forecastio"
forecastio.each do |value|
timestamp = Time.at(value.time)
if (timestamp.to_date - Time.now.to_date).to_i == which_day
Expand Down Expand Up @@ -174,12 +181,5 @@ def model(service,which_day)
end
end
end

def check
if key_setup?
create_event :payload => model(service, which_day).merge('location' => location)
end
end

end
end
28 changes: 28 additions & 0 deletions spec/models/agents/weather_agent_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'spec_helper'

describe Agents::WeatherAgent do
let(:agent) do
Agents::WeatherAgent.create(
name: 'weather',
options: {
:location => 94103,
:lat => 37.779329,
:lng => -122.41915,
:api_key => 'test'
}
).tap do |agent|
agent.user = users(:bob)
agent.save!
end
end

it "creates a valid agent" do
expect(agent).to be_valid
end

describe "#service" do
it "doesn't have a Service object attached" do
expect(agent.service).to be_nil
end
end
end

0 comments on commit ea2ef01

Please sign in to comment.