Chizuru 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 651cb625e157a022ce6c852bed6816a1932b6786
4
+ data.tar.gz: 120c1ec62086b528b420644a70e0b30dc87d2fe6
5
+ SHA512:
6
+ metadata.gz: fe34ea4c3fdfca1d4faa4326707c815fbc0ca9d0da5e714aaa3042e567bb85490dc41743c45aa57e4f945905668f434db268d2074d522c13d0fccdd8e6a1d9d6
7
+ data.tar.gz: 8aadd02228f3b1955c03a3dd3d47a6d8650a89bee7edfe2217cb0c52f2878e04f590dde2c4ed220435f631a46a8dc0c82dcac4b2bd0af747c3f340fb41323e9c
data/Chizuru.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'Chizuru/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "Chizuru"
8
+ spec.version = Chizuru::VERSION
9
+ spec.authors = ["mayth"]
10
+ spec.email = ["chimeaquas@hotmail.com"]
11
+ spec.description = %q{Twitter bot framework}
12
+ spec.summary = %q{Twitter bot frameowrk}
13
+ spec.homepage = "https://github.com/mayth/Chizuru"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_dependency 'oauth'
24
+ spec.add_dependency 'yajl-ruby'
25
+ end
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in Chizuru.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,21 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ Chizuru (0.0.2)
5
+ oauth
6
+ yajl-ruby
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ oauth (0.4.7)
12
+ rake (10.0.4)
13
+ yajl-ruby (1.1.0)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ Chizuru!
20
+ bundler (~> 1.3)
21
+ rake
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 mayth
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,62 @@
1
+ Chizuru
2
+ =======
3
+ re-structured twitter bot framework
4
+
5
+ Installation
6
+ ============
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'Chizuru'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install Chizuru
19
+
20
+ How To
21
+ ======
22
+ Notice: See `test.rb`.
23
+
24
+ 1. Define `Consumer` and `Deliverer`.
25
+ 2. Acquire the consumer secret and access token, and save it. We use YAML file that have these keys: `consumer_key`, `consumer_secret`, `access_token`, `access_token_secret`.
26
+ 3. Prepare the source. We provide the default source that uses UserStreaming. If you decide not to use it, you must define the source class that is inherited from `Chizuru::Source`.
27
+ 4. Configure the bot. Use `Chizuru::Bot.configure`.
28
+ 5. Start it!
29
+
30
+ Basic Structure
31
+ ===============
32
+
33
+ Source
34
+ ------
35
+ **Source** provides the tweets. We provide `UserStream` class. It uses UserStreaming.
36
+
37
+ Consumer
38
+ --------
39
+ **Consumer** receives tweets.
40
+ Consumer must be inherited from `Chizuru::Consumer`, and must have `receive(data)` method.
41
+
42
+ Deliverer
43
+ ---------
44
+ **Deliverer** posts tweets.
45
+ `Deliverer` must have `deliver(data)` method.
46
+
47
+ Contributing to Chizuru
48
+ =======================
49
+
50
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
51
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
52
+ * Fork the project.
53
+ * Start a feature/bugfix branch.
54
+ * Commit and push until you are happy with your contribution.
55
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
56
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
57
+
58
+ Copyright
59
+ =========
60
+ Copyright (c) 2013 mayth.
61
+
62
+ Chizuru is licensed under the MIT License. See LICENSE.txt.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/chizuru.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "chizuru/version"
2
+ require 'chizuru/credential'
3
+ require 'chizuru/consumer'
4
+ require 'chizuru/provider'
5
+ require 'chizuru/source'
6
+ require 'chizuru/user_stream'
7
+ require 'chizuru/bot'
@@ -0,0 +1,44 @@
1
+ require 'chizuru/credential'
2
+
3
+ module Chizuru
4
+ class ConsumerHelper
5
+ attr_reader :consumer, :credential
6
+
7
+ def initialize(cons, credential)
8
+ @consumer = cons
9
+ @credential = credential
10
+ end
11
+
12
+ def deliverer(deliv)
13
+ @consumer.add_deliverer(deliv)
14
+ end
15
+ end
16
+
17
+ class Bot
18
+ attr_accessor :src, :credential, :provider
19
+ def self.configure(cred_path, &block)
20
+ bot = Bot.new
21
+ bot.credential = Credential.new(cred_path)
22
+ bot.provider = Provider.new
23
+ bot.instance_eval &block
24
+ bot
25
+ end
26
+
27
+ def consumer(cons, *init_args, &block)
28
+ if cons.instance_of? Class
29
+ cons = cons.new(*init_args)
30
+ end
31
+ ch = ConsumerHelper.new(cons, credential)
32
+ ch.instance_eval &block
33
+ provider.add_consumer(ch.consumer)
34
+ end
35
+
36
+ def source(src)
37
+ @source = src
38
+ end
39
+
40
+ def start
41
+ @source.start
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,34 @@
1
+ module Chizuru
2
+ class Consumer
3
+ def initialize
4
+ @deliverers = []
5
+ @queue = Queue.new
6
+ Thread.new do
7
+ while true
8
+ data = @queue.deq
9
+ dispatch(data)
10
+ end
11
+ end
12
+ end
13
+
14
+ def add_deliverer(deliverer)
15
+ @deliverers << deliverer
16
+ end
17
+
18
+ def remove_deliverer(deliverer)
19
+ @deliverers.delete(deliverer)
20
+ end
21
+
22
+ def deliver(data)
23
+ @queue.enq data
24
+ end
25
+
26
+ def dispatch(data)
27
+ @deliverers.each do |deliverer|
28
+ Thread.new do
29
+ deliverer.deliver(data)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,18 @@
1
+ require 'yaml'
2
+
3
+ module Chizuru
4
+ class Credential
5
+ attr_reader :consumer, :access_token
6
+ def initialize(path)
7
+ cred = YAML.load_file(path)
8
+ @consumer = OAuth::Consumer.new(
9
+ cred['consumer_key'],
10
+ cred['consumer_secret'],
11
+ site: 'https://api.twitter.com/1.1')
12
+ @access_token = OAuth::AccessToken.new(
13
+ consumer,
14
+ cred['access_token'],
15
+ cred['access_token_secret'])
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,36 @@
1
+ require 'thread'
2
+
3
+ module Chizuru
4
+ class Provider
5
+ def initialize
6
+ @consumers = []
7
+ @queue = Queue.new
8
+ Thread.new do
9
+ while (true)
10
+ data = @queue.deq
11
+ dispatch(data)
12
+ end
13
+ end
14
+ end
15
+
16
+ def add_consumer(cons)
17
+ @consumers << cons
18
+ end
19
+
20
+ def remove_consumer(cons)
21
+ @consumers.delete(cons)
22
+ end
23
+
24
+ def receive(data)
25
+ @queue.enq data
26
+ end
27
+
28
+ def dispatch(data)
29
+ @consumers.each do |consumer|
30
+ Thread.new do
31
+ consumer.receive(data)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,8 @@
1
+ module Chizuru
2
+ class Source
3
+ def initialize(provider)
4
+ raise ArgumentError unless provider
5
+ @provider = provider
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,64 @@
1
+ require 'oauth'
2
+ require 'yaml'
3
+ require 'yajl'
4
+
5
+ require 'chizuru/source'
6
+
7
+ module Chizuru
8
+ class UserStream < Source
9
+ DEFAULT_USER_AGENT = 'TwitterBot/1.0 (Based on Chizuru)'
10
+
11
+ def initialize(provider, cred, screen_name, ca_file_path, user_agent = DEFAULT_USER_AGENT)
12
+ super(provider)
13
+ raise ArgumentError unless cred
14
+ @credential = cred
15
+ raise ArgumentError unless screen_name
16
+ @screen_name = screen_name
17
+ raise ArgumentError unless ca_file_path
18
+ @ca_file = ca_file_path
19
+ @user_agent = user_agent
20
+ end
21
+
22
+ def start
23
+ puts '[UserStream] Start Streaming'
24
+ connect do |json|
25
+ @provider.receive(json)
26
+ end
27
+ end
28
+
29
+ def connect
30
+ uri = URI.parse("https://userstream.twitter.com/2/user.json?track=#{@screen_name}")
31
+
32
+ https = Net::HTTP.new(uri.host, uri.port)
33
+ https.use_ssl = true
34
+ https.ca_file = @ca_file
35
+ https.verify_mode = OpenSSL::SSL::VERIFY_PEER
36
+ https.verify_depth = 5
37
+
38
+ https.start do |https|
39
+ request = Net::HTTP::Get.new(uri.request_uri,
40
+ "User-Agent" => @user_agent,
41
+ "Accept-Encoding" => "identity")
42
+ request.oauth!(https, @credential.consumer, @credential.access_token)
43
+
44
+ buf = ""
45
+ https.request(request) do |response|
46
+ response.read_body do |chunk|
47
+ buf << chunk
48
+ while ((line = buf[/.+?(\r\n)+/m]) != nil)
49
+ begin
50
+ buf.sub!(line, "")
51
+ line.strip!
52
+ status = Yajl::Parser.parse(line)
53
+ rescue
54
+ break
55
+ end
56
+
57
+ yield status
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module Chizuru
2
+ VERSION = "0.0.2"
3
+ end
data/test.rb ADDED
@@ -0,0 +1,72 @@
1
+ require './provider'
2
+ require './consumer'
3
+ require './credential'
4
+ require './user_stream'
5
+ require './bot'
6
+
7
+ class SimpleConsumer < Chizuru::Consumer
8
+ def receive(data)
9
+ deliver(data)
10
+ end
11
+ end
12
+
13
+ class MeitanConsumer < Chizuru::Consumer
14
+ HASHTAG_CANDIDATE_CHAR = ('a'..'z').to_a
15
+ HASHTAG_LENGTH = 5
16
+ def receive(data)
17
+ return unless data['text']
18
+ if data['text'].include?('めいたん')
19
+ deliver({status: "@#{data['user']['screen_name']} めいたんじゃない! ##{HASHTAG_CANDIDATE_CHAR.sample(HASHTAG_LENGTH).join}", in_reply_to: data})
20
+ end
21
+ end
22
+ end
23
+
24
+ class EchoDeliverer
25
+ def deliver(data)
26
+ p data
27
+ end
28
+ end
29
+
30
+ class LoggingDeliverer
31
+ def initialize(log_file)
32
+ @log_file = log_file
33
+ end
34
+
35
+ def deliver(data)
36
+ open(@log_file, 'a:utf-8') do |file|
37
+ file.puts(data.inspect)
38
+ end
39
+ end
40
+ end
41
+
42
+ class TweetDeliverer
43
+ def initialize(cred)
44
+ @credential = cred
45
+ end
46
+
47
+ def deliver(data)
48
+ opts = {status: data[:status]}
49
+ if data[:in_reply_to]
50
+ opts[:in_reply_to_status_id] = data[:in_reply_to]['id']
51
+ end
52
+ @credential.access_token.post(
53
+ '/statuses/update.json', opts)
54
+ end
55
+ end
56
+
57
+ bot = Chizuru::Bot.configure 'credential.yaml' do
58
+ source Chizuru::UserStream.new(provider, credential, 'meitanbot', 'cert', 'meitanbot/2.0 (Chizuru/1.0)')
59
+ echo_deliv = EchoDeliverer.new
60
+ log_deliv = LoggingDeliverer.new('./stream.log')
61
+ consumer SimpleConsumer do
62
+ deliverer echo_deliv
63
+ deliverer log_deliv
64
+ end
65
+
66
+ consumer MeitanConsumer do
67
+ deliverer echo_deliv
68
+ deliverer TweetDeliverer.new(credential)
69
+ end
70
+ end
71
+
72
+ bot.start
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Chizuru
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - mayth
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: oauth
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'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yajl-ruby
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Twitter bot framework
70
+ email:
71
+ - chimeaquas@hotmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Chizuru.gemspec
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/chizuru.rb
84
+ - lib/chizuru/bot.rb
85
+ - lib/chizuru/consumer.rb
86
+ - lib/chizuru/credential.rb
87
+ - lib/chizuru/provider.rb
88
+ - lib/chizuru/source.rb
89
+ - lib/chizuru/user_stream.rb
90
+ - lib/chizuru/version.rb
91
+ - test.rb
92
+ homepage: https://github.com/mayth/Chizuru
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.0.2
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Twitter bot frameowrk
116
+ test_files: []