twitter_ebooks 3.0.9 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3f4312e16bb18ae646bbca7559d3044ecc7b00f7
4
- data.tar.gz: 9fdcb9b3eb8cf9fc4449699ed20661a8b5ef8d2a
3
+ metadata.gz: 0bf168a080a0aabf939715c3dcb59655fca83463
4
+ data.tar.gz: 88ddb4ead32b0ca21db59b6edb4dcd97cf0d0594
5
5
  SHA512:
6
- metadata.gz: 39aee9bc75ffe627b24d1f6ae29f7bfd1c3093e3295dfebf5fdafb67b8ed649f417811112c5086f50449bb0a21ee899e7e3a5896e76a0de32a524da548e05a39
7
- data.tar.gz: 8136aec89545928bd5a04dd94fd14c078752a913f098a32a6ee96fd2c065f9d03ed14dcdc32550ce17d9f3815aaaee20153b2e24f617903e34502faa85e19a3b
6
+ metadata.gz: 13873b6ea1ba04b79e0b5b44e735aa41a97b8aeaee798eee2e3cc43795dabb8bc4149ecc37418d341818d671bee7fd91f10316d19e53c941a8d652fc7dae1cc7
7
+ data.tar.gz: 48b0e54e02267df3e5cc12c7a75d2deea62adfeaed9cf1e8274b6e30917cda55041f428ff3cd5bd5cf62eb1f8c84fe0880b1bb002e0c0db650892885ff221978
data/bin/ebooks CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  require 'twitter_ebooks'
5
5
  require 'ostruct'
6
+ require 'fileutils'
6
7
 
7
8
  module Ebooks::Util
8
9
  def pretty_exception(e)
@@ -144,6 +144,8 @@ module Ebooks
144
144
  attr_accessor :access_token
145
145
  # @return [String] OAuth access secret from `ebooks auth`
146
146
  attr_accessor :access_token_secret
147
+ # @return [Twitter::User] Twitter user object of bot
148
+ attr_accessor :user
147
149
  # @return [String] Twitter username of bot
148
150
  attr_accessor :username
149
151
  # @return [Array<String>] list of usernames to block on contact
@@ -247,31 +249,18 @@ module Ebooks
247
249
  # Receive an event from the twitter stream
248
250
  # @param ev [Object] Twitter streaming event
249
251
  def receive_event(ev)
250
- if ev.is_a? Array # Initial array sent on first connection
252
+ case ev
253
+ when Array # Initial array sent on first connection
251
254
  log "Online!"
255
+ fire(:connect, ev)
252
256
  return
253
- end
254
-
255
- if ev.is_a? Twitter::DirectMessage
256
- return if ev.sender.screen_name.downcase == @username.downcase # Don't reply to self
257
+ when Twitter::DirectMessage
258
+ return if ev.sender.id == @user.id # Don't reply to self
257
259
  log "DM from @#{ev.sender.screen_name}: #{ev.text}"
258
260
  fire(:message, ev)
259
-
260
- elsif ev.respond_to?(:name)
261
- if ev.name == :follow
262
- return if ev.source.screen_name.downcase == @username.downcase
263
- log "Followed by #{ev.source.screen_name}"
264
- fire(:follow, ev.source)
265
-
266
- elsif ev.name == :favorite || ev.name == :unfavorite
267
- return if ev.source.screen_name.downcase == @username.downcase # Ignore our own favorites
268
- log "@#{ev.source.screen_name} #{ev.name.to_s}d: #{ev.target_object.text}"
269
- fire(ev.name, ev.source, ev.target_object)
270
- end
271
-
272
- elsif ev.is_a? Twitter::Tweet
261
+ when Twitter::Tweet
273
262
  return unless ev.text # If it's not a text-containing tweet, ignore it
274
- return if ev.user.screen_name.downcase == @username.downcase # Ignore our own tweets
263
+ return if ev.user.id == @user.id # Ignore our own tweets
275
264
 
276
265
  meta = meta(ev)
277
266
 
@@ -295,15 +284,34 @@ module Ebooks
295
284
  else
296
285
  fire(:timeline, ev)
297
286
  end
298
-
299
- elsif ev.is_a?(Twitter::Streaming::DeletedTweet) ||
300
- ev.is_a?(Twitter::Streaming::Event)
301
- # pass
287
+ when Twitter::Streaming::Event
288
+ case ev.name
289
+ when :follow
290
+ return if ev.source.id == @user.id
291
+ log "Followed by #{ev.source.screen_name}"
292
+ fire(:follow, ev.source)
293
+ when :favorite, :unfavorite
294
+ return if ev.source.id == @user.id # Ignore our own favorites
295
+ log "@#{ev.source.screen_name} #{ev.name.to_s}d: #{ev.target_object.text}"
296
+ fire(ev.name, ev.source, ev.target_object)
297
+ when :user_update
298
+ update_myself ev.source
299
+ end
300
+ when Twitter::Streaming::DeletedTweet
301
+ # Pass
302
302
  else
303
303
  log ev
304
304
  end
305
305
  end
306
306
 
307
+ # Updates @user and calls on_user_update.
308
+ def update_myself(new_me=twitter.user)
309
+ @user = new_me if @user.nil? || new_me.id == @user.id
310
+ @username = @user.screen_name
311
+ log 'User information updated'
312
+ fire(:user_update)
313
+ end
314
+
307
315
  # Configures client and fires startup event
308
316
  def prepare
309
317
  # Sanity check
@@ -323,12 +331,12 @@ module Ebooks
323
331
  exit 1
324
332
  end
325
333
 
326
- real_name = twitter.user.screen_name
327
-
328
- if real_name != @username
329
- log "connected to @#{real_name}-- please update config to match Twitter account name"
330
- @username = real_name
331
- end
334
+ # Save old name
335
+ old_name = username
336
+ # Load user object and actual username
337
+ update_myself
338
+ # Warn about mismatches unless it was clearly intentional
339
+ log "warning: bot expected to be @#{old_name} but connected to @#{username}" unless username == old_name || old_name.empty?
332
340
 
333
341
  fire(:startup)
334
342
  end
@@ -71,7 +71,6 @@ module Ebooks
71
71
  start = site[0]
72
72
 
73
73
  site[1].shuffle.each do |alt|
74
- start, alt = site[0], site[1].sample
75
74
  verbatim << @sentences[alt[0]]
76
75
  suffix = @sentences[alt[0]][alt[1]..-1]
77
76
  potential = tikis[0..start+1] + suffix
@@ -1,3 +1,3 @@
1
1
  module Ebooks
2
- VERSION = "3.0.9"
2
+ VERSION = "3.1.0"
3
3
  end
data/spec/bot_spec.rb CHANGED
@@ -24,8 +24,17 @@ end
24
24
 
25
25
  module Ebooks::Test
26
26
  # Generates a random twitter id
27
- def twitter_id
28
- (rand*10**18).to_i
27
+ # Or a non-random one, given a string.
28
+ def twitter_id(seed = nil)
29
+ if seed.nil?
30
+ (rand*10**18).to_i
31
+ else
32
+ id = 1
33
+ seed.downcase.each_byte do |byte|
34
+ id *= byte/10
35
+ end
36
+ id
37
+ end
29
38
  end
30
39
 
31
40
  # Creates a mock direct message
@@ -33,7 +42,7 @@ module Ebooks::Test
33
42
  # @param text DM content
34
43
  def mock_dm(username, text)
35
44
  Twitter::DirectMessage.new(id: twitter_id,
36
- sender: { id: twitter_id, screen_name: username},
45
+ sender: { id: twitter_id(username), screen_name: username},
37
46
  text: text)
38
47
  end
39
48
 
@@ -45,7 +54,7 @@ module Ebooks::Test
45
54
  tweet = Twitter::Tweet.new({
46
55
  id: twitter_id,
47
56
  in_reply_to_status_id: 'mock-link',
48
- user: { id: twitter_id, screen_name: username },
57
+ user: { id: twitter_id(username), screen_name: username },
49
58
  text: text,
50
59
  created_at: Time.now.to_s,
51
60
  entities: {
@@ -58,14 +67,21 @@ module Ebooks::Test
58
67
  tweet
59
68
  end
60
69
 
70
+ # Creates a mock user
71
+ def mock_user(username)
72
+ Twitter::User.new(id: twitter_id(username), screen_name: username)
73
+ end
74
+
61
75
  def twitter_spy(bot)
62
76
  twitter = spy("twitter")
63
77
  allow(twitter).to receive(:update).and_return(mock_tweet(bot.username, "test tweet"))
78
+ allow(twitter).to receive(:user).with(no_args).and_return(mock_user(bot.username))
64
79
  twitter
65
80
  end
66
81
 
67
82
  def simulate(bot, &b)
68
83
  bot.twitter = twitter_spy(bot)
84
+ bot.update_myself # Usually called in prepare
69
85
  b.call
70
86
  end
71
87
 
@@ -95,6 +111,13 @@ describe Ebooks::Bot do
95
111
  end
96
112
  end
97
113
 
114
+ it "ignores its own dms" do
115
+ simulate(bot) do
116
+ expect(bot).to_not receive(:on_message)
117
+ bot.receive_event(mock_dm("Test_Ebooks", "why am I talking to myself"))
118
+ end
119
+ end
120
+
98
121
  it "responds to mentions" do
99
122
  simulate(bot) do
100
123
  bot.receive_event(mock_tweet("m1sp", "@test_ebooks this is a mention"))
@@ -102,6 +125,14 @@ describe Ebooks::Bot do
102
125
  end
103
126
  end
104
127
 
128
+ it "ignores its own mentions" do
129
+ simulate(bot) do
130
+ expect(bot).to_not receive(:on_mention)
131
+ expect(bot).to_not receive(:on_timeline)
132
+ bot.receive_event(mock_tweet("Test_Ebooks", "@m1sp i think that @test_ebooks is best bot"))
133
+ end
134
+ end
135
+
105
136
  it "responds to timeline tweets" do
106
137
  simulate(bot) do
107
138
  bot.receive_event(mock_tweet("m1sp", "some excellent tweet"))
@@ -109,6 +140,13 @@ describe Ebooks::Bot do
109
140
  end
110
141
  end
111
142
 
143
+ it "ignores its own timeline tweets" do
144
+ simulate(bot) do
145
+ expect(bot).to_not receive(:on_timeline)
146
+ bot.receive_event(mock_tweet("Test_Ebooks", "pudding is cute"))
147
+ end
148
+ end
149
+
112
150
  it "links tweets to conversations correctly" do
113
151
  tweet1 = mock_tweet("m1sp", "tweet 1", id: 1, in_reply_to_status_id: nil)
114
152
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twitter_ebooks
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.9
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaiden Mispy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-12 00:00:00.000000000 Z
11
+ date: 2015-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -278,7 +278,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
278
278
  version: '0'
279
279
  requirements: []
280
280
  rubyforge_project:
281
- rubygems_version: 2.4.5
281
+ rubygems_version: 2.4.6
282
282
  signing_key:
283
283
  specification_version: 4
284
284
  summary: Markov chains for all your friends~