Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tweet Neuron #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions brain/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ gem 'faraday_middleware'
# Astricon Schedule Neuron
gem 'blurrily'

# Tweet gem
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tweet neuron?

gem 'twitter'

group :development, :test do
gem 'rspec'
gem 'guard'
Expand Down
6 changes: 6 additions & 0 deletions brain/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ GEM
ruby_speech (2.3.0)
activesupport (>= 3.0.7, < 5.0.0)
nokogiri (~> 1.6)
simple_oauth (0.2.0)
slop (3.4.6)
state_machine (1.2.0)
thor (0.18.1)
Expand All @@ -150,6 +151,10 @@ GEM
timecop (0.6.3)
timers (1.1.0)
toadhopper (2.1)
twitter (4.8.1)
faraday (~> 0.8, < 0.10)
multi_json (~> 1.0)
simple_oauth (~> 0.2)
tzinfo (0.3.37)
virtus (0.5.2)
backports (~> 2.6.1)
Expand Down Expand Up @@ -177,4 +182,5 @@ DEPENDENCIES
pipejump
rspec
timecop
twitter
webmock
2 changes: 2 additions & 0 deletions brain/lib/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require_relative 'mirror_mirror_neuron'
require_relative 'astricon_now_speaking'
require_relative 'time_neuron'
require_relative 'tweet_neuron'

class App < Adhearsion::Plugin
init :brain do
Expand All @@ -20,6 +21,7 @@ class App < Adhearsion::Plugin
@brain.add_neuron MirrorMirrorNeuron.new
@brain.add_neuron AstriconNowSpeaking.new
@brain.add_neuron TimeNeuron.new
@brain.add_neuron TweetNeuron.new
end

run :brain do
Expand Down
32 changes: 32 additions & 0 deletions brain/lib/tweet_neuron.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'twitter'

class TweetNeuron
def initialize
end

def intent
'tweet'
end

def reply(message, interpretation)
client = get_twitter_client(message.user)
return "You must log in via Twitter so I can help you tweet." unless client
entities = interpretation['outcome']['entities']
return "You have to tell me what to tweet!" unless entities.has_key? 'message'

client.update entities['message']['value']
"Message sent."
end

def get_twitter_client(user)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be private.

grant = user['auth_grants'].select { |grant| grant[:provider] == :twitter }.first
if grant
Twitter::Client.new do |config|
config.consumer_key = ENV['TWITTER_KEY']
config.consumer_secret = ENV['TWITTER_SECRET']
config.access_token = grant[:credentials]['token']
config.access_token_secret = grant[:credentials]['secret']
end
end
end
end
32 changes: 32 additions & 0 deletions brain/spec/lib/tweet_neuron_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'

describe TweetNeuron do
include NeuronMatchers

let(:subject) { TweetNeuron.new }

it "should return an error message if no tweet is provided" do
interpretation = wit_interpretation 'Tweet this', 'tweet'
should handle_message('Tweet this', :default_user, interpretation)
.and_respond_with("You have to tell me what to tweet!")
end

it "should send a tweet" do
interpretation = wit_interpretation 'Tweet Hello from AstriCon', 'tweet', message: 'Hello from AstriCon'
should handle_message('Tweet this Hello from Astricon', :default_user, interpretation)
.and_respond_with("Message sent.")
end

context "getting a Twitter client" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be tested via an attempt to send a tweet.

it "should return nil if the user has no credentials for Twitter" do
subject.get_twitter_client('auth_grants' => []).should be nil
end

it "should return an instance of Twitter::Client if credentials are supplied" do
user = {'auth_grants' => [{provider: :twitter, credentials: {'token' => 'abcd', 'secret' => 'efgh'}}]}
subject.get_twitter_client(user).should be_a Twitter::Client
end
end

end

Binary file added brain/vendor/cache/simple_oauth-0.2.0.gem
Binary file not shown.
Binary file added brain/vendor/cache/twitter-4.8.1.gem
Binary file not shown.