mirror of
https://github.com/Fishwaldo/huginn.git
synced 2025-03-15 19:31:26 +00:00
Merge pull request #1541 from kreuzwerker/permanent-dropbox-links
Permanent dropbox links
This commit is contained in:
commit
47715c3349
2 changed files with 82 additions and 35 deletions
|
@ -6,7 +6,7 @@ module Agents
|
|||
no_bulk_receive!
|
||||
|
||||
description <<-MD
|
||||
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).
|
||||
The _DropboxFileUrlAgent_ is used to work with Dropbox. It takes a file path (or multiple files paths) and emits events with either [temporary links](https://www.dropbox.com/developers/core/docs#media) or [permanent links](https://www.dropbox.com/developers/core/docs#shares).
|
||||
|
||||
#{'## 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?}
|
||||
|
||||
|
@ -28,6 +28,8 @@ module Agents
|
|||
|
||||
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.
|
||||
|
||||
Set `link_type` to `'temporary'` if you want temporary links, or to `'permanent'` for permanent ones.
|
||||
|
||||
MD
|
||||
|
||||
event_description <<-MD
|
||||
|
@ -39,21 +41,35 @@ module Agents
|
|||
}
|
||||
MD
|
||||
|
||||
def default_options
|
||||
{
|
||||
'link_type' => 'temporary'
|
||||
}
|
||||
end
|
||||
|
||||
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) }
|
||||
events.flat_map { |e| e.payload['paths'].split(',').map(&:strip) }
|
||||
.each do |path|
|
||||
create_event payload: (options['link_type'] == 'permanent' ? permanent_url_for(path) : temporary_url_for(path))
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def url_for(path)
|
||||
def temporary_url_for(path)
|
||||
dropbox.find(path).direct_url
|
||||
end
|
||||
|
||||
def permanent_url_for(path)
|
||||
result = dropbox.find(path).share_url({ :short_url => false })
|
||||
result.url = result.url.gsub('?dl=0','?dl=1') # cause the url to point to the file, instead of to a preview page for the file
|
||||
result
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -24,51 +24,82 @@ describe Agents::DropboxFileUrlAgent do
|
|||
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)
|
||||
def 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 } }
|
||||
context 'with temporaty urls' 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' } }
|
||||
|
||||
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 = 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 = 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
|
||||
|
||||
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)
|
||||
context 'with permanent urls' do
|
||||
def response_for(url)
|
||||
Dropbox::API::Object.new(
|
||||
url: "https://www.dropbox.com/s/#{url}?dl=0",
|
||||
expires: "Tue, 01 Jan 2030 00:00:00 +0000",
|
||||
visibility: "PUBLIC"
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
let(:first_dropbox_url_payload) { response_for('/first/path') }
|
||||
let(:second_dropbox_url_payload) { response_for('/second/path') }
|
||||
let(:third_dropbox_url_payload) { response_for('/third/path') }
|
||||
|
||||
context 'with multiple comma-separated paths' do
|
||||
before(:each) do
|
||||
stub.proxy(Dropbox::API::Client).new do |api|
|
||||
stub(api).find('/first/path') { stub(Dropbox::API::File.new).share_url { first_dropbox_url_payload } }
|
||||
stub(api).find('/second/path') { stub(Dropbox::API::File.new).share_url { second_dropbox_url_payload } }
|
||||
stub(api).find('/third/path') { stub(Dropbox::API::File.new).share_url { third_dropbox_url_payload } }
|
||||
end
|
||||
@agent.options['link_type'] = 'permanent'
|
||||
end
|
||||
|
||||
before(:each) { @event = create_event(paths: '/first/path, /second/path, /third/path') }
|
||||
it 'creates one event with a single path' do
|
||||
expect { @agent.receive([event(paths: '/first/path')]) }.to change(Event, :count).by(1)
|
||||
expect(Event.last.payload).to eq(first_dropbox_url_payload.to_h)
|
||||
end
|
||||
|
||||
it 'creates one event with the temporary dropbox link for each path' do
|
||||
expect { @agent.receive([@event]) }.to change(Event, :count).by(3)
|
||||
it 'creates one event with the permanent dropbox link for each path' do
|
||||
event = event(paths: '/first/path, /second/path, /third/path')
|
||||
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)
|
||||
expect(last_events[0].payload).to eq(first_dropbox_url_payload.to_h)
|
||||
expect(last_events[1].payload).to eq(second_dropbox_url_payload.to_h)
|
||||
expect(last_events[2].payload).to eq(third_dropbox_url_payload.to_h)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue