simonreed-lyrebird 0.1.0.2.1 → 0.1.0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/bin/lyre_bird +1 -1
- data/lib/lyre_bird/request.rb +12 -10
- data/lib/lyre_bird/response.rb +62 -60
- data/lib/lyre_bird/route.rb +37 -35
- data/lib/lyre_bird/twitter.rb +28 -26
- data/lib/lyre_bird.rb +57 -55
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -14,7 +14,7 @@ Twitter Gateway, allows your app to respond to natural language questions from T
|
|
14
14
|
{ 'str' => "Do you have a :item for me" }
|
15
15
|
]
|
16
16
|
|
17
|
-
lyre = LyreBird.new(:username => 'username', :password => 'password', :routes => routes)
|
17
|
+
lyre = LyreBird::Base.new(:username => 'username', :password => 'password', :routes => routes)
|
18
18
|
lyre.check() # Check for new messages.
|
19
19
|
|
20
20
|
== Install
|
data/bin/lyre_bird
CHANGED
data/lib/lyre_bird/request.rb
CHANGED
@@ -1,15 +1,17 @@
|
|
1
|
-
|
1
|
+
module LyreBird
|
2
|
+
class Request
|
2
3
|
|
3
|
-
|
4
|
-
|
4
|
+
attr_accessor :tweet
|
5
|
+
attr_accessor :response
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
def initialize(options={})
|
8
|
+
self.tweet = options[:tweet]
|
9
|
+
self.response = self.respond(:text => self.tweet['text'].gsub(/\@nicetripper /,''))
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
def respond(options={})
|
13
|
+
params = LyreBird::Route.route(options[:text])
|
14
|
+
LyreBird::Response.new(:params => params,:to => self.tweet['user']['screen_name'], :request_tweet => self.tweet)
|
15
|
+
end
|
14
16
|
end
|
15
17
|
end
|
data/lib/lyre_bird/response.rb
CHANGED
@@ -1,78 +1,80 @@
|
|
1
|
-
|
1
|
+
module LyreBird
|
2
|
+
class Response
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
attr_accessor :to
|
5
|
+
attr_accessor :text
|
6
|
+
attr_accessor :params
|
7
|
+
attr_accessor :request_tweet
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
def initialize(options={})
|
10
|
+
self.params = options[:params]
|
11
|
+
self.to = options[:to]
|
12
|
+
self.request_tweet = options[:request_tweet]
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
if self.params.nil?
|
15
|
+
error
|
16
|
+
return
|
17
|
+
end
|
17
18
|
|
18
|
-
|
19
|
+
history = $responses[self.to]
|
19
20
|
|
20
|
-
|
21
|
-
|
21
|
+
klass = self.params['class'] || 'LyresController'
|
22
|
+
method = self.params['method'] || 'hot'
|
22
23
|
|
23
|
-
|
24
|
+
self.text = klass.constantize.new.send method.to_sym, self, history, self.params
|
24
25
|
|
25
|
-
|
26
|
+
end
|
26
27
|
|
27
|
-
|
28
|
-
|
29
|
-
|
28
|
+
def register_response(params)
|
29
|
+
$responses[self.to] = params
|
30
|
+
end
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
32
|
+
def error
|
33
|
+
puts "LyreBird::Response.error"
|
34
|
+
$no_responses.push(self.request_tweet)
|
35
|
+
msg = "Don't know what you're asking for"
|
36
|
+
self.text = msg
|
37
|
+
puts "LyreBird::Response.error self.text #{self.text}"
|
38
|
+
if DEBUG
|
39
|
+
puts "-" * 50
|
40
|
+
puts
|
41
|
+
puts "From @#{self.request_tweet['user']['screen_name']} - \"#{self.request_tweet['text']}\""
|
42
|
+
puts
|
43
|
+
puts "POSTING Don't get it - \"#{msg}\""
|
44
|
+
puts
|
45
|
+
puts "-" * 50
|
46
|
+
end
|
45
47
|
end
|
46
|
-
end
|
47
48
|
|
48
|
-
|
49
|
+
def post(live=false)
|
49
50
|
|
50
|
-
|
51
|
+
history = $responses[self.to]
|
51
52
|
|
52
|
-
|
53
|
+
msg = self.text
|
53
54
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
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}"
|
55
|
+
puts
|
56
|
+
puts "-" * 50
|
57
|
+
puts
|
58
|
+
puts "From @#{self.request_tweet['user']['screen_name']} - \"#{self.request_tweet['text']}\""
|
67
59
|
puts
|
68
|
-
puts "
|
69
|
-
puts
|
70
|
-
puts "-" * 50
|
71
|
-
|
60
|
+
puts "Reply - \"#{msg}\""
|
61
|
+
puts
|
62
|
+
puts "-" * 50
|
63
|
+
puts
|
64
|
+
|
65
|
+
if DEBUG
|
66
|
+
puts
|
67
|
+
puts "Params - #{self.params.inspect}"
|
68
|
+
puts
|
69
|
+
puts "History - #{history.inspect}"
|
70
|
+
puts
|
71
|
+
puts "-" * 50
|
72
|
+
end
|
72
73
|
|
73
74
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
75
|
+
if live
|
76
|
+
$twitter.post(msg)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
78
80
|
end
|
data/lib/lyre_bird/route.rb
CHANGED
@@ -1,46 +1,48 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module LyreBird
|
2
|
+
class Route
|
3
|
+
def initialize(options={})
|
3
4
|
|
4
|
-
|
5
|
+
end
|
5
6
|
|
6
|
-
|
7
|
+
def self.route(str='', debug=false)
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
9
|
+
successes = []
|
10
|
+
puts "Parsing '#{str}'" if DEBUG
|
11
|
+
$lyrebird_routes.each do | r |
|
12
|
+
route = r['str']
|
13
|
+
clear = r['clear']
|
14
|
+
puts "- Trying '#{route}'" if DEBUG
|
15
|
+
regex = {}
|
16
|
+
regex[:vars] = route.gsub(/(:[a-z]*)/, '(:[a-z]*)')
|
17
|
+
puts "- REGEX VARS '#{regex[:vars]}'" if DEBUG
|
18
|
+
vars = route.match(regex[:vars])
|
18
19
|
|
19
|
-
|
20
|
-
|
20
|
+
puts "str = '#{str}'" if CONSOLE and vars.nil?
|
21
|
+
puts "str.match(/#{regex[:vars]}/)" if CONSOLE and vars.nil?
|
21
22
|
|
22
|
-
|
23
|
-
|
23
|
+
puts "- NO VARS '#{vars.inspect}'" if DEBUG and vars.nil?
|
24
|
+
next if vars.nil?
|
24
25
|
|
25
|
-
|
26
|
-
|
27
|
-
|
26
|
+
regex[:values] = regex[:vars].gsub(/\:\[a\-z\]/, '.')
|
27
|
+
puts "- Trying '#{regex[:values]}'" if DEBUG
|
28
|
+
values = str.match(regex[:values])
|
28
29
|
|
29
|
-
|
30
|
+
next if values.nil?
|
30
31
|
|
31
|
-
|
32
|
+
params = {}.merge(r)
|
32
33
|
|
33
|
-
|
34
|
+
i = 1;
|
34
35
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
36
|
+
vars[1,vars.size].each do | var |
|
37
|
+
clean_name = var.gsub(/:/,'')
|
38
|
+
params[clean_name.to_sym] = values[i]
|
39
|
+
i += 1
|
40
|
+
end
|
41
|
+
params = (str.match(/good|ok|pretty good/).nil?) ? params : params.merge({:sort => :rating})
|
42
|
+
puts "-- PASS '#{route}' got #{params.inspect}" if DEBUG
|
43
|
+
successes.unshift(params)
|
44
|
+
end
|
45
|
+
return successes.last
|
46
|
+
end
|
47
|
+
end
|
46
48
|
end
|
data/lib/lyre_bird/twitter.rb
CHANGED
@@ -1,34 +1,36 @@
|
|
1
1
|
require 'httparty'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
module LyreBird
|
4
|
+
class Twitter
|
5
|
+
include HTTParty
|
6
|
+
base_uri 'twitter.com'
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
def initialize(u, p)
|
9
|
+
@auth = {:username => u, :password => p}
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
12
|
+
# which can be :friends, :user or :public
|
13
|
+
# options[:query] can be things like since, since_id, count, etc.
|
14
|
+
def mentions(which=:mentions, options={})
|
15
|
+
since_id = options[:since_id]
|
16
|
+
options = {}
|
17
|
+
options.merge!({:basic_auth => @auth})
|
18
|
+
url = "/statuses/#{which}.json?"
|
19
|
+
url += "since_id=#{since_id}" unless since_id.nil?
|
20
|
+
puts "Requesting #{url} from Twitter API"
|
21
|
+
self.class.get(url, options)
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
24
|
+
# which can be :friends, :user or :public
|
25
|
+
# options[:query] can be things like since, since_id, count, etc.
|
26
|
+
def timeline(which=:friends, options={})
|
27
|
+
options.merge!({:basic_auth => @auth})
|
28
|
+
self.class.get("/statuses/#{which}_timeline.json", options)
|
29
|
+
end
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
def post(text)
|
32
|
+
options = { :query => {:status => text}, :basic_auth => @auth }
|
33
|
+
self.class.post('/statuses/update.json', options)
|
34
|
+
end
|
33
35
|
end
|
34
36
|
end
|
data/lib/lyre_bird.rb
CHANGED
@@ -3,74 +3,76 @@ require 'lyre_bird/request'
|
|
3
3
|
require 'lyre_bird/route'
|
4
4
|
require 'lyre_bird/twitter'
|
5
5
|
|
6
|
-
|
6
|
+
module LyreBird
|
7
|
+
class Base
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
attr_accessor :username
|
10
|
+
attr_accessor :password
|
11
|
+
attr_accessor :since_filename
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
13
|
+
def initialize(options={})
|
14
|
+
$responses = {}
|
15
|
+
self.username = options[:username] || 'username'
|
16
|
+
self.password = options[:password] || 'password'
|
17
|
+
self.since_filename = options[:since_filename] || "lyre_bird_listen_since.txt"
|
18
|
+
$lyrebird_routes = options[:routes] || []
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
-
|
21
|
+
def listen(options={})
|
22
|
+
options = { :limit => 1, :sleep=> 5}.merge(options)
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
responses = []
|
25
|
+
:limit.to_i.times do
|
26
|
+
responses.push(self.check(:post => true))
|
27
|
+
end
|
28
|
+
responses
|
26
29
|
end
|
27
|
-
responses
|
28
|
-
end
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
31
|
+
def get_since
|
32
|
+
since = 0;
|
33
|
+
if File.exists?(self.since_filename)
|
34
|
+
File.open(self.since_filename).each { |line|
|
35
|
+
since = line
|
36
|
+
}
|
37
|
+
return since
|
38
|
+
else
|
39
|
+
return nil
|
40
|
+
end
|
39
41
|
end
|
40
|
-
end
|
41
42
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
43
|
+
def set_since(since_id)
|
44
|
+
puts "Modified SINCE_ID to - #{since_id}"
|
45
|
+
since = File.open(self.since_filename, 'w') {|f| f.write(since_id) }
|
46
|
+
since
|
47
|
+
end
|
47
48
|
|
48
|
-
|
49
|
+
def check(options={})
|
49
50
|
|
50
|
-
|
51
|
-
|
51
|
+
options = {:live=>true}.merge(options)
|
52
|
+
responses = []
|
52
53
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
54
|
+
if options[:messages]
|
55
|
+
puts "DOING AN OFFLINE LYREBIRD CHECK"
|
56
|
+
mentions = options[:messages]
|
57
|
+
else
|
58
|
+
$twitter = LyreBird::Twitter.new(self.username,self.password)
|
59
|
+
since = options[:since] || get_since()
|
60
|
+
mentions = $twitter.mentions(:mentions,{ :since_id => since })
|
61
|
+
end
|
61
62
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
63
|
+
mentions = mentions.sort {|a,b| a['id']<=>b['id']}
|
64
|
+
mentions.each do | tweet |
|
65
|
+
puts "Tweet - #{tweet.keys.to_yaml}" if DEBUG
|
66
|
+
req = LyreBird::Request.new(:tweet => tweet)
|
67
|
+
if options[:post]
|
68
|
+
req.response.post(options[:live])
|
69
|
+
end
|
70
|
+
responses.push(req.response)
|
68
71
|
end
|
69
|
-
responses.push(req.response)
|
70
|
-
end
|
71
72
|
|
72
|
-
|
73
|
-
|
74
|
-
|
73
|
+
puts "NO NEW TWEETS @ #{Time.now}" unless mentions.size > 0
|
74
|
+
set_since(mentions.last['id']) unless mentions.size == 0
|
75
|
+
end
|
75
76
|
|
77
|
+
end
|
76
78
|
end
|