ruboty-twitter_track 0.1.0 → 0.1.1

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: 332ecf6d38e4fcc8edf2677bc119ec27c5278bce
4
- data.tar.gz: 6581c64efec90384b6c63d94d3ebce48c7b0c925
3
+ metadata.gz: 6bcad5f258d6f12adc8bf514bb42b091e9110c73
4
+ data.tar.gz: feeac45bcda8da737ecd41ba628e274a10b57645
5
5
  SHA512:
6
- metadata.gz: eed785ff8df72589a7c020bce99bdac1ed7c5945dc94b54bf1995f29a5a4e9b8a359235f3cc9d2364f563b14dc821cd9453ba8346c8c4b5b70a1bf28c71a8e5f
7
- data.tar.gz: 2c1976543de864f235e14b7119380e70775565186aba3b88d9638e2b574f38ee1b83c7107aa7e666612bdceafcdbae188c375889b4e723fa80611384c9b42fbc
6
+ metadata.gz: 39c955ee2d2b30aae1ccdf8f853b3f37024bd67e5ff82750627431af3458f3d5a4696f868a8fe5f027e6ac0c4ded17fc1b95817484ade07164f70c997ea95dfd
7
+ data.tar.gz: ed6ff5dbc00eaf9267c0be105ce3ade7ee99e6c54941b1198c9c5b99318dbc27e4674a064febcff3272743024f3207b157753a3c6b1185c533028c0e8563b008
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Ruboty::TwitterTrack
2
2
 
3
- Ruboty handler to track terms in the twitter stream.
3
+ Ruboty handler to track certain words in the twitter stream.
4
4
 
5
5
  ## Installation
6
6
 
@@ -17,18 +17,18 @@ And then execute:
17
17
  ## Usage
18
18
 
19
19
  ```
20
- > @ruboty track tweets by ruboty
21
- Tracked 'ruboty'
20
+ > @ruboty twitter track by rubygems
21
+ @ruboty> Tracked 'rubygems'
22
22
 
23
23
  @ruboty> https://twitter.com/haccht/status/123456789
24
24
  @ruboty> https://twitter.com/haccht/status/123456790
25
25
  @ruboty> https://twitter.com/haccht/status/123456791
26
26
 
27
- > @ruboty list tweets tracking
28
- @haccht
27
+ > @ruboty twitter tracking
28
+ @ruboty> 'rubygems'
29
29
 
30
- > @ruboty untrack tweets by ruboty
31
- Untracked 'ruboty'
30
+ > @ruboty twitter untrack by rubygems
31
+ @ruboty> Untracked 'rubygems'
32
32
  ```
33
33
 
34
34
  ## Env
@@ -1,69 +1,73 @@
1
1
  module Ruboty
2
2
  module Handlers
3
3
  class TwitterTrack < Base
4
- on(/track tweets by (?<term>.+)\z/,
4
+ on(/twitter track by (?<term>.+)\z/,
5
5
  name: 'track',
6
- description: 'Track tweets in the twitter stream.')
6
+ description: 'Track the twitter stream by the certain term.')
7
7
 
8
- on(/untrack tweets by (?<term>.+)\z/,
8
+ on(/twitter untrack by (?<term>.+)\z/,
9
9
  name: 'untrack',
10
- description: 'Untrack tweets in the twitter stream.')
10
+ description: 'Untrack the twitter stream by the certain term.')
11
11
 
12
- on(/list tweets tracking\z/,
12
+ on(/twitter tracking\z/,
13
13
  name: 'tracking',
14
- description: 'List tracking tweets in the twitter stream.')
14
+ description: 'List tracking terms.')
15
15
 
16
16
  def initialize(*args)
17
17
  super
18
18
 
19
19
  @stream = Ruboty::TwitterTrack::Stream.new(robot)
20
- @stream.start(tracking_terms)
20
+ @stream.start(cache[:message], cache[:terms])
21
21
  end
22
22
 
23
23
  def track(message)
24
- message[:term].split(',').map { |e| e.strip }.each do |term|
25
- if tracking_terms.include?(term)
26
- message.reply("Already trackedd '#{term}'.")
27
- else
28
- tracking_terms << term
29
- message.reply("Tracked '#{term}'.")
30
- end
24
+ cache[:message] = message.original.except(:robot)
25
+
26
+ message[:term].split(',').each do |term|
27
+ words = term.strip.split(/\s+/).sort
28
+ cache[:terms].push(words).uniq!
31
29
  end
32
30
 
33
31
  begin
34
- @stream.restart(tracking_terms)
32
+ @stream.restart(cache[:message], cache[:terms])
33
+ message.reply("Done.")
35
34
  rescue Twitter::Error::Forbidden
36
35
  message.reply("Unable to verify your credentials.")
37
36
  end
38
37
  end
39
38
 
40
39
  def untrack(message)
41
- message[:term].split(',').map { |e| e.strip }.each do |term|
42
- if tracking_terms.include?(term)
43
- tracking_terms.delete(term)
44
- message.reply("Untracked '#{term}'.")
45
- else
46
- message.reply("'#{term}' has not tracked.")
47
- end
40
+ cache[:message] = message.original.except(:robot)
41
+
42
+ message[:term].split(',').each do |term|
43
+ words = term.strip.split(/\s+/).sort
44
+ cache[:terms].delete(words)
48
45
  end
49
46
 
50
47
  begin
51
- @stream.restart(tracking_terms)
48
+ @stream.restart(cache[:message], cache[:terms])
49
+ message.reply("Done.")
52
50
  rescue Twitter::Error::Forbidden
53
51
  message.reply("Unable to verify your credentials.")
54
52
  end
55
53
  end
56
54
 
57
55
  def tracking(message)
58
- if tracking_terms.empty?
59
- message.reply("tracking no terms.")
56
+ if cache[:terms].empty?
57
+ message.reply("Tracking no terms.")
60
58
  else
61
- tracking_terms.each { |term| message.reply(term, code:true) }
59
+ response = cache[:terms].map { |words| message.reply(words.join(' ') }.join("\n")
60
+ message.reply(response, code:true)
62
61
  end
63
62
  end
64
63
 
65
- def tracking_terms
66
- robot.brain.data[Ruboty::TwitterTrack::NAMESPACE] ||= []
64
+ def cache
65
+ unless robot.brain.data[Ruboty::TwitterTrack::NAMESPACE]
66
+ status = { message: nil, terms: [] }
67
+ robot.brain.data[Ruboty::TwitterTrack::NAMESPACE] = status
68
+ end
69
+
70
+ robot.brain.data[Ruboty::TwitterTrack::NAMESPACE]
67
71
  end
68
72
  end
69
73
  end
@@ -34,20 +34,22 @@ module Ruboty
34
34
  end
35
35
  end
36
36
 
37
- def start(terms)
37
+ def start(message, terms)
38
38
  return if terms.empty?
39
39
 
40
40
  Thread.start do
41
- @client.track(*terms) do |object|
42
- message = Message.new(robot: @robot)
43
- message.reply(u(object))
41
+ query = terms.map { |w| w.join(' ') }
42
+ @client.track(*query) do |object|
43
+ Message.new(message.merge(robot: @robot)).reply(u(object))
44
44
  end
45
45
  end
46
46
  end
47
47
 
48
- def restart(terms)
49
- return start(terms) unless @client.stream
50
- @client.stream.update(params: {:track => terms.join(',')})
48
+ def restart(message, terms)
49
+ return start(message, terms) unless @client.stream
50
+
51
+ query = terms.map { |w| w.join(' ') }
52
+ @client.stream.update(params: {:track => query.join(',')})
51
53
  end
52
54
 
53
55
  private
@@ -1,5 +1,5 @@
1
1
  module Ruboty
2
2
  module TwitterTrack
3
- VERSION = '0.1.0'
3
+ VERSION = '0.1.1'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruboty-twitter_track
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - haccht
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-24 00:00:00.000000000 Z
11
+ date: 2015-12-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruboty