poundie 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :rubygems
2
+
3
+ gem "tinder"
4
+ gem "typhoeus"
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.0.7)
5
+ addressable (2.2.6)
6
+ eventmachine (0.12.10)
7
+ faraday (0.5.7)
8
+ addressable (~> 2.2.4)
9
+ multipart-post (~> 1.1.0)
10
+ rack (>= 1.1.0, < 2)
11
+ mime-types (1.16)
12
+ multipart-post (1.1.1)
13
+ rack (1.2.2)
14
+ simple_oauth (0.1.5)
15
+ tinder (1.4.4)
16
+ activesupport
17
+ eventmachine
18
+ faraday (~> 0.5.1)
19
+ mime-types
20
+ multipart-post
21
+ twitter-stream
22
+ twitter-stream (0.1.13)
23
+ eventmachine (~> 0.12.8)
24
+ simple_oauth (~> 0.1.4)
25
+ typhoeus (0.2.3)
26
+ mime-types
27
+ mime-types
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ tinder
34
+ typhoeus
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # POUNDIE
2
+
3
+ Poundie is a Campfire bot.
4
+
5
+ Here's how you use it:
6
+
7
+ require "rubygems"
8
+ require "poundie"
9
+
10
+ # Specify which plugins to use
11
+ Poundie.use :greeter
12
+
13
+ # Start listening
14
+ Poundie.start
15
+
16
+ Poundie ships with one built-in plugin, called `:greeter`. Here's how a
17
+ plugin works:
18
+
19
+ # Your plugin needs inherit from Poundie::Plugin
20
+ class Greeter < Poundie::Plugin
21
+
22
+ # Next, register your plugin. This will be used to activate it.
23
+ register :greeter
24
+
25
+ # This specifies which messages should trigger the plugin's behavior
26
+ match do |message|
27
+ message.body == "greet"
28
+ end
29
+
30
+ # This specifies the plugin's behavior
31
+ action do |message|
32
+ speak "Hello, #{message.user.name}"
33
+ end
34
+ end
35
+
36
+ Pretty easy! The message that gets passed to `match` and `action` is:
37
+
38
+ message.body # text of the message
39
+ message.type # TextMessage, PasteMessage, SoundMessage, or TweetMessage
40
+ message.text? # message is text?
41
+ message.paste? # message is paste?
42
+ message.sound? # message is sound?
43
+ message.tweet? # message is tweet?
44
+ message.created_at # when the message was posted
45
+ message.user.name # name of the user who posted the message
46
+ message.user.email # email address of the user
47
+ message.user.admin? # is the user an admin?
48
+ message.user.created_at # when the user was created
49
+
50
+ What can you do in the `action`?
51
+
52
+ speak "Some text"
53
+ tweet "https://twitter.com/steve_martocci/status/69140542038097921"
54
+ paste "Some long text"
55
+
56
+ That's about it for now.
@@ -0,0 +1,49 @@
1
+ module Poundie
2
+ module Campfire
3
+ class Message
4
+ def initialize(hash)
5
+ @hash = hash
6
+ end
7
+
8
+ def id
9
+ @hash[:id]
10
+ end
11
+
12
+ def body
13
+ @hash[:body]
14
+ end
15
+
16
+ def room_id
17
+ @hash[:room_id]
18
+ end
19
+
20
+ def created_at
21
+ @hash[:created_at]
22
+ end
23
+
24
+ def user
25
+ Poundie::Campfire::User.new(@hash[:user])
26
+ end
27
+
28
+ def type
29
+ @hash[:type]
30
+ end
31
+
32
+ def text?
33
+ type == "TextMessage"
34
+ end
35
+
36
+ def paste?
37
+ type == "PasteMessage"
38
+ end
39
+
40
+ def sound?
41
+ type == "SoundMessage"
42
+ end
43
+
44
+ def tweet?
45
+ type == "TweetMessage"
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,33 @@
1
+ module Poundie
2
+ module Campfire
3
+ class User
4
+ def initialize(hash)
5
+ @hash = hash
6
+ end
7
+
8
+ def id
9
+ @hash[:id]
10
+ end
11
+
12
+ def name
13
+ @hash[:name]
14
+ end
15
+
16
+ def email_address
17
+ @hash[:email_address]
18
+ end
19
+
20
+ def admin?
21
+ @hash[:admin]
22
+ end
23
+
24
+ def created_at
25
+ @hash[:admin]
26
+ end
27
+
28
+ def type
29
+ @hash[:type]
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,59 @@
1
+ module Poundie
2
+ class Plugin
3
+ def self.list
4
+ @list ||= {}
5
+ end
6
+
7
+ def self.active
8
+ @active ||= []
9
+ end
10
+
11
+ def self.register(name)
12
+ puts "Registering #{name}: #{self}"
13
+ Poundie::Plugin.list[name] = self
14
+ end
15
+
16
+ def self.match(&block)
17
+ define_method(:match?, &block)
18
+ end
19
+
20
+ def self.action(&block)
21
+ define_method(:perform, &block)
22
+ end
23
+
24
+ def initialize(room)
25
+ @room = room
26
+ end
27
+
28
+ def call(message)
29
+ message = Poundie::Campfire::Message.new(message)
30
+ match?(message) && perform(message)
31
+ end
32
+
33
+ private
34
+
35
+ def get(url, params={})
36
+ Typhoeus::Request.get(url, params).body
37
+ end
38
+
39
+ def post(url, params={})
40
+ Typhoeus::Request.post(url, params).body
41
+ end
42
+
43
+ def speak(msg)
44
+ @room.speak(msg)
45
+ end
46
+
47
+ def paste(msg)
48
+ @room.paste(msg)
49
+ end
50
+
51
+ def tweet(url)
52
+ @room.tweet(url)
53
+ end
54
+
55
+ def play(sound)
56
+ @room.play(sound)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,15 @@
1
+ module Poundie
2
+ module Plugins
3
+ class Greeter < Poundie::Plugin
4
+ register :greeter
5
+
6
+ match do |message|
7
+ message.body == "greet"
8
+ end
9
+
10
+ action do |message|
11
+ speak "Hello, #{message.user.name}"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,55 @@
1
+ module Poundie
2
+ class Runner
3
+ CONFIG_FILE = File.join(ENV["HOME"], ".poundie_")
4
+
5
+ def initialize(name)
6
+ @name = name
7
+ configure
8
+ end
9
+
10
+ def run
11
+ trap('INT') { exit! }
12
+ room.listen do |line|
13
+ Thread.new do
14
+ plugins.each { |plugin|
15
+ plugin.call(line)
16
+ }
17
+ end
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def plugins
24
+ @plugins ||= Poundie::Plugin.active.map do |plugin|
25
+ plugin.new(room)
26
+ end
27
+ end
28
+
29
+ def room
30
+ @room ||= campfire.rooms.detect { |r| r.name == @room_name }
31
+ end
32
+
33
+ def campfire
34
+ @campfire ||= Tinder::Campfire.new(@subdomain, :token => @token)
35
+ end
36
+
37
+ def configure
38
+ if File.exists?(CONFIG_FILE + @name)
39
+ @subdomain, @token, @room_name = YAML.load_file(CONFIG_FILE + @name)
40
+ else
41
+ puts "Configuring..."
42
+ @subdomain = ask("Enter Campfire Subdomain: ")
43
+ @token = ask("Enter Campfire Token: ")
44
+ @room_name = ask("Enter Campfire Room Name: ")
45
+ File.open(CONFIG_FILE + @name, "w+") { |file|
46
+ YAML.dump([@subdomain, @token, @room_name], file)
47
+ }
48
+ end
49
+ end
50
+
51
+ def ask(msg)
52
+ Readline.readline(msg)
53
+ end
54
+ end
55
+ end
data/lib/poundie.rb ADDED
@@ -0,0 +1,28 @@
1
+ $LOAD_PATH << File.dirname(__FILE__)
2
+
3
+ require "tinder"
4
+ require "yaml"
5
+ require "readline"
6
+ require "typhoeus"
7
+ require "json"
8
+
9
+ require "poundie/runner"
10
+ require "poundie/plugin"
11
+ require "poundie/campfire/user"
12
+ require "poundie/campfire/message"
13
+ require "poundie/plugins/greeter"
14
+
15
+ trap('INT') { puts "ok bye!" ; exit! }
16
+
17
+ module Poundie
18
+ def self.start(name="default")
19
+ name.gsub!(/\W/, '_')
20
+ runner = Runner.new(name)
21
+ runner.run
22
+ end
23
+
24
+ def self.use(plugin)
25
+ puts "Using #{plugin}"
26
+ Poundie::Plugin.active << Poundie::Plugin.list[plugin]
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: poundie
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Pat Nakajima
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-13 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: tinder
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: typhoeus
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ description: Uses plugins to interact with campfire rooms
49
+ email:
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ extra_rdoc_files: []
55
+
56
+ files:
57
+ - Gemfile
58
+ - Gemfile.lock
59
+ - README.md
60
+ - lib/poundie.rb
61
+ - lib/poundie/campfire/message.rb
62
+ - lib/poundie/campfire/user.rb
63
+ - lib/poundie/plugin.rb
64
+ - lib/poundie/plugins/greeter.rb
65
+ - lib/poundie/runner.rb
66
+ homepage:
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options: []
71
+
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ requirements: []
93
+
94
+ rubyforge_project:
95
+ rubygems_version: 1.8.0
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: A campfire bot
99
+ test_files: []
100
+