ruboty-tweetstream 0.0.3 → 0.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: 6a6c74e08390f08c9dc6d19475b478b419bc2c3d
4
- data.tar.gz: ce85993be83303e4ad8cff5173822695b63aac58
3
+ metadata.gz: 82fd6a29c2eb198bc028cdaf0cc7795460543646
4
+ data.tar.gz: 0706a612363b5f0f7ae1dab7c8ba818841e2f8da
5
5
  SHA512:
6
- metadata.gz: a0c58a8e19a54f3668fb48513c7d5998db368d014dc8845b163ab9c40a225666d70155b3b2c4b33325f644276832d1ec2f9d162b89073b0415806a3b688dce11
7
- data.tar.gz: a3c587aa48b6fff6e7ff1120a6872195cdf2a500cf85066f81da18004522a03d2dc1d4c231da8f7871b5c0e18cec382cb1dc9effddda723040afe7cdeb8bc19e
6
+ metadata.gz: 2591f7c7baea9dd4fee1ea2b1dbe3f0da0ce41227ca003ab005b8b0ddbf6b01ed65ba632323d249ba9fa793b99fe85e5139673a5a0ff6bc5490a0850db21a859
7
+ data.tar.gz: d2f7fb74fbd96ec6e7c7ece17de47dcbc2a17ee1a8a8d7cb55bf26997fdfc9baf20639d3e75db666689558d21e54f9c9bc17e20402ab4d6462116e1986cb98ed
data/README.md CHANGED
@@ -13,29 +13,30 @@ gem 'ruboty-tweetstream'
13
13
  ## Usage
14
14
 
15
15
  ```
16
- @ruboty add ts @<username>
17
- @ruboty delete ts @<username>
18
- @ruboty list ts
16
+ @ruboty add stream @<username>
17
+ @ruboty delete stream <id>
18
+ @ruboty list streams
19
+ @ruboty list streams --all
19
20
  ```
20
21
 
21
22
  ## Example
22
23
 
23
24
  ```
24
- > @ruboty add ts @kaihar4
25
+ > @ruboty add stream @kaihar4
25
26
  '@kaihar4' stream is started.
26
- @kaihar4> テスト1
27
- @kaihar4> テスト2
28
- > @ruboty list ts
29
- @kaihar4
30
- > @ruboty delete ts @kaihar4
31
- '@kaihar4' stream is stopped.
27
+ @kaihar4> test1
28
+ @kaihar4> test2
29
+ > @ruboty list streams
30
+ 123: @kaihar4
31
+ > @ruboty delete stream 123
32
+ '123' stream is stopped.
32
33
  ```
33
34
 
34
35
  ## Setup
35
36
 
36
37
  Ruboty::Tweetstream requires access tokens.
37
38
 
38
- Please get from [Here](https://apps.twitter.com/).
39
+ Please get keys from [here](https://apps.twitter.com/).
39
40
 
40
41
  ```
41
42
  export CONSUMER_KEY=
@@ -1,11 +1,25 @@
1
1
  require 'twitter'
2
+ require 'xrc'
3
+
4
+ Ruboty::Message.class_eval do
5
+ XMPP_PATERN = /\A(?:([^@]*)@)??([^@\/]*)(?:\/(.*?))?\z/
6
+
7
+ def channel
8
+ if XMPP_PATERN === from
9
+ # Get foobar@example.com in foobar@example.com/bot
10
+ Xrc::Jid.new(from).strip
11
+ else
12
+ from
13
+ end
14
+ end
15
+ end
2
16
 
3
17
  module Ruboty
4
18
  module Handlers
5
19
  class Tweetstream < Base
6
- on /add ts @(?<username>.+)/, name: 'add', description: 'Add a tweetstream'
7
- on /list ts\z/, name: 'list', description: 'Show all tweetstreams'
8
- on /delete ts @(?<username>.+)/, name: 'delete', description: 'Delete a tweetstream'
20
+ on /add stream @(?<username>.+)/, name: 'add', description: 'Add a tweetstream'
21
+ on /list streams(?<all_flag> --all)?\z/, name: 'list', description: 'Show all tweetstreams'
22
+ on /delete stream (?<id>.+)/, name: 'delete', description: 'Delete a tweetstream'
9
23
 
10
24
  def initialize(*args)
11
25
  super
@@ -13,55 +27,75 @@ module Ruboty
13
27
  end
14
28
 
15
29
  def add(message)
30
+ id = generate_id
16
31
  username = message[:username]
17
- if streams.has_key?(username)
32
+ channel = message.channel
33
+
34
+ if stream_exists?(username, channel)
18
35
  message.reply("'@#{username}' stream is already started.")
19
36
  else
20
37
  stream = Ruboty::Tweetstream::Stream.new(
21
38
  message.original.except(:robot).merge(
22
39
  username: username,
23
- id: twitter_id(username)
40
+ twitter_id: twitter_id(username),
41
+ channel: channel
24
42
  )
25
43
  )
26
44
 
27
45
  stream.start(robot)
28
- running_streams[username] = stream
46
+ running_streams[id] = stream
29
47
 
30
- streams[username] = stream.hash
48
+ streams[id] = stream.hash
31
49
  message.reply("'@#{username}' stream is started.")
32
50
  end
51
+ rescue Twitter::Error::Forbidden
52
+ message.reply('Unable to verify your credentials.')
33
53
  end
34
54
 
35
55
  def delete(message)
36
- username = message[:username]
37
- if streams.has_key?(username)
38
- running_streams[username].stop
39
- running_streams.delete(username)
56
+ id = message[:id].to_i
57
+ if streams.has_key?(id)
58
+ running_streams[id].stop
59
+ running_streams.delete(id)
40
60
 
41
- streams.delete(username)
42
- message.reply("'@#{username}' stream is stopped.")
61
+ streams.delete(id)
62
+ message.reply("'#{id}' stream is stopped.")
43
63
  else
44
- message.reply("'@#{username}' stream is not found.")
64
+ message.reply("'#{id}' stream is not found.")
45
65
  end
46
66
  end
47
67
 
48
68
  def list(message)
49
- if streams.empty?
69
+ sorted_streams = streams.sort_by {|_id, hash| hash[:username]}
70
+
71
+ if message[:all_flag]
72
+ stream_list = sorted_streams.map do |id, hash|
73
+ stream = "#{id}: @#{hash[:username]}"
74
+ stream << " (#{hash[:channel]})" unless hash[:channel].nil?
75
+ stream
76
+ end
77
+ else
78
+ stream_list = sorted_streams.map { |id, hash|
79
+ # Skip the channel that registered from other channel.
80
+ next unless hash[:channel] == message.channel
81
+
82
+ "#{id}: @#{hash[:username]}"
83
+ }.compact
84
+ end
85
+
86
+ if stream_list.empty?
50
87
  message.reply("The stream doesn't exist.")
51
88
  else
52
- stream_list = streams.map do |username, _hash|
53
- "@#{username}"
54
- end.join("\t")
55
- message.reply(stream_list, code: true)
89
+ message.reply(stream_list.join("\n"), code: true)
56
90
  end
57
91
  end
58
92
 
59
93
  def restart
60
- streams.each do |username, hash|
94
+ streams.each do |id, hash|
61
95
  stream = Ruboty::Tweetstream::Stream.new(hash)
62
96
 
63
97
  stream.start(robot)
64
- running_streams[username] = stream
98
+ running_streams[id] = stream
65
99
  end
66
100
  end
67
101
 
@@ -82,6 +116,20 @@ module Ruboty
82
116
  end
83
117
  client.user(username).id
84
118
  end
119
+
120
+ def generate_id
121
+ loop do
122
+ id = rand(1000)
123
+ break id unless streams.has_key?(id)
124
+ end
125
+ end
126
+
127
+ def stream_exists?(username, channel)
128
+ streams.each do |_id, hash|
129
+ return true if hash[:username] == username && hash[:channel] == channel
130
+ end
131
+ return false
132
+ end
85
133
  end
86
134
  end
87
135
  end
@@ -1,11 +1,9 @@
1
1
  require 'twitter'
2
2
 
3
- module Twitter
4
- class Tweet
5
- def is_pure_user_tweet?(username)
6
- self.in_reply_to_screen_name.nil? &&\
7
- !self.full_text.include?("@#{username}")
8
- end
3
+ Twitter::Tweet.class_eval do
4
+ def is_pure_user_tweet?(username)
5
+ self.in_reply_to_screen_name.nil? &&\
6
+ !self.full_text.include?("@#{username}")
9
7
  end
10
8
  end
11
9
 
@@ -27,10 +25,10 @@ module Ruboty
27
25
 
28
26
  def start(robot)
29
27
  @thread = Thread.start do
30
- @client.filter(follow: hash[:id].to_s) do |object|
28
+ @client.filter(follow: hash[:twitter_id].to_s) do |object|
31
29
  if object.is_a?(Twitter::Tweet) && object.is_pure_user_tweet?(hash[:username])
32
30
  Message.new(
33
- hash.except(:username, :id).merge(robot: robot)
31
+ hash.except(:username, :twitter_id).merge(robot: robot)
34
32
  ).reply("@#{hash[:username]}> #{object.full_text}")
35
33
  end
36
34
  end
@@ -1,5 +1,5 @@
1
1
  module Ruboty
2
2
  module Tweetstream
3
- VERSION = '0.0.3'
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
18
18
 
19
19
  spec.add_dependency 'ruboty'
20
20
  spec.add_dependency 'twitter'
21
+ spec.add_dependency 'xrc'
21
22
 
22
23
  spec.add_development_dependency 'bundler'
23
24
  spec.add_development_dependency 'rake'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruboty-tweetstream
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaihar4
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-19 00:00:00.000000000 Z
11
+ date: 2014-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruboty
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: xrc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement