simonreed-lyrebird 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.
- data/README.rdoc +12 -0
- data/lib/lyre_bird/request.rb +15 -0
- data/lib/lyre_bird/response.rb +78 -0
- data/lib/lyre_bird/route.rb +48 -0
- data/lib/lyre_bird/twitter.rb +34 -0
- data/lib/lyre_bird.rb +69 -0
- metadata +72 -0
data/README.rdoc
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
= LyreBird
|
2
|
+
|
3
|
+
Twitter Gateway, allows your app to respond to natural language questions from Twitter.
|
4
|
+
|
5
|
+
== Requirements
|
6
|
+
|
7
|
+
* HTTParty http://github.com/jnunemaker/httparty/ - For accessing the Twitter web service.
|
8
|
+
|
9
|
+
== Install
|
10
|
+
|
11
|
+
* sudo gem sources -a http://gems.github.com
|
12
|
+
* sudo gem install simonreed-lyrebird
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class LyreBird::Request
|
2
|
+
|
3
|
+
attr_accessor :tweet
|
4
|
+
attr_accessor :response
|
5
|
+
|
6
|
+
def initialize(options={})
|
7
|
+
self.tweet = options[:tweet]
|
8
|
+
self.response = self.respond(:text => self.tweet['text'].gsub(/\@nicetripper /,''))
|
9
|
+
end
|
10
|
+
|
11
|
+
def respond(options={})
|
12
|
+
params = LyreBird::Route.route(options[:text])
|
13
|
+
LyreBird::Response.new(:params => params,:to => self.tweet['user']['screen_name'], :request_tweet => self.tweet)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
class LyreBird::Response
|
2
|
+
|
3
|
+
attr_accessor :to
|
4
|
+
attr_accessor :text
|
5
|
+
attr_accessor :params
|
6
|
+
attr_accessor :request_tweet
|
7
|
+
|
8
|
+
def initialize(options={})
|
9
|
+
self.params = options[:params]
|
10
|
+
self.to = options[:to]
|
11
|
+
self.request_tweet = options[:request_tweet]
|
12
|
+
|
13
|
+
if self.params.nil?
|
14
|
+
error
|
15
|
+
return
|
16
|
+
end
|
17
|
+
|
18
|
+
history = $responses[self.to]
|
19
|
+
|
20
|
+
klass = self.params['class'] || 'LyresController'
|
21
|
+
method = self.params['method'] || 'hot'
|
22
|
+
|
23
|
+
self.text = klass.constantize.new.send method.to_sym, self, history, self.params
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
def register_response(params)
|
28
|
+
$responses[self.to] = params
|
29
|
+
end
|
30
|
+
|
31
|
+
def error
|
32
|
+
puts "LyreBird::Response.error"
|
33
|
+
$no_responses.push(self.request_tweet)
|
34
|
+
msg = "Don't know what you're asking for"
|
35
|
+
self.text = msg
|
36
|
+
puts "LyreBird::Response.error self.text #{self.text}"
|
37
|
+
if DEBUG
|
38
|
+
puts "-" * 50
|
39
|
+
puts
|
40
|
+
puts "From @#{self.request_tweet['user']['screen_name']} - \"#{self.request_tweet['text']}\""
|
41
|
+
puts
|
42
|
+
puts "POSTING Don't get it - \"#{msg}\""
|
43
|
+
puts
|
44
|
+
puts "-" * 50
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def post(live=false)
|
49
|
+
|
50
|
+
history = $responses[self.to]
|
51
|
+
|
52
|
+
msg = self.text
|
53
|
+
|
54
|
+
puts
|
55
|
+
puts "-" * 50
|
56
|
+
puts
|
57
|
+
puts "From @#{self.request_tweet['user']['screen_name']} - \"#{self.request_tweet['text']}\""
|
58
|
+
puts
|
59
|
+
puts "Reply - \"#{msg}\""
|
60
|
+
puts
|
61
|
+
puts "-" * 50
|
62
|
+
puts
|
63
|
+
|
64
|
+
if DEBUG
|
65
|
+
puts
|
66
|
+
puts "Params - #{self.params.inspect}"
|
67
|
+
puts
|
68
|
+
puts "History - #{history.inspect}"
|
69
|
+
puts
|
70
|
+
puts "-" * 50
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
if live
|
75
|
+
$twitter.post(msg)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
ROUTES = YAML.load_file("#{RAILS_ROOT}/config/lyre_routes.yml")
|
2
|
+
|
3
|
+
class LyreBird::Route
|
4
|
+
def initialize(options={})
|
5
|
+
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.route(str='', debug=false)
|
9
|
+
|
10
|
+
successes = []
|
11
|
+
puts "Parsing '#{str}'" if DEBUG
|
12
|
+
::ROUTES.each do | r |
|
13
|
+
route = r['str']
|
14
|
+
clear = r['clear']
|
15
|
+
puts "- Trying '#{route}'" if DEBUG
|
16
|
+
regex = {}
|
17
|
+
regex[:vars] = route.gsub(/(:[a-z]*)/, '(:[a-z]*)')
|
18
|
+
puts "- REGEX VARS '#{regex[:vars]}'" if DEBUG
|
19
|
+
vars = route.match(regex[:vars])
|
20
|
+
|
21
|
+
puts "str = '#{str}'" if CONSOLE and vars.nil?
|
22
|
+
puts "str.match(/#{regex[:vars]}/)" if CONSOLE and vars.nil?
|
23
|
+
|
24
|
+
puts "- NO VARS '#{vars.inspect}'" if DEBUG and vars.nil?
|
25
|
+
next if vars.nil?
|
26
|
+
|
27
|
+
regex[:values] = regex[:vars].gsub(/\:\[a\-z\]/, '.')
|
28
|
+
puts "- Trying '#{regex[:values]}'" if DEBUG
|
29
|
+
values = str.match(regex[:values])
|
30
|
+
|
31
|
+
next if values.nil?
|
32
|
+
|
33
|
+
params = {}.merge(r)
|
34
|
+
|
35
|
+
i = 1;
|
36
|
+
|
37
|
+
vars[1,vars.size].each do | var |
|
38
|
+
clean_name = var.gsub(/:/,'')
|
39
|
+
params[clean_name.to_sym] = values[i]
|
40
|
+
i += 1
|
41
|
+
end
|
42
|
+
params = (str.match(/good|ok|pretty good/).nil?) ? params : params.merge({:sort => :rating})
|
43
|
+
puts "-- PASS '#{route}' got #{params.inspect}" if DEBUG
|
44
|
+
successes.unshift(params)
|
45
|
+
end
|
46
|
+
return successes.last
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
|
3
|
+
class LyreBird::Twitter
|
4
|
+
include HTTParty
|
5
|
+
base_uri 'twitter.com'
|
6
|
+
|
7
|
+
def initialize(u, p)
|
8
|
+
@auth = {:username => u, :password => p}
|
9
|
+
end
|
10
|
+
|
11
|
+
# which can be :friends, :user or :public
|
12
|
+
# options[:query] can be things like since, since_id, count, etc.
|
13
|
+
def mentions(which=:mentions, options={})
|
14
|
+
since_id = options[:since_id]
|
15
|
+
options = {}
|
16
|
+
options.merge!({:basic_auth => @auth})
|
17
|
+
url = "/statuses/#{which}.json?"
|
18
|
+
url += "since_id=#{since_id}" unless since_id.nil?
|
19
|
+
puts "Requesting #{url} from Twitter API"
|
20
|
+
self.class.get(url, options)
|
21
|
+
end
|
22
|
+
|
23
|
+
# which can be :friends, :user or :public
|
24
|
+
# options[:query] can be things like since, since_id, count, etc.
|
25
|
+
def timeline(which=:friends, options={})
|
26
|
+
options.merge!({:basic_auth => @auth})
|
27
|
+
self.class.get("/statuses/#{which}_timeline.json", options)
|
28
|
+
end
|
29
|
+
|
30
|
+
def post(text)
|
31
|
+
options = { :query => {:status => text}, :basic_auth => @auth }
|
32
|
+
self.class.post('/statuses/update.json', options)
|
33
|
+
end
|
34
|
+
end
|
data/lib/lyre_bird.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
CONSOLE = false
|
2
|
+
DEBUG = false
|
3
|
+
|
4
|
+
class LyreBird
|
5
|
+
|
6
|
+
attr_accessor :username
|
7
|
+
attr_accessor :password
|
8
|
+
|
9
|
+
def initialize(options={})
|
10
|
+
$responses = {}
|
11
|
+
self.username = options[:username] || 'username'
|
12
|
+
self.password = options[:password] || 'password'
|
13
|
+
end
|
14
|
+
|
15
|
+
def listen(options={})
|
16
|
+
options = { :limit => 1, :sleep=> 5}.merge(options)
|
17
|
+
|
18
|
+
responses = []
|
19
|
+
:limit.to_i.times do
|
20
|
+
responses.push(self.check(:post => true))
|
21
|
+
end
|
22
|
+
responses
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_since
|
26
|
+
since = 0;
|
27
|
+
filename = "#{RAILS_ROOT}/config/listenSince.txt"
|
28
|
+
File.open(filename).each { |line|
|
29
|
+
since = line
|
30
|
+
}
|
31
|
+
since
|
32
|
+
end
|
33
|
+
|
34
|
+
def set_since(since_id)
|
35
|
+
filename = "#{RAILS_ROOT}/config/listenSince.txt"
|
36
|
+
puts "Modified SINCE_ID to - #{since_id}"
|
37
|
+
since = File.open(filename, 'w') {|f| f.write(since_id) }
|
38
|
+
since
|
39
|
+
end
|
40
|
+
|
41
|
+
def check(options={})
|
42
|
+
|
43
|
+
options = {:live=>true}.merge(options)
|
44
|
+
responses = []
|
45
|
+
|
46
|
+
if options[:messages]
|
47
|
+
puts "DOING AN OFFLINE LYREBIRD CHECK"
|
48
|
+
mentions = options[:messages]
|
49
|
+
else
|
50
|
+
$twitter = LyreBird::Twitter.new(self.username,self.password)
|
51
|
+
since = options[:since] || get_since()
|
52
|
+
mentions = $twitter.mentions(:mentions,{ :since_id => since })
|
53
|
+
end
|
54
|
+
|
55
|
+
mentions = mentions.sort {|a,b| a['id']<=>b['id']}
|
56
|
+
mentions.each do | tweet |
|
57
|
+
puts "Tweet - #{tweet.keys.to_yaml}" if DEBUG
|
58
|
+
req = LyreBird::Request.new(:tweet => tweet)
|
59
|
+
if options[:post]
|
60
|
+
req.response.post(options[:live])
|
61
|
+
end
|
62
|
+
responses.push(req.response)
|
63
|
+
end
|
64
|
+
|
65
|
+
puts "NO NEW TWEETS @ #{Time.now}" unless mentions.size > 0
|
66
|
+
set_since(mentions.last['id']) unless mentions.size == 0
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simonreed-lyrebird
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Simon Reed
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-07 00:00:00 -07:00
|
13
|
+
default_executable: lyrebird
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: httparty
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.4.4
|
24
|
+
version:
|
25
|
+
description: Twitter Gateway, respond to natural language questions from Twitter.
|
26
|
+
email: simonpreed@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- lib/lyre_bird/request.rb
|
35
|
+
- lib/lyre_bird/response.rb
|
36
|
+
- lib/lyre_bird/route.rb
|
37
|
+
- lib/lyre_bird/twitter.rb
|
38
|
+
- lib/lyre_bird.rb
|
39
|
+
- README.rdoc
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://github.com/simonreed/LyreBird/tree/master
|
42
|
+
post_install_message: Thanks for installing LyreBird!
|
43
|
+
rdoc_options:
|
44
|
+
- --line-numbers
|
45
|
+
- --inline-source
|
46
|
+
- --title
|
47
|
+
- Httparty
|
48
|
+
- --main
|
49
|
+
- README.rdoc
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "1.2"
|
63
|
+
version:
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.2.0
|
68
|
+
signing_key:
|
69
|
+
specification_version: 2
|
70
|
+
summary: Twitter Gateway, respond to natural language questions from Twitter.
|
71
|
+
test_files: []
|
72
|
+
|