data_output_agent.rb 4.7 KB

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