file_handling.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. module FileHandling
  2. extend ActiveSupport::Concern
  3. def get_file_pointer(file)
  4. { file_pointer: { file: file, agent_id: id } }
  5. end
  6. def get_io(event)
  7. return nil unless event.payload['file_pointer'] &&
  8. event.payload['file_pointer']['file'] &&
  9. event.payload['file_pointer']['agent_id']
  10. event.user.agents.find(event.payload['file_pointer']['agent_id']).get_io(event.payload['file_pointer']['file'])
  11. end
  12. def emitting_file_handling_agent_description
  13. @emitting_file_handling_agent_description ||=
  14. "This agent only emits a 'file pointer', not the data inside the files, the following agents can consume the created events: `#{receiving_file_handling_agents.join('`, `')}`. Read more about the concept in the [wiki](https://github.com/cantino/huginn/wiki/How-Huginn-works-with-files)."
  15. end
  16. def receiving_file_handling_agent_description
  17. @receiving_file_handling_agent_description ||=
  18. "This agent can consume a 'file pointer' event from the following agents with no additional configuration: `#{emitting_file_handling_agents.join('`, `')}`. Read more about the concept in the [wiki](https://github.com/cantino/huginn/wiki/How-Huginn-works-with-files)."
  19. end
  20. private
  21. def emitting_file_handling_agents
  22. emitting_file_handling_agents = file_handling_agents.select { |a| a.emits_file_pointer? }
  23. emitting_file_handling_agents.map { |a| a.to_s.demodulize }
  24. end
  25. def receiving_file_handling_agents
  26. receiving_file_handling_agents = file_handling_agents.select { |a| a.consumes_file_pointer? }
  27. receiving_file_handling_agents.map { |a| a.to_s.demodulize }
  28. end
  29. def file_handling_agents
  30. @file_handling_agents ||= Agent.types.select{ |c| c.included_modules.include?(FileHandling) }.map { |d| d.name.constantize }
  31. end
  32. module ClassMethods
  33. def emits_file_pointer!
  34. @emits_file_pointer = true
  35. end
  36. def emits_file_pointer?
  37. !!@emits_file_pointer
  38. end
  39. def consumes_file_pointer!
  40. @consumes_file_pointer = true
  41. end
  42. def consumes_file_pointer?
  43. !!@consumes_file_pointer
  44. end
  45. end
  46. end