mirror of
https://github.com/Fishwaldo/huginn.git
synced 2025-03-17 12:21:31 +00:00
Add dropbox agent to emit urls for the given paths.
This commit is contained in:
parent
f9f9c1061b
commit
9044e8ba9b
2 changed files with 130 additions and 0 deletions
56
app/models/agents/dropbox_file_url_agent.rb
Normal file
56
app/models/agents/dropbox_file_url_agent.rb
Normal file
|
@ -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
|
74
spec/models/agents/dropbox_file_url_agent_spec.rb
Normal file
74
spec/models/agents/dropbox_file_url_agent_spec.rb
Normal file
|
@ -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
|
Loading…
Add table
Reference in a new issue