Class: TRMNLP::Poller

Inherits:
Object
  • Object
show all
Defined in:
lib/trmnlp/poller.rb

Instance Method Summary collapse

Constructor Details

#initialize(config:, paths:, reporter: Reporter.new) ⇒ Poller

Returns a new instance of Poller.



11
12
13
14
15
# File 'lib/trmnlp/poller.rb', line 11

def initialize(config:, paths:, reporter: Reporter.new)
  @config = config
  @paths = paths
  @reporter = reporter
end

Instance Method Details

#poll_dataObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/trmnlp/poller.rb', line 17

def poll_data
  return unless config.plugin.polling?
  raise InvalidConfig, 'config must specify polling_url or polling_urls' if config.plugin.polling_urls.empty?

  data = aggregate_responses
  write_user_data(data)
  data
# NOTE: trmnlp is a dev tool — a flaky upstream API should surface a warning
# and keep the preview server alive, not crash the user's session. We
# deliberately swallow here and return {} so the renderer keeps rendering.
rescue StandardError => e
  reporter.info(reporter.yellow("warning: #{e.message}"))
  {}
end

#put_webhook(payload) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/trmnlp/poller.rb', line 32

def put_webhook(payload)
  write_user_data(wrap_array(JSON.parse(payload)))
# NOTE: Same rationale as #poll_data — a bad webhook payload shouldn't take
# down the dev server. Report a warning and keep serving.
rescue StandardError => e
  reporter.info(reporter.yellow("webhook warning: #{e.message}"))
end