botsy 0.0.1

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.
Files changed (3) hide show
  1. data/lib/botsy.rb +91 -0
  2. data/lib/botsy/version.rb +5 -0
  3. metadata +78 -0
data/lib/botsy.rb ADDED
@@ -0,0 +1,91 @@
1
+ require 'bundler/setup'
2
+ require 'net/http'
3
+ require 'json'
4
+ require 'yajl/http_stream'
5
+
6
+ module Botsy
7
+
8
+ class Bot
9
+
10
+ def initialize(subdomain, token, room_id, &block)
11
+ @subdomain = subdomain
12
+ @room_id = room_id
13
+ @token = token
14
+ @regexes = {}
15
+ load_info
16
+ join_room
17
+ ascertain_meaning(&block)
18
+ start_listening_forever
19
+ end
20
+
21
+ # Register a responder via a regex that will be evaluated on the body
22
+ def hear(regex, &block)
23
+ @regexes[regex] ||= []
24
+ @regexes[regex] << block
25
+ end
26
+
27
+ # Submit an HTTPS request with the JSON content to the speak path
28
+ def say(thing, type = 'TextMessage')
29
+ request = Net::HTTP::Post.new("/room/#{@room_id}/speak.json", 'Content-Type' => 'application/json')
30
+ request.body = "{\"message\":{\"type\":\"#{type}\",\"body\":\"#{thing}\"}}"
31
+ request.basic_auth @token, 'x'
32
+ http = Net::HTTP.new("#{@subdomain}.campfirenow.com", 443)
33
+ http.use_ssl = true
34
+ http.request(request)
35
+ end
36
+
37
+ private
38
+
39
+ def load_info
40
+ request = Net::HTTP::Get.new('/users/me.json')
41
+ request.basic_auth @token, 'x'
42
+ http = Net::HTTP.new("#{@subdomain}.campfirenow.com", 443)
43
+ http.use_ssl = true
44
+ if data = http.request(request).body
45
+ @me_info = Yajl::Parser::parse(data)['user']
46
+ end
47
+ end
48
+
49
+ def join_room
50
+ request = Net::HTTP::Post.new("/room/#{@room_id}/join.json")
51
+ request.basic_auth @token, 'x'
52
+ http = Net::HTTP.new("#{@subdomain}.campfirenow.com", 443)
53
+ http.use_ssl = true
54
+ http.request(request)
55
+ end
56
+
57
+ def ascertain_meaning(&block)
58
+ instance_eval &block
59
+ end
60
+
61
+ def start_listening_forever
62
+ while true # foreva means foreva bro
63
+ start_listening_in
64
+ end
65
+ end
66
+
67
+ def start_listening_in
68
+ puts 'Botsy is putting his ear to the ground...'
69
+ http = Net::HTTP.new('streaming.campfirenow.com', 443)
70
+ http.use_ssl = true
71
+
72
+ uri = URI.parse("http://#{@token}:x@streaming.campfirenow.com/room/#{@room_id}/live.json")
73
+ item = nil
74
+ Yajl::HttpStream.get(uri, :symbolize_keys => true) do |item|
75
+ handle_item item
76
+ end
77
+ end
78
+
79
+ def handle_item(item)
80
+ return unless item[:body].is_a?(String) # skip empties for now
81
+ return if @me_info && item[:user_id] == @me_info['id'] # don't repeat yourself, botsy
82
+ @regexes.each do |regex, do_this|
83
+ if mdata = item[:body].strip.match(regex)
84
+ do_this.each { |d| d.call(item, mdata) }
85
+ end
86
+ end
87
+ end
88
+
89
+ end
90
+
91
+ end
@@ -0,0 +1,5 @@
1
+ module Botsy
2
+
3
+ VERSION = '0.0.1'
4
+
5
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: botsy
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - John Crepezzi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-02-14 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: yajl-ruby
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 0.8.1
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ description: Smart-ass robot
39
+ email: john.crepezzi@patch.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - lib/botsy/version.rb
48
+ - lib/botsy.rb
49
+ has_rdoc: true
50
+ homepage: http://seejohnrun.github.com/botsy/
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ requirements: []
71
+
72
+ rubyforge_project: botsy
73
+ rubygems_version: 1.5.0
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Robot that does what he wants
77
+ test_files: []
78
+