Class: Agents::RedditAgent

Inherits:
Agent
  • Object
show all
Includes:
FormConfigurable
Defined in:
lib/huginn_reddit_agent/reddit_agent.rb

Instance Method Summary collapse

Instance Method Details

#checkObject



156
157
158
# File 'lib/huginn_reddit_agent/reddit_agent.rb', line 156

def check
  trigger_action
end

#default_optionsObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/huginn_reddit_agent/reddit_agent.rb', line 68

def default_options
  {
    'type' => 'read_unreadmessage',
    'debug' => 'false',
    'password' => '',
    'username' => '',
    'client_id' => '',
    'secret_key' => '',
    'bearer_token' => '{% credential reddit_bearer_token %}',
    'subreddit' => '',
    'limit' => '',
    'emit_events' => 'true',
    'expected_receive_period_in_days' => '2',
  }
end

#receive(incoming_events) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/huginn_reddit_agent/reddit_agent.rb', line 147

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

#validate_optionsObject



95
96
97
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/huginn_reddit_agent/reddit_agent.rb', line 95

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

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

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

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

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

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

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

  unless options['limit'].present? || !['hottest_post_subreddit'].include?(options['type'])
    errors.add(:base, "limit 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?('details') && boolify(options['details']).nil?
    errors.add(:base, "if provided, details 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)


143
144
145
# File 'lib/huginn_reddit_agent/reddit_agent.rb', line 143

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