data_output_agent.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. module Agents
  2. class DataOutputAgent < Agent
  3. include LiquidInterpolatable
  4. cannot_be_scheduled!
  5. description do
  6. <<-MD
  7. The Agent outputs received events as either RSS or JSON. Use it to output a public or private stream of Huginn data.
  8. This Agent will output data at:
  9. `https://#{ENV['DOMAIN']}/users/#{user.id}/web_requests/#{id || '<id>'}/:secret.xml`
  10. where `:secret` is one of the allowed secrets specified in your options and the extension can be `xml` or `json`.
  11. You can setup multiple secrets so that you can individually authorize external systems to
  12. access your Huginn data.
  13. Options:
  14. * `secrets` - An array of tokens that the requestor must provide for light-weight authentication.
  15. * `expected_receive_period_in_days` - How often you expect data to be received by this Agent from other Agents.
  16. * `template` - A JSON object representing a mapping between item output keys and incoming event values. Use [Liquid](https://github.com/cantino/huginn/wiki/Formatting-Events-using-Liquid) to format the values. The `item` key will be repeated for every Event.
  17. MD
  18. end
  19. def default_options
  20. {
  21. "secrets" => ["a-secret-key"],
  22. "expected_receive_period_in_days" => 2,
  23. "template" => {
  24. "title" => "XKCD comics as a feed",
  25. "description" => "This is a feed of recent XKCD comics, generated by Huginn",
  26. "item" => {
  27. "title" => "{{title}}",
  28. "description" => "Secret hovertext: {{hovertext}}",
  29. "link" => "{{url}}",
  30. }
  31. }
  32. }
  33. end
  34. #"guid" => "",
  35. # "pubDate" => ""
  36. def working?
  37. last_receive_at && last_receive_at > options['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
  38. end
  39. def validate_options
  40. unless options['secrets'].is_a?(Array) && options['secrets'].length > 0
  41. errors.add(:base, "Please specify one or more secrets for 'authenticating' incoming feed requests")
  42. end
  43. unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
  44. 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")
  45. end
  46. unless options['template'].present? && options['template']['item'].present? && options['template']['item'].is_a?(Hash)
  47. errors.add(:base, "Please provide template and template.item")
  48. end
  49. end
  50. def events_to_show
  51. (options['events_to_show'].presence || 40).to_i
  52. end
  53. def feed_ttl
  54. (options['ttl'].presence || 60).to_i
  55. end
  56. def feed_title
  57. options['template']['title'].presence || "#{name} Event Feed"
  58. end
  59. def feed_link
  60. options['template']['link'].presence || "https://#{ENV['DOMAIN']}"
  61. end
  62. def feed_description
  63. options['template']['description'].presence || "A feed of Events received by the '#{name}' Huginn Agent"
  64. end
  65. def receive_web_request(params, method, format)
  66. if options['secrets'].include?(params['secret'])
  67. items = received_events.order('id desc').limit(events_to_show).map do |event|
  68. interpolated = interpolate_options(options['template']['item'], event.payload)
  69. interpolated['guid'] = event.id
  70. interpolated['pubDate'] = event.created_at.rfc2822.to_s
  71. interpolated
  72. end
  73. if format =~ /json/
  74. content = {
  75. 'title' => feed_title,
  76. 'description' => feed_description,
  77. 'pubDate' => Time.now,
  78. 'items' => items
  79. }
  80. return [content, 200]
  81. else
  82. content = Utils.unindent(<<-XML)
  83. <?xml version="1.0" encoding="UTF-8" ?>
  84. <rss version="2.0">
  85. <channel>
  86. <title>#{feed_title.encode(:xml => :text)}</title>
  87. <description>#{feed_description.encode(:xml => :text)}</description>
  88. <link>#{feed_link.encode(:xml => :text)}</link>
  89. <lastBuildDate>#{Time.now.rfc2822.to_s.encode(:xml => :text)}</lastBuildDate>
  90. <pubDate>#{Time.now.rfc2822.to_s.encode(:xml => :text)}</pubDate>
  91. <ttl>#{feed_ttl}</ttl>
  92. XML
  93. content += items.to_xml(:skip_types => true, :root => "items", :skip_instruct => true, :indent => 1).gsub(/^<\/?items>/, '').strip
  94. content += Utils.unindent(<<-XML)
  95. </channel>
  96. </rss>
  97. XML
  98. return [content, 200, 'text/xml']
  99. end
  100. else
  101. if format =~ /json/
  102. return [{ :error => "Not Authorized" }, 401]
  103. else
  104. return ["Not Authorized", 401]
  105. end
  106. end
  107. end
  108. end
  109. end