chatroid 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.
data/README.md CHANGED
@@ -9,6 +9,8 @@ $ gem "chatroid"
9
9
 
10
10
  ## Example
11
11
  You can create your own bot working in a chat service.
12
+ The following example creates a bot,
13
+ which responds to tweets which include the word "yunotti" or any replies to it.
12
14
 
13
15
  ```ruby
14
16
  require "chatroid"
@@ -20,14 +22,15 @@ Chatroid.new do
20
22
  set :access_key, "..."
21
23
  set :access_secret, "..."
22
24
 
23
- on_message do |event|
25
+ on_tweet do |event|
24
26
  if event["text"] =~ /yunotti/
25
- post "✘╹◡╹✘"
27
+ favorite event
28
+ follow event
26
29
  end
27
30
  end
28
31
 
29
32
  on_reply do |event|
30
- post "me too", :to => event
33
+ reply "✘╹◡╹✘", event
31
34
  end
32
35
  end.run!
33
36
  ```
@@ -36,4 +39,8 @@ end.run!
36
39
  Currently following services are supported:
37
40
 
38
41
  * Twitter
39
- * HipChat(WIP)
42
+
43
+ I plan to support following services:
44
+
45
+ * IRC
46
+ * HipChat
@@ -4,30 +4,18 @@ require "json"
4
4
 
5
5
  class Chatroid
6
6
  module Adapter
7
- class Twitter
8
- def initialize(chatroid)
9
- @chatroid = chatroid
10
- end
7
+ module Twitter
8
+ private
11
9
 
12
10
  def connect
13
- EventMachine.error_handler do |error|
14
- puts "Error raised during event loop: #{error.message}"
15
- end
16
11
  EventMachine::run do
17
- stream.each_item(&method(:on_each_event))
12
+ stream.each_item do |json|
13
+ event = JSON.parse(json)
14
+ on_each_event(event)
15
+ end
18
16
  end
19
17
  end
20
18
 
21
- def post(body, options = {})
22
- client.update(body)
23
- end
24
-
25
- def user_info
26
- @user_info ||= client.info
27
- end
28
-
29
- private
30
-
31
19
  def stream
32
20
  @stream ||= ::Twitter::JSONStream.connect(
33
21
  :host => "userstream.twitter.com",
@@ -35,38 +23,53 @@ class Chatroid
35
23
  :port => 443,
36
24
  :ssl => true,
37
25
  :oauth => {
38
- :consumer_key => @chatroid.config[:consumer_key],
39
- :consumer_secret => @chatroid.config[:consumer_secret],
40
- :access_key => @chatroid.config[:access_key],
41
- :access_secret => @chatroid.config[:access_secret],
26
+ :consumer_key => config[:consumer_key],
27
+ :consumer_secret => config[:consumer_secret],
28
+ :access_key => config[:access_key],
29
+ :access_secret => config[:access_secret],
42
30
  }
43
31
  )
44
32
  end
45
33
 
46
34
  def client
47
35
  @client ||= TwitterOAuth::Client.new(
48
- :consumer_key => @chatroid.config[:consumer_key],
49
- :consumer_secret => @chatroid.config[:consumer_secret],
50
- :token => @chatroid.config[:access_key],
51
- :secret => @chatroid.config[:access_secret]
36
+ :consumer_key => config[:consumer_key],
37
+ :consumer_secret => config[:consumer_secret],
38
+ :token => config[:access_key],
39
+ :secret => config[:access_secret]
52
40
  )
53
41
  end
54
42
 
55
- def on_each_event(json)
56
- case event = JSON.parse(json)
57
- when event["in_reply_to_status_id"]
58
- on_reply(event)
59
- when event["text"]
60
- on_tweet(event)
61
- end
43
+ def tweet(body)
44
+ client.update(body)
45
+ end
46
+
47
+ def reply(body, event)
48
+ id = event["id"]
49
+ user = event["user"]["screen_name"]
50
+ body = "@#{user} #{body}"
51
+ client.update(body, :in_reply_to_status_id => id)
52
+ end
53
+
54
+ def favorite(event)
55
+ client.favorite(event["id"])
62
56
  end
63
57
 
64
- def on_tweet(event)
65
- @chatroid.trigger_tweet(event)
58
+ def follow(event)
59
+ client.friend(event["user"]["id"])
66
60
  end
67
61
 
68
- def on_reply(event)
69
- @chatroid.trigger_reply(event)
62
+ def user_info
63
+ @user_info ||= client.info
64
+ end
65
+
66
+ def on_each_event(event)
67
+ case
68
+ when event["in_reply_to_user_id"] == user_info["id"]
69
+ trigger_reply(event)
70
+ when event["text"]
71
+ trigger_tweet(event)
72
+ end
70
73
  end
71
74
  end
72
75
  end
@@ -1,3 +1,3 @@
1
1
  class Chatroid
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/chatroid.rb CHANGED
@@ -15,7 +15,8 @@ class Chatroid
15
15
 
16
16
  def run!
17
17
  validate_connection
18
- adapter.connect
18
+ extend(adapter)
19
+ connect
19
20
  end
20
21
 
21
22
  private
@@ -36,11 +37,6 @@ class Chatroid
36
37
  config.merge!(hash)
37
38
  end
38
39
 
39
- # Post message via adapter
40
- def post(*args)
41
- adapter.post(*args)
42
- end
43
-
44
40
  def validate_connection
45
41
  validate_config
46
42
  validate_adapter
@@ -63,15 +59,11 @@ class Chatroid
63
59
  end
64
60
 
65
61
  def has_adapter?
66
- has_service? && adapter_class
67
- end
68
-
69
- def adapter_class
70
- Adapter.find(config[:service])
62
+ has_service? && adapter
71
63
  end
72
64
 
73
65
  def adapter
74
- @adapter ||= adapter_class.new(self)
66
+ Adapter.find(config[:service])
75
67
  end
76
68
 
77
69
  class ConnectionError < StandardError; end
@@ -1,28 +1,47 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Chatroid::Adapter::Twitter do
4
- shared_examples_for "interface of Chatroid::Adapter" do
5
- it do
6
- Chatroid::Adapter::Twitter.should be_respond_to(:new)
7
- end
8
-
9
- it do
10
- Chatroid::Adapter::Twitter.new(mock).should be_respond_to(:connect)
11
- end
12
-
13
- it do
14
- Chatroid::Adapter::Twitter.new(mock).should be_respond_to(:post)
4
+ let(:chatroid) do
5
+ Chatroid.new do
6
+ extend Chatroid::Adapter::Twitter
15
7
  end
16
8
  end
17
9
 
18
- it_should_behave_like "interface of Chatroid::Adapter"
19
-
20
10
  describe "#connect" do
21
11
  it "should work in EventMachine::run" do
22
12
  EventMachine.should_receive(:run) do |&block|
23
13
  block.should be_a(Proc)
24
14
  end
25
- Chatroid::Adapter::Twitter.new(mock).connect
15
+ chatroid.send(:connect)
16
+ end
17
+ end
18
+
19
+ describe "#tweet" do
20
+ it "should call TwitterOAuth::Client#update with body" do
21
+ TwitterOAuth::Client.any_instance.should_receive(:update).with("body")
22
+ chatroid.send(:tweet, "body")
23
+ end
24
+ end
25
+
26
+ describe "#reply" do
27
+ it "should call TwitterOAuth::Client#update with body and id" do
28
+ TwitterOAuth::Client.any_instance.should_receive(:update).
29
+ with("@foo body", :in_reply_to_status_id => 1)
30
+ chatroid.send(:reply, "body", "id" => 1, "user" => { "screen_name" => "foo" })
31
+ end
32
+ end
33
+
34
+ describe "#favorite" do
35
+ it "should call TwitterOAuth::Client#favorite with id" do
36
+ TwitterOAuth::Client.any_instance.should_receive(:favorite).with("1")
37
+ chatroid.send(:favorite, "id" => "1")
38
+ end
39
+ end
40
+
41
+ describe "#follow" do
42
+ it "should call TwitterOAuth::Client#friend with id" do
43
+ TwitterOAuth::Client.any_instance.should_receive(:friend).with("1")
44
+ chatroid.send(:follow, "user" => { "id" => "1" })
26
45
  end
27
46
  end
28
47
  end
@@ -55,8 +55,10 @@ describe Chatroid do
55
55
  chatroid = Chatroid.new
56
56
  chatroid.stub(:has_service?).and_return(true)
57
57
  chatroid.stub(:has_adapter?).and_return(true)
58
+ chatroid.stub(:adapter_class)
59
+ chatroid.stub(:extend)
58
60
  chatroid.stub(:adapter).and_return(adapter)
59
- adapter.should_receive(:connect)
61
+ chatroid.should_receive(:connect)
60
62
  chatroid.run!
61
63
  end
62
64
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chatroid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-09-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: twitter-stream
16
- requirement: &70293885438900 !ruby/object:Gem::Requirement
16
+ requirement: &70196510426520 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70293885438900
24
+ version_requirements: *70196510426520
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: twitter_oauth
27
- requirement: &70293885438300 !ruby/object:Gem::Requirement
27
+ requirement: &70196510425840 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70293885438300
35
+ version_requirements: *70196510425840
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70293885437520 !ruby/object:Gem::Requirement
38
+ requirement: &70196510425060 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70293885437520
46
+ version_requirements: *70196510425060
47
47
  description: Chatroid is a gem for quickly creating chatterbot in Ruby
48
48
  email:
49
49
  - r7kamura@gmail.com