cinch-twitterstatus 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
  [![Coverage Status](https://coveralls.io/repos/bhaberer/cinch-twitterstatus/badge.png?branch=master)](https://coveralls.io/r/bhaberer/cinch-twitterstatus?branch=master)
7
7
  [![Code Climate](https://codeclimate.com/github/bhaberer/cinch-twitterstatus.png)](https://codeclimate.com/github/bhaberer/cinch-twitterstatus)
8
8
 
9
- Posts the content of a linked Tweet to the channel.
9
+ Posts the content of a linked Tweet to the channel, can also follow users and post their tweets.
10
10
 
11
11
  ## Installation
12
12
 
@@ -43,6 +43,18 @@ Once you have said credentials you will need to pass them to the Plugin's config
43
43
 
44
44
  Then post a link to a specific tweet and the bot should post the content of said tweet to the channel.
45
45
 
46
+ ### Watching Twitter Users ###
47
+
48
+ If you have a twitter account that the bot should watch for new tweets, you can
49
+ define it in the config as well:
50
+
51
+ c.plugins.options[Cinch::Plugins::TwitterStatus] = { consumer_key: 'consumer_key',
52
+ consumer_secret: 'consumer_secret',
53
+ oauth_token: 'oauth_token',
54
+ oauth_secret: 'oauth_secret',
55
+ watchers: { '#channel': ['twitteruser'] } }
56
+
57
+
46
58
  ## Contributing
47
59
 
48
60
  1. Fork it
@@ -11,6 +11,7 @@ Gem::Specification.new do |gem|
11
11
  gem.description = %q{Cinch IRC bot Plugin that access the Twitter API to post the content of linked twitter statuses to the channel.}
12
12
  gem.summary = %q{Cinch Plugin to post tweets to channel.}
13
13
  gem.homepage = "https://github.com/bhaberer/cinch-twitterstatus"
14
+ gem.license = 'MIT'
14
15
 
15
16
  gem.files = `git ls-files`.split($/)
16
17
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -22,5 +23,6 @@ Gem::Specification.new do |gem|
22
23
  gem.add_development_dependency 'coveralls'
23
24
  gem.add_development_dependency 'cinch-test'
24
25
 
25
- gem.add_dependency 'twitter', '~> 4.8.1'
26
+ gem.add_dependency 'twitter', '~> 4.8.1'
27
+ gem.add_dependency 'cinch', '~> 2.0.0'
26
28
  end
@@ -9,6 +9,7 @@ module Cinch::Plugins
9
9
  self.help = 'Just link to a specific twitter status and I will post the content of that tweet.'
10
10
 
11
11
  listen_to :channel
12
+ timer 15, method: :check_watched
12
13
 
13
14
  def initialize(*args)
14
15
  super
@@ -19,6 +20,37 @@ module Cinch::Plugins
19
20
  c.oauth_token = config[:oauth_token]
20
21
  c.oauth_token_secret = config[:oauth_secret]
21
22
  end
23
+
24
+ if config[:watchers]
25
+ @watched = Hash.new
26
+ config[:watchers].each_pair do |chan, users|
27
+ @watched[chan] = Hash.new
28
+ users.each { |user| refresh_cache(chan, user) }
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ def check_watched
35
+ return unless @watched
36
+
37
+ @watched.keys.each do |chan|
38
+ @watched[chan].keys.each do |user|
39
+ begin
40
+ # Just check the last tweet, if they are posting more than once
41
+ # every timer tick I don't want to spam the channel.
42
+ tweet = Twitter::Client.new.user_timeline(user).first
43
+ unless @watched[chan][user].include?(tweet.id)
44
+ @watched[chan][user] << tweet.id
45
+
46
+ msg = format_tweet(user, Twitter.status(tweet.id).text)
47
+ Channel(chan).send msg unless msg.nil?
48
+ end
49
+ refresh_cache(chan, user)
50
+ rescue Twitter::Error::NotFound
51
+ debug "You have set an invalid or protected user (#{user}) to watch, please correct this error"
52
+ end
53
+ end
22
54
  end
23
55
  end
24
56
 
@@ -27,12 +59,8 @@ module Cinch::Plugins
27
59
  urls.each do |url|
28
60
  if url.match(/^https?:\/\/mobile|w{3}?\.?twitter\.com/)
29
61
  if tweet = url.match(/https?:\/\/mobile|w{3}?\.?twitter\.com\/?#?!?\/([^\/]+)\/statuse?s?\/(\d+)\/?/)
30
- status = Twitter.status(tweet[2]).text
31
- status.gsub!(/[\n]+/, " ") if status.match(/\n/)
32
- user = tweet[1]
33
- unless user.nil? || status.nil?
34
- m.reply "@#{user} tweeted \"#{status}\""
35
- end
62
+ msg = format_tweet(tweet[1], Twitter.status(tweet[2]).text)
63
+ m.reply msg unless msg.nil?
36
64
  end
37
65
  end
38
66
  end
@@ -41,5 +69,26 @@ module Cinch::Plugins
41
69
  rescue Twitter::Error::Forbidden
42
70
  debug "User attempted to post a Protected Tweet or you have not set valid Twitter credentials."
43
71
  end
72
+
73
+ private
74
+
75
+ def format_tweet(user, status)
76
+ # Return blank if we didn't get a good user and status
77
+ return if user.nil? || status.nil?
78
+
79
+ # Scrub the tweet for returns so we don't have multilined responses.
80
+ status.gsub!(/[\n]+/, " ") if status.match(/\n/)
81
+
82
+ return "@#{user} tweeted \"#{status}\""
83
+ end
84
+
85
+ def refresh_cache(chan, user)
86
+ @watched[chan][user] = []
87
+ Twitter::Client.new.user_timeline(user).each do |tweet|
88
+ @watched[chan][user] << tweet.id
89
+ end
90
+ rescue Twitter::Error::NotFound
91
+ debug "You have set an invalid or protected user (#{user}) to watch, please correct this error"
92
+ end
44
93
  end
45
94
  end
@@ -1,5 +1,5 @@
1
1
  module Cinch
2
2
  module Twitterstatus
3
- VERSION = "1.0.0"
3
+ VERSION = "1.0.1"
4
4
  end
5
5
  end
@@ -5,37 +5,48 @@ describe Cinch::Plugins::TwitterStatus do
5
5
  include Cinch::Test
6
6
 
7
7
  before(:all) do
8
- @bot = make_bot(Cinch::Plugins::TwitterStatus,
8
+ @bot = make_bot(Cinch::Plugins::TwitterStatus,
9
9
  { filename: '/dev/null',
10
- hostname: 'marvintesting.tumblr.com',
10
+ watchers: { '#foo' => ['weirdo513'] },
11
11
  consumer_key: ENV['CONSUMER_KEY'],
12
12
  consumer_secret: ENV['CONSUMER_SECRET'],
13
13
  oauth_token: ENV['OAUTH_TOKEN'],
14
14
  oauth_secret: ENV['OAUTH_SECRET'] })
15
15
  end
16
16
 
17
- it 'should return the texof e when linked in the channel' do
18
- get_replies(make_message(@bot, 'https://twitter.com/weirdo513/status/344186643609174016',
19
- { channel: '#foo', nick: 'bar' })).
20
- first.text.should == "@weirdo513 tweeted \"HOW IS THAT MIC STILL ON JESUS\""
21
- end
17
+ describe "Twitter Link Parsing" do
18
+ it 'should return the text of the tweet when linked in the channel' do
19
+ get_replies(make_message(@bot, 'https://twitter.com/weirdo513/status/344186643609174016',
20
+ { channel: '#foo', nick: 'bar' })).
21
+ first.text.should == "@weirdo513 tweeted \"HOW IS THAT MIC STILL ON JESUS\""
22
+ end
22
23
 
23
- it 'should not return any text if the status is invalid' do
24
- get_replies(make_message(@bot, 'https://twitter.com/weirdo513/status/3INVALI643609174016',
25
- { channel: '#foo', nick: 'bar' })).
26
- should be_empty
27
- end
24
+ it 'should not return any text if the status is invalid' do
25
+ get_replies(make_message(@bot, 'https://twitter.com/weirdo513/status/3INVALI643609174016',
26
+ { channel: '#foo', nick: 'bar' })).
27
+ should be_empty
28
+ end
29
+
30
+ it 'should not return any text if the status is protected' do
31
+ get_replies(make_message(@bot, 'https://twitter.com/brewtopian/status/68071618055901184',
32
+ { channel: '#foo', nick: 'bar' })).
33
+ should be_empty
34
+ end
28
35
 
29
- it 'should not return any text if the status is protected' do
30
- get_replies(make_message(@bot, 'https://twitter.com/brewtopian/status/68071618055901184',
31
- { channel: '#foo', nick: 'bar' })).
32
- should be_empty
36
+ it 'should not run without credentials set' do
37
+ bot = make_bot(Cinch::Plugins::TwitterStatus)
38
+ get_replies(make_message(bot, 'https://twitter.com/weirdo513/status/344186643609174016',
39
+ { channel: '#foo', nick: 'bar' }))
40
+ end
33
41
  end
34
42
 
35
- it 'should not run without credentials set' do
36
- bot = make_bot(Cinch::Plugins::TwitterStatus)
37
- get_replies(make_message(bot, 'https://twitter.com/weirdo513/status/344186643609174016',
38
- { channel: '#foo', nick: 'bar' }))
43
+ describe "Watchers" do
44
+ # FIXME: cinch-test does not allow timers to function
45
+ it 'should not post tweets that already existed when the bot was started' do
46
+ sleep 20
47
+ get_replies(make_message(@bot, 'https://twitter.com/weirdo513/status/344186643609174016',
48
+ { channel: '#foo', nick: 'bar' }))
49
+ end
39
50
  end
40
51
  end
41
52
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cinch-twitterstatus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-14 00:00:00.000000000 Z
12
+ date: 2013-08-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -91,6 +91,22 @@ dependencies:
91
91
  - - ~>
92
92
  - !ruby/object:Gem::Version
93
93
  version: 4.8.1
94
+ - !ruby/object:Gem::Dependency
95
+ name: cinch
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 2.0.0
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 2.0.0
94
110
  description: Cinch IRC bot Plugin that access the Twitter API to post the content
95
111
  of linked twitter statuses to the channel.
96
112
  email:
@@ -112,7 +128,8 @@ files:
112
128
  - spec/cinch-twitterstatus_spec.rb
113
129
  - spec/spec_helper.rb
114
130
  homepage: https://github.com/bhaberer/cinch-twitterstatus
115
- licenses: []
131
+ licenses:
132
+ - MIT
116
133
  post_install_message:
117
134
  rdoc_options: []
118
135
  require_paths: