Class: Agents::OpenweathermapAgent

Inherits:
Agent
  • Object
show all
Includes:
FormConfigurable
Defined in:
lib/huginn_openweathermap_agent/openweathermap_agent.rb

Instance Method Summary collapse

Instance Method Details

#checkObject



140
141
142
# File 'lib/huginn_openweathermap_agent/openweathermap_agent.rb', line 140

def check
  trigger_action
end

#default_optionsObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/huginn_openweathermap_agent/openweathermap_agent.rb', line 73

def default_options
  {
    'type' => '',
    'token' => '',
    'limit' => '',
    'unit' => 'metric',
    'lat' => '',
    'lon' => '',
    'language' => 'en',
    'debug' => 'false',
    'emit_events' => 'true',
    'expected_receive_period_in_days' => '2',
  }
end

#receive(incoming_events) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/huginn_openweathermap_agent/openweathermap_agent.rb', line 131

def receive(incoming_events)
  incoming_events.each do |event|
    interpolate_with(event) do
      log event
      trigger_action
    end
  end
end

#validate_optionsObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/huginn_openweathermap_agent/openweathermap_agent.rb', line 98

def validate_options
  errors.add(:base, "type has invalid value: should be 'current_weather' 'air_pollution' 'onecall' 'forecast5d'") if interpolated['type'].present? && !%w(current_weather air_pollution onecall forecast5d).include?(interpolated['type'])

  errors.add(:base, "unit has invalid value: should be 'metric' 'standard' 'imperial'") if interpolated['type'].present? && !%w(metric standard imperial).include?(interpolated['unit'])
  errors.add(:base, "lon must be provided") if not interpolated['lon'].present? && ( interpolated['type'] == 'current_weather' or interpolated['type'] == 'air_pollution' or interpolated['type'] == 'onecall' or interpolated['type'] == 'forecast5d')

  errors.add(:base, "lat must be provided") if not interpolated['lat'].present? && ( interpolated['type'] == 'current_weather' or interpolated['type'] == 'air_pollution' or interpolated['type'] == 'onecall' or interpolated['type'] == 'forecast5d')

  unless options['language'].present?
    errors.add(:base, "language is a required field")
  end

  unless options['token'].present?
    errors.add(:base, "token is a required field")
  end

  if options.has_key?('emit_events') && boolify(options['emit_events']).nil?
    errors.add(:base, "if provided, emit_events must be true or false")
  end

  if options.has_key?('debug') && boolify(options['debug']).nil?
    errors.add(:base, "if provided, debug must be true or false")
  end

  unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
    errors.add(:base, "Please provide 'expected_receive_period_in_days' to indicate how many days can pass before this Agent is considered to be not working")
  end
end

#working?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/huginn_openweathermap_agent/openweathermap_agent.rb', line 127

def working?
  event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
end