Skip to content

Commit

Permalink
Merge 34460f2 into 43d0256
Browse files Browse the repository at this point in the history
  • Loading branch information
jvans1 committed Jun 1, 2014
2 parents 43d0256 + 34460f2 commit fc64b6a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
46 changes: 31 additions & 15 deletions app/concerns/twitter_concern.rb
Expand Up @@ -2,40 +2,56 @@ module TwitterConcern
extend ActiveSupport::Concern

included do
validate :validate_twitter_options
end

def validate_twitter_options
unless twitter_consumer_key.present? &&
twitter_consumer_secret.present? &&
twitter_oauth_token.present? &&
twitter_oauth_token_secret.present?
errors.add(:base, "Twitter consumer_key, consumer_secret, oauth_token, and oauth_token_secret are required to authenticate with the Twitter API. You can provide these as options to this Agent, or as Credentials with the same names, but starting with 'twitter_'.")
end
validate :credential_presence
validate :valid_credentials
end

def twitter_consumer_key
options['consumer_key'].presence || credential('twitter_consumer_key')
@twitter_consumer_key ||= options['consumer_key'].presence || credential('twitter_consumer_key')
end

def twitter_consumer_secret
options['consumer_secret'].presence || credential('twitter_consumer_secret')
@twitter_consumer_secret ||= options['consumer_secret'].presence || credential('twitter_consumer_secret')
end

def twitter_oauth_token
options['oauth_token'].presence || options['access_key'].presence || credential('twitter_oauth_token')
@twitter_oauth_token ||= options['oauth_token'].presence || options['access_key'].presence || credential('twitter_oauth_token')
end

def twitter_oauth_token_secret
options['oauth_token_secret'].presence || options['access_secret'].presence || credential('twitter_oauth_token_secret')
@twitter_oauth_token_secret ||= options['oauth_token_secret'].presence || options['access_secret'].presence || credential('twitter_oauth_token_secret')
end

def twitter
Twitter::REST::Client.new do |config|
@twitter ||= Twitter::REST::Client.new do |config|
config.consumer_key = twitter_consumer_key
config.consumer_secret = twitter_consumer_secret
config.access_token = twitter_oauth_token
config.access_token_secret = twitter_oauth_token_secret
end
end


private
def credential_presence
if twitter_consumer_key.nil?
errors.add(:base, "Consumer Key cannot be blank")
end
if twitter_consumer_secret.nil?
errors.add(:base, "Consumer secret cannot be blank")
end
if twitter_oauth_token.nil?
errors.add(:base, "Oauth token cannot be blank")
end
if twitter_oauth_token_secret.nil?
errors.add(:base, "Oauth secret cannot be blank")
end
end

def valid_credentials
twitter.verify_credentials.present?
rescue Twitter::Error
errors.add(:base, "Twitter credentials are invalid, a connection could not be established")
false
end
end
20 changes: 20 additions & 0 deletions spec/models/agents/twitter_stream_agent_spec.rb
Expand Up @@ -17,6 +17,26 @@
@agent.save!
end

describe "#valid_credentials" do
let(:stream) { Agents::TwitterStreamAgent.new }
let(:invalid_error_message) { "Twitter credentials are invalid, a connection could not be established" }
context "invalid credentials" do
xit 'includes invalid credential errors' do
allow_any_instance_of(Twitter::REST::Client).to receive(:verify_credentials).and_raise Twitter::Error::Unauthorized.new("invalid")
stream.valid?
expect(stream.errors.full_messages).to include?(invalid_error_message)
end
end

context "valid credentials" do
xit 'includes invalid credential errors' do
allow_any_instance_of(Twitter::REST::Client).to receive(:verify_credentials).and_return(true)
stream.valid?
expect(stream.errors.full_messages).to_not include?(invalid_error_message)
end
end
end

describe '#process_tweet' do
context "when generate is set to 'counts'" do
before do
Expand Down

0 comments on commit fc64b6a

Please sign in to comment.