ruboty-tweetstream 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0daa0e09b9b02bb202c5758b7e7da75bf62b0754
4
+ data.tar.gz: 910e27925876cef8b523fdadd86304c451647d22
5
+ SHA512:
6
+ metadata.gz: 4cf3b110e2d90087382c91a18d03ab9d869b2020564123028676123e1859793d44785a24534d6ab5291499bfe3b5dd608232224a1eaa305892fe16ca9aee0a20
7
+ data.tar.gz: 89feb0ccf67d56db00fa0a87fe14884ca5feeb9c09ad2aaf0a698e3138b7fd9940ac7c5ca4a1585e1398acfd5400f9d697a80a63bfad881b2c4fa770af5155b2
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruboty-tweetstream.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 kaihar4
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # Ruboty::Tweetstream
2
+
3
+ Ruboty handler to stream specified user's tweet from Twitter.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ruboty-tweetstream'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```
16
+ @ruboty add ts @<username>
17
+ @ruboty delete ts @<username>
18
+ @ruboty list ts
19
+ ```
20
+
21
+ ## Example
22
+
23
+ ```
24
+ > @ruboty add ts @kaihar4
25
+ '@kaihar4' stream is started.
26
+ @kaihar4> テスト1
27
+ @kaihar4> テスト2
28
+ > @ruboty list ts
29
+ @kaihar4 -> id:2703995576, started_at:2014-11-18 17:00:00 +0900
30
+ > @ruboty delete ts @kaihar4
31
+ '@kaihar4' stream is stopped.
32
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,84 @@
1
+ require 'twitter'
2
+
3
+ module Ruboty
4
+ module Handlers
5
+ class Tweetstream < Base
6
+ on /add ts @(?<username>.+)/, name: 'add', description: 'Add a tweetstream'
7
+ on /list ts/, name: 'list', description: 'Show all tweetstreams'
8
+ on /delete ts @(?<username>.+)/, name: 'delete', description: 'Delete a tweetstream'
9
+
10
+ def initialize(*args)
11
+ super
12
+ restart
13
+ end
14
+
15
+ def add(message)
16
+ username = message[:username]
17
+ stream = Ruboty::Tweetstream::Stream.new(
18
+ message.original.except(:robot).merge(
19
+ username: username,
20
+ id: twitter_id(username)
21
+ )
22
+ )
23
+ stream.start(robot)
24
+ running_streams[username] = stream
25
+
26
+ streams[username] = stream.hash
27
+ message.reply("'@#{username}' stream is started.")
28
+ end
29
+
30
+ def delete(message)
31
+ username = message[:username]
32
+ if streams.has_key?(username)
33
+ running_streams[username].stop
34
+ running_streams.delete(username)
35
+
36
+ streams.delete(username)
37
+ message.reply("'@#{username}' stream is stopped.")
38
+ else
39
+ message.reply("'@#{username}' stream is not found.")
40
+ end
41
+ end
42
+
43
+ def list(message)
44
+ body =
45
+ if streams.empty?
46
+ "The stream doesn't exist."
47
+ else
48
+ streams.map do |username, hash|
49
+ "@#{username} -> id:#{hash[:id]}"
50
+ end.join("\n")
51
+ end
52
+
53
+ message.reply(body)
54
+ end
55
+
56
+ def restart
57
+ streams.each do |username, hash|
58
+ stream = Ruboty::Tweetstream::Stream.new(hash)
59
+
60
+ stream.start(robot)
61
+ running_streams[username] = stream
62
+ end
63
+ end
64
+
65
+ def streams
66
+ robot.brain.data[Ruboty::Tweetstream::NAMESPACE] ||= {}
67
+ end
68
+
69
+ def running_streams
70
+ @running_streams ||= {}
71
+ end
72
+
73
+ def twitter_id(username)
74
+ client = Twitter::REST::Client.new do |config|
75
+ config.consumer_key = Ruboty::Tweetstream::CONSUMER_KEY
76
+ config.consumer_secret = Ruboty::Tweetstream::CONSUMER_SECRET
77
+ config.access_token = Ruboty::Tweetstream::ACCESS_TOKEN
78
+ config.access_token_secret = Ruboty::Tweetstream::ACCESS_TOKEN_SECRET
79
+ end
80
+ client.user(username).id
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,14 @@
1
+ require 'ruboty'
2
+ require 'ruboty/tweetstream/stream'
3
+ require 'ruboty/handlers/tweetstream'
4
+
5
+ module Ruboty
6
+ module Tweetstream
7
+ NAMESPACE = 'tweetstream'
8
+
9
+ CONSUMER_KEY = ENV['CONSUMER_KEY']
10
+ CONSUMER_SECRET = ENV['CONSUMER_SECRET']
11
+ ACCESS_TOKEN = ENV['ACCESS_TOKEN']
12
+ ACCESS_TOKEN_SECRET = ENV['ACCESS_TOKEN_SECRET']
13
+ end
14
+ end
@@ -0,0 +1,34 @@
1
+ module Ruboty
2
+ module Tweetstream
3
+ class Stream
4
+ attr_reader :hash, :thread
5
+
6
+ def initialize(hash)
7
+ @hash = hash
8
+
9
+ @client = Twitter::Streaming::Client.new do |config|
10
+ config.consumer_key = CONSUMER_KEY
11
+ config.consumer_secret = CONSUMER_SECRET
12
+ config.access_token = ACCESS_TOKEN
13
+ config.access_token_secret = ACCESS_TOKEN_SECRET
14
+ end
15
+ end
16
+
17
+ def start(robot)
18
+ @thread = Thread.start do
19
+ @client.filter(follow: hash[:id].to_s) do |object|
20
+ if object.is_a?(Twitter::Tweet)
21
+ Message.new(
22
+ hash.except(:username, :id).merge(robot: robot)
23
+ ).reply("@#{hash[:username]}> #{ object.text}")
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ def stop
30
+ thread.kill
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ module Ruboty
2
+ module Tweetstream
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ $LOAD_PATH << File.expand_path('../lib', __FILE__)
2
+ require 'ruboty/tweetstream/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'ruboty-tweetstream'
6
+ spec.version = Ruboty::Tweetstream::VERSION
7
+ spec.authors = ['kaihar4']
8
+ spec.email = ['kaihar4@gmail.com']
9
+ spec.summary = "Ruboty handler to stream specified user's tweet from Twitter."
10
+ spec.description = spec.summary
11
+ spec.homepage = 'https://github.com/kaihar4/ruboty-tweetstream'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_dependency 'ruboty'
20
+ spec.add_dependency 'twitter'
21
+
22
+ spec.add_development_dependency 'bundler'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'pry'
25
+ spec.add_development_dependency 'awesome_print'
26
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruboty-tweetstream
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - kaihar4
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruboty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: twitter
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: awesome_print
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Ruboty handler to stream specified user's tweet from Twitter.
98
+ email:
99
+ - kaihar4@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - lib/ruboty/handlers/tweetstream.rb
110
+ - lib/ruboty/tweetstream.rb
111
+ - lib/ruboty/tweetstream/stream.rb
112
+ - lib/ruboty/tweetstream/version.rb
113
+ - ruboty-tweetstream.gemspec
114
+ homepage: https://github.com/kaihar4/ruboty-tweetstream
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.2.2
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: Ruboty handler to stream specified user's tweet from Twitter.
138
+ test_files: []