From 9044e8ba9bc9a77ca0c01f4fb91c59cf2013016c Mon Sep 17 00:00:00 2001 From: "Guilherme J. Tramontina" Date: Wed, 15 Oct 2014 11:47:28 -0300 Subject: [PATCH] Add dropbox agent to emit urls for the given paths. --- app/models/agents/dropbox_file_url_agent.rb | 56 ++++++++++++++ .../agents/dropbox_file_url_agent_spec.rb | 74 +++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 app/models/agents/dropbox_file_url_agent.rb create mode 100644 spec/models/agents/dropbox_file_url_agent_spec.rb diff --git a/app/models/agents/dropbox_file_url_agent.rb b/app/models/agents/dropbox_file_url_agent.rb new file mode 100644 index 00000000..08e5897c --- /dev/null +++ b/app/models/agents/dropbox_file_url_agent.rb @@ -0,0 +1,56 @@ +module Agents + class DropboxFileUrlAgent < Agent + include DropboxConcern + + cannot_be_scheduled! + + description <<-MD + #{'## 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?} + The _DropboxFileUrlAgent_ takes a file path (or multiple files paths) and emits + events with [temporary links](https://www.dropbox.com/developers/core/docs#media). + + The incoming event payload needs to have a `paths` key, with a comma-separated list of files you want the URL for. For example: + + { + "paths": "first/path, second/path" + } + + __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_: + + { + "instructions": { + "paths": "{{ added | map: 'path' | join: ',' }}" + }, + "matchers": [], + "mode": "clean" + } + + MD + + event_description <<-MD + The event payload will contain the following fields: + + { + "url": "https://dl.dropboxusercontent.com/1/view/abcdefghijk/example", + "expires": "Fri, 16 Sep 2011 01:01:25 +0000" + } + MD + + def working? + !recent_error_logs? + end + + def receive(events) + events.map { |e| e.payload['paths'].split(',').map(&:strip) } + .flatten.each { |path| create_event payload: url_for(path) } + end + + private + + def url_for(path) + dropbox.find(path).direct_url + end + + end + +end diff --git a/spec/models/agents/dropbox_file_url_agent_spec.rb b/spec/models/agents/dropbox_file_url_agent_spec.rb new file mode 100644 index 00000000..a8a434f6 --- /dev/null +++ b/spec/models/agents/dropbox_file_url_agent_spec.rb @@ -0,0 +1,74 @@ +require 'spec_helper' + +describe Agents::DropboxFileUrlAgent do + before(:each) do + @agent = Agents::DropboxFileUrlAgent.new( + name: 'dropbox file to url', + options: {} + ) + @agent.user = users(:bob) + @agent.service = services(:generic) + @agent.save! + end + + it 'cannot be scheduled' do + expect(@agent.cannot_be_scheduled?).to eq true + end + + it 'has agent description' do + expect(@agent.description).to_not be_nil + end + + it 'has event description' do + expect(@agent.event_description).to_not be_nil + end + + describe "#receive" do + + let(:first_dropbox_url_payload) { { 'url' => 'http://dropbox.com/first/path/url' } } + let(:second_dropbox_url_payload) { { 'url' => 'http://dropbox.com/second/path/url' } } + let(:third_dropbox_url_payload) { { 'url' => 'http://dropbox.com/third/path/url' } } + + def create_event(payload) + event = Event.new(payload: payload) + event.agent = agents(:bob_manual_event_agent) + event.save! + event + end + + before(:each) do + stub.proxy(Dropbox::API::Client).new do |api| + stub(api).find('/first/path') { stub(Dropbox::API::File.new).direct_url { first_dropbox_url_payload } } + stub(api).find('/second/path') { stub(Dropbox::API::File.new).direct_url { second_dropbox_url_payload } } + stub(api).find('/third/path') { stub(Dropbox::API::File.new).direct_url { third_dropbox_url_payload } } + end + end + + context 'with a single path' do + + before(:each) { @event = create_event(paths: '/first/path') } + + it 'creates one event with the temporary dropbox link' do + expect { @agent.receive([@event]) }.to change(Event, :count).by(1) + expect(Event.last.payload).to eq(first_dropbox_url_payload) + end + + end + + context 'with multiple comma-separated paths' do + + before(:each) { @event = create_event(paths: '/first/path, /second/path, /third/path') } + + it 'creates one event with the temporary dropbox link for each path' do + expect { @agent.receive([@event]) }.to change(Event, :count).by(3) + last_events = Event.last(3) + expect(last_events[0].payload).to eq(first_dropbox_url_payload) + expect(last_events[1].payload).to eq(second_dropbox_url_payload) + expect(last_events[2].payload).to eq(third_dropbox_url_payload) + end + + end + + end + +end \ No newline at end of file