Class: Agents::StravaAgent

Inherits:
Agent
  • Object
show all
Includes:
FormConfigurable
Defined in:
lib/huginn_strava_agent/strava_agent.rb

Instance Method Summary collapse

Instance Method Details

#checkObject



165
166
167
# File 'lib/huginn_strava_agent/strava_agent.rb', line 165

def check
  trigger_action
end

#default_optionsObject



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/huginn_strava_agent/strava_agent.rb', line 99

def default_options
  {
    'type' => 'get_activities',
    'client_id' => '',
    'client_secret' => '',
    'refresh_token' => '{% credential strava_refresh_token %}',
    'bearer_token' => '{% credential strava_bearer_token %}',
    'per_page' => '5',
    'debug' => 'false',
    'expected_receive_period_in_days' => '2',
  }
end

#receive(incoming_events) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/huginn_strava_agent/strava_agent.rb', line 156

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

#validate_optionsObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/huginn_strava_agent/strava_agent.rb', line 120

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

  unless options['per_page'].present? || !['get_activities'].include?(options['type'])
    errors.add(:base, "per_page is a required field")
  end

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

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

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

  unless options['bearer_token'].present?
    errors.add(:base, "bearer_token is a required field")
  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)


152
153
154
# File 'lib/huginn_strava_agent/strava_agent.rb', line 152

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