dropbox_file_url_agent.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. module Agents
  2. class DropboxFileUrlAgent < Agent
  3. include DropboxConcern
  4. cannot_be_scheduled!
  5. description <<-MD
  6. The Dropbox File Url Agent is used to work with Dropbox. It takes a file path (or multiple file paths) and emits events with [temporary links](https://www.dropbox.com/developers/core/docs#media).
  7. #{'## Include the `dropbox-api` and `omniauth-dropbox` gems in your `Gemfile` and set `DROPBOX_OAUTH_KEY` and `DROPBOX_OAUTH_SECRET` in your environment to use Dropbox Agents.' if dependencies_missing?}
  8. The incoming event payload needs to have a `paths` key, with a comma-separated list of files you want the URL for. For example:
  9. {
  10. "paths": "first/path, second/path"
  11. }
  12. __TIP__: You can use the _Event Formatting Agent_ to format events before they come in. Here's an example configuration for formatting an event coming out of a _Dropbox Watch Agent_:
  13. {
  14. "instructions": {
  15. "paths": "{{ added | map: 'path' | join: ',' }}"
  16. },
  17. "matchers": [],
  18. "mode": "clean"
  19. }
  20. An example of usage would be to watch a specific Dropbox directory (with the _DropboxWatchAgent_) and get the URLs for the added or updated files. You could then, for example, send emails with those links.
  21. MD
  22. event_description <<-MD
  23. The event payload will contain the following fields:
  24. {
  25. "url": "https://dl.dropboxusercontent.com/1/view/abcdefghijk/example",
  26. "expires": "Fri, 16 Sep 2011 01:01:25 +0000"
  27. }
  28. MD
  29. def working?
  30. !recent_error_logs?
  31. end
  32. def receive(events)
  33. events.map { |e| e.payload['paths'].split(',').map(&:strip) }
  34. .flatten.each { |path| create_event payload: url_for(path) }
  35. end
  36. private
  37. def url_for(path)
  38. dropbox.find(path).direct_url
  39. end
  40. end
  41. end