Chizuru 0.0.2 → 0.0.3
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 +4 -4
- data/Chizuru.gemspec +2 -0
- data/Gemfile.lock +1 -1
- data/README.md +8 -5
- data/lib/chizuru.rb +0 -1
- data/lib/chizuru/bot.rb +23 -2
- data/lib/chizuru/consumer.rb +8 -1
- data/lib/chizuru/credential.rb +9 -1
- data/lib/chizuru/provider.rb +8 -0
- data/lib/chizuru/source.rb +4 -0
- data/lib/chizuru/user_stream.rb +13 -0
- data/lib/chizuru/version.rb +2 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab93acb12743db1e5e5479b9f8499e65902f69d6
|
4
|
+
data.tar.gz: 16c0e2e9f28438e27c9a36035ebd240c57102ed5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff98c1d883b9aa4629a89a5a961d57dd11e86b5f4e6d03241c453d12ee526c52cfce2f5534cd5daae34067bc3d25a65fe4e8014a21552914ef64d3467c2f4500
|
7
|
+
data.tar.gz: 06596432c4b40ba775b88c61b6263a90ae6e97693934cca6f5c48d05f54e0c571b101cee5d369f8aa663ea9f99e698a34db5e99914cdb319bc37ba9ba032cd4e
|
data/Chizuru.gemspec
CHANGED
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
+
spec.has_rdoc = true
|
22
|
+
|
21
23
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
24
|
spec.add_development_dependency "rake"
|
23
25
|
spec.add_dependency 'oauth'
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -2,6 +2,8 @@ Chizuru
|
|
2
2
|
=======
|
3
3
|
re-structured twitter bot framework
|
4
4
|
|
5
|
+
On RubyGems.org: https://rubygems.org/gems/Chizuru
|
6
|
+
|
5
7
|
Installation
|
6
8
|
============
|
7
9
|
|
@@ -21,11 +23,12 @@ How To
|
|
21
23
|
======
|
22
24
|
Notice: See `test.rb`.
|
23
25
|
|
24
|
-
1.
|
25
|
-
2.
|
26
|
-
3.
|
27
|
-
4.
|
28
|
-
5.
|
26
|
+
1. Add `require 'chizuru'`.
|
27
|
+
2. Define `Consumer` and `Deliverer`.
|
28
|
+
3. 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`.
|
29
|
+
4. Prepare the source. We provide the default source that uses UserStreaming. If you decide to use it, add `require 'chizuru/user_stream'` and set the instance of `Chizuru::UserStream` as the source in the next step. Otherwise, you must define the source class that is inherited from `Chizuru::Source`.
|
30
|
+
5. Configure the bot. Use `Chizuru::Bot.configure`. Set the source and consumer.
|
31
|
+
6. Start it!
|
29
32
|
|
30
33
|
Basic Structure
|
31
34
|
===============
|
data/lib/chizuru.rb
CHANGED
data/lib/chizuru/bot.rb
CHANGED
@@ -1,21 +1,36 @@
|
|
1
1
|
require 'chizuru/credential'
|
2
2
|
|
3
3
|
module Chizuru
|
4
|
+
# Helper for DSL
|
4
5
|
class ConsumerHelper
|
5
|
-
|
6
|
+
# Gets the consumer.
|
7
|
+
attr_reader :consumer
|
8
|
+
# Gets the credentials.
|
9
|
+
attr_reader :credential
|
6
10
|
|
11
|
+
# Initializes an instance of ConsumerHelper.
|
12
|
+
# _cons_ is the consumer, _credential_ is the credential.
|
7
13
|
def initialize(cons, credential)
|
8
14
|
@consumer = cons
|
9
15
|
@credential = credential
|
10
16
|
end
|
11
17
|
|
18
|
+
# Add deliverer
|
12
19
|
def deliverer(deliv)
|
13
20
|
@consumer.add_deliverer(deliv)
|
14
21
|
end
|
15
22
|
end
|
16
23
|
|
24
|
+
# Represents a twitter-bot.
|
17
25
|
class Bot
|
18
|
-
|
26
|
+
# Gets or sets a source.
|
27
|
+
attr_accessor :src
|
28
|
+
# Gets or sets a provider.
|
29
|
+
attr_accessor :provider
|
30
|
+
# Gets or sets credentials. (This is given to the source.)
|
31
|
+
attr_accessor :credential
|
32
|
+
|
33
|
+
# Configures this bot.
|
19
34
|
def self.configure(cred_path, &block)
|
20
35
|
bot = Bot.new
|
21
36
|
bot.credential = Credential.new(cred_path)
|
@@ -24,6 +39,10 @@ module Chizuru
|
|
24
39
|
bot
|
25
40
|
end
|
26
41
|
|
42
|
+
# Adds a consumer.
|
43
|
+
#
|
44
|
+
# * If an instance of Consumer or its subclasses is given, it is used.
|
45
|
+
# * If Class is given, initialize its instance, and use it. In this case, the rest arguments are passed to the constructor of the given class.
|
27
46
|
def consumer(cons, *init_args, &block)
|
28
47
|
if cons.instance_of? Class
|
29
48
|
cons = cons.new(*init_args)
|
@@ -33,10 +52,12 @@ module Chizuru
|
|
33
52
|
provider.add_consumer(ch.consumer)
|
34
53
|
end
|
35
54
|
|
55
|
+
# Sets a source.
|
36
56
|
def source(src)
|
37
57
|
@source = src
|
38
58
|
end
|
39
59
|
|
60
|
+
# Starts this bot.
|
40
61
|
def start
|
41
62
|
@source.start
|
42
63
|
end
|
data/lib/chizuru/consumer.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
module Chizuru
|
2
|
+
# Represents a consumer.
|
3
|
+
# Consumer takes tweets from Source, and generates status and passes it to deliverers.
|
2
4
|
class Consumer
|
5
|
+
# Initializes an instance of Consumer and starts loop.
|
3
6
|
def initialize
|
4
7
|
@deliverers = []
|
5
8
|
@queue = Queue.new
|
@@ -11,18 +14,22 @@ module Chizuru
|
|
11
14
|
end
|
12
15
|
end
|
13
16
|
|
17
|
+
# Adds deliverer.
|
14
18
|
def add_deliverer(deliverer)
|
15
19
|
@deliverers << deliverer
|
16
20
|
end
|
17
|
-
|
21
|
+
|
22
|
+
# Removes a deliverer.
|
18
23
|
def remove_deliverer(deliverer)
|
19
24
|
@deliverers.delete(deliverer)
|
20
25
|
end
|
21
26
|
|
27
|
+
# On delivering a status.
|
22
28
|
def deliver(data)
|
23
29
|
@queue.enq data
|
24
30
|
end
|
25
31
|
|
32
|
+
# Dispatches a status to deliverers this has.
|
26
33
|
def dispatch(data)
|
27
34
|
@deliverers.each do |deliverer|
|
28
35
|
Thread.new do
|
data/lib/chizuru/credential.rb
CHANGED
@@ -1,8 +1,16 @@
|
|
1
1
|
require 'yaml'
|
2
2
|
|
3
3
|
module Chizuru
|
4
|
+
# Represents credentials.
|
4
5
|
class Credential
|
5
|
-
|
6
|
+
# Gets the consumer.
|
7
|
+
attr_reader :consumer
|
8
|
+
# Gets the access token.
|
9
|
+
attr_reader :access_token
|
10
|
+
|
11
|
+
# Initializes the credentials from the given file.
|
12
|
+
#
|
13
|
+
# The file must be YAML file, and it must have these keys: `consumer_key`, `consumer_secret`, `access_token`, and `access_token_secret`.
|
6
14
|
def initialize(path)
|
7
15
|
cred = YAML.load_file(path)
|
8
16
|
@consumer = OAuth::Consumer.new(
|
data/lib/chizuru/provider.rb
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
require 'thread'
|
2
2
|
|
3
3
|
module Chizuru
|
4
|
+
# Represents a tweets provider.
|
5
|
+
#
|
6
|
+
# Provider takes tweets from Source and provides them to Consumer.
|
4
7
|
class Provider
|
8
|
+
# Initializes an instance of Provider and starts the loop.
|
5
9
|
def initialize
|
6
10
|
@consumers = []
|
7
11
|
@queue = Queue.new
|
@@ -13,18 +17,22 @@ module Chizuru
|
|
13
17
|
end
|
14
18
|
end
|
15
19
|
|
20
|
+
# Adds a consumer.
|
16
21
|
def add_consumer(cons)
|
17
22
|
@consumers << cons
|
18
23
|
end
|
19
24
|
|
25
|
+
# Removes a consumer.
|
20
26
|
def remove_consumer(cons)
|
21
27
|
@consumers.delete(cons)
|
22
28
|
end
|
23
29
|
|
30
|
+
# On receiving a status from the source.
|
24
31
|
def receive(data)
|
25
32
|
@queue.enq data
|
26
33
|
end
|
27
34
|
|
35
|
+
# Dispatches the status to the consumers this has.
|
28
36
|
def dispatch(data)
|
29
37
|
@consumers.each do |consumer|
|
30
38
|
Thread.new do
|
data/lib/chizuru/source.rb
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
module Chizuru
|
2
|
+
# Represents a tweets source.
|
3
|
+
#
|
4
|
+
# Source provides events or statuses from Twitter API, or other any sources (for example, the log file of the streaming).
|
2
5
|
class Source
|
6
|
+
# Initializes an instance of Source with the given Provider.
|
3
7
|
def initialize(provider)
|
4
8
|
raise ArgumentError unless provider
|
5
9
|
@provider = provider
|
data/lib/chizuru/user_stream.rb
CHANGED
@@ -5,9 +5,18 @@ require 'yajl'
|
|
5
5
|
require 'chizuru/source'
|
6
6
|
|
7
7
|
module Chizuru
|
8
|
+
# UserStream takes events or statuses from UserStreaming API.
|
8
9
|
class UserStream < Source
|
10
|
+
# The default value of User-Agent.
|
9
11
|
DEFAULT_USER_AGENT = 'TwitterBot/1.0 (Based on Chizuru)'
|
10
12
|
|
13
|
+
# Initializes an instance of UserStream.
|
14
|
+
#
|
15
|
+
# * _provider_ is the provider that this will give the events or statuses.
|
16
|
+
# * _cred_ is the credentials.
|
17
|
+
# * _screen_name_ is the screen name of this bot.
|
18
|
+
# * _ca_file_path_ is the path for the certification file. It is required to establish the SSL connection with Twitter API.
|
19
|
+
# * _user_agent_ is the User-Agent. Twitter strongly recommends set the User-Agent that includes the version of the client. See https://dev.twitter.com/docs/streaming-apis/connecting#User_Agent
|
11
20
|
def initialize(provider, cred, screen_name, ca_file_path, user_agent = DEFAULT_USER_AGENT)
|
12
21
|
super(provider)
|
13
22
|
raise ArgumentError unless cred
|
@@ -19,6 +28,7 @@ module Chizuru
|
|
19
28
|
@user_agent = user_agent
|
20
29
|
end
|
21
30
|
|
31
|
+
# Starts the bot.
|
22
32
|
def start
|
23
33
|
puts '[UserStream] Start Streaming'
|
24
34
|
connect do |json|
|
@@ -26,6 +36,9 @@ module Chizuru
|
|
26
36
|
end
|
27
37
|
end
|
28
38
|
|
39
|
+
# Connects to UserStreaming API.
|
40
|
+
#
|
41
|
+
# The block will be given the events or statuses from Twitter API in JSON format.
|
29
42
|
def connect
|
30
43
|
uri = URI.parse("https://userstream.twitter.com/2/user.json?track=#{@screen_name}")
|
31
44
|
|
data/lib/chizuru/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: Chizuru
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mayth
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-05-
|
11
|
+
date: 2013-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|