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. #{'## 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?}
  7. The _DropboxFileUrlAgent_ is used to work with Dropbox. It takes a file path (or multiple files paths) and emits
  8. events with [temporary links](https://www.dropbox.com/developers/core/docs#media).
  9. The incoming event payload needs to have a `paths` key, with a comma-separated list of files you want the URL for. For example:
  10. {
  11. "paths": "first/path, second/path"
  12. }
  13. __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_:
  14. {
  15. "instructions": {
  16. "paths": "{{ added | map: 'path' | join: ',' }}"
  17. },
  18. "matchers": [],
  19. "mode": "clean"
  20. }
  21. 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.
  22. MD
  23. event_description <<-MD
  24. The event payload will contain the following fields:
  25. {
  26. "url": "https://dl.dropboxusercontent.com/1/view/abcdefghijk/example",
  27. "expires": "Fri, 16 Sep 2011 01:01:25 +0000"
  28. }
  29. MD
  30. def working?
  31. !recent_error_logs?
  32. end
  33. def receive(events)
  34. events.map { |e| e.payload['paths'].split(',').map(&:strip) }
  35. .flatten.each { |path| create_event payload: url_for(path) }
  36. end
  37. private
  38. def url_for(path)
  39. dropbox.find(path).direct_url
  40. end
  41. end
  42. end