forked from huginn/huginn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request huginn#649 from dsander/wunderlist
Adds Wunderlist agent
- Loading branch information
Showing
9 changed files
with
175 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
module Agents | ||
class WunderlistAgent < Agent | ||
include FormConfigurable | ||
include Oauthable | ||
valid_oauth_providers :wunderlist | ||
|
||
cannot_be_scheduled! | ||
|
||
gem_dependency_check { Devise.omniauth_providers.include?(:wunderlist) } | ||
|
||
description <<-MD | ||
#{'## Include the `omniauth-wunderlist` gem in your `Gemfile` and set `WUNDERLIST_OAUTH_KEY` and `WUNDERLIST_OAUTH_SECRET` in your environment to use this Agent' if dependencies_missing?} | ||
The WunderlistAgent creates new new tasks based on the incoming event. | ||
To be able to use this Agent you need to authenticate with Wunderlist in the [Services](/services) section first. | ||
MD | ||
|
||
def default_options | ||
{ | ||
'list_id' => '', | ||
'title' => '{{title}}' | ||
} | ||
end | ||
|
||
form_configurable :list_id, roles: :completable | ||
form_configurable :title | ||
|
||
def complete_list_id | ||
response = request_guard do | ||
HTTParty.get lists_url, request_options | ||
end | ||
response.map { |p| {text: "#{p['title']} (#{p['id']})", id: p['id']}} | ||
end | ||
|
||
def validate_options | ||
errors.add(:base, "you need to specify the list you want to add tasks to") unless options['list_id'].present? | ||
errors.add(:base, "you need to specify the title of the task to create") unless options['title'].present? | ||
end | ||
|
||
def working? | ||
!recent_error_logs? | ||
end | ||
|
||
def receive(incoming_events) | ||
incoming_events.each do |event| | ||
mo = interpolated(event) | ||
title = mo[:title][0..244] | ||
log("Creating new task '#{title}' on list #{mo[:list_id]}", inbound_event: event) | ||
request_guard do | ||
HTTParty.post tasks_url, request_options.merge(body: {title: title, list_id: mo[:list_id].to_i}.to_json) | ||
end | ||
end | ||
end | ||
private | ||
def request_guard(&blk) | ||
response = yield | ||
error("Error during http request: #{response.body}") if response.code > 400 | ||
response | ||
end | ||
|
||
def lists_url | ||
"https://a.wunderlist.com/api/v1/lists" | ||
end | ||
|
||
def tasks_url | ||
"https://a.wunderlist.com/api/v1/tasks" | ||
end | ||
|
||
def request_options | ||
{:headers => {'Content-Type' => 'application/json', | ||
'User-Agent' => 'Huginn (https://github.com/cantino/huginn)', | ||
'X-Access-Token' => service.token, | ||
'X-Client-ID' => ENV["WUNDERLIST_OAUTH_KEY"] }} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
require 'spec_helper' | ||
require 'models/concerns/oauthable' | ||
|
||
describe Agents::WunderlistAgent do | ||
it_behaves_like Oauthable | ||
|
||
before(:each) do | ||
|
||
@valid_params = { | ||
'list_id' => '12345', | ||
'title' => '{{title}}: {{url}}', | ||
} | ||
|
||
@checker = Agents::WunderlistAgent.new(:name => "somename", :options => @valid_params) | ||
@checker.user = users(:jane) | ||
@checker.service = services(:generic) | ||
@checker.save! | ||
|
||
@event = Event.new | ||
@event.agent = agents(:bob_weather_agent) | ||
@event.payload = { title: 'hello', url: 'www.example.com'} | ||
@event.save! | ||
end | ||
|
||
describe "validating" do | ||
before do | ||
expect(@checker).to be_valid | ||
end | ||
|
||
it "should require the title" do | ||
@checker.options['title'] = nil | ||
expect(@checker).not_to be_valid | ||
end | ||
|
||
it "should require the list_id" do | ||
@checker.options['list_id'] = nil | ||
expect(@checker).not_to be_valid | ||
end | ||
end | ||
|
||
it "should generate the request_options" do | ||
expect(@checker.send(:request_options)).to eq({:headers=>{"Content-Type"=>"application/json", "User-Agent"=>"Huginn (https://github.com/cantino/huginn)", "X-Access-Token"=>"1234token", "X-Client-ID"=>"wunderoauthkey"}}) | ||
end | ||
|
||
describe "#complete_list_id" do | ||
it "should return a array of hashes" do | ||
stub_request(:get, 'https://a.wunderlist.com/api/v1/lists').to_return( | ||
:body => JSON.dump([{title: 'test', id: 12345}]), | ||
:headers => {"Content-Type" => "text/json"} | ||
) | ||
expect(@checker.complete_list_id).to eq([{:text=>"test (12345)", :id=>12345}]) | ||
end | ||
end | ||
|
||
describe "#receive" do | ||
it "send a message to the hipchat" do | ||
stub_request(:post, 'https://a.wunderlist.com/api/v1/tasks').with { |request| request.body == 'abc'} | ||
@checker.receive([@event]) | ||
end | ||
end | ||
|
||
describe "#working?" do | ||
it "should be working with no entry in the error log" do | ||
expect(@checker).to be_working | ||
end | ||
|
||
it "should not be working with a recent entry in the error log" do | ||
@checker.error("test") | ||
@checker.reload | ||
@checker.last_event_at = Time.now | ||
expect(@checker).to_not be_working | ||
end | ||
end | ||
end |