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 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
@@ -3,7 +3,7 @@ require 'lyre_bird'
3
3
 
4
4
  routes = [ { 'str' => "Do you have a :item for me" } ]
5
5
 
6
- @lb = LyreBird.new(
6
+ @lb = LyreBird::Base.new(
7
7
  :username => 'username',
8
8
  :password => 'password',
9
9
  :routes => routes,
@@ -1,15 +1,17 @@
1
- class LyreBird::Request
1
+ module LyreBird
2
+ class Request
2
3
 
3
- attr_accessor :tweet
4
- attr_accessor :response
4
+ attr_accessor :tweet
5
+ attr_accessor :response
5
6
 
6
- def initialize(options={})
7
- self.tweet = options[:tweet]
8
- self.response = self.respond(:text => self.tweet['text'].gsub(/\@nicetripper /,''))
9
- end
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
- 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)
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
@@ -1,78 +1,80 @@
1
- class LyreBird::Response
1
+ module LyreBird
2
+ class Response
2
3
 
3
- attr_accessor :to
4
- attr_accessor :text
5
- attr_accessor :params
6
- attr_accessor :request_tweet
4
+ attr_accessor :to
5
+ attr_accessor :text
6
+ attr_accessor :params
7
+ attr_accessor :request_tweet
7
8
 
8
- def initialize(options={})
9
- self.params = options[:params]
10
- self.to = options[:to]
11
- self.request_tweet = options[:request_tweet]
9
+ def initialize(options={})
10
+ self.params = options[:params]
11
+ self.to = options[:to]
12
+ self.request_tweet = options[:request_tweet]
12
13
 
13
- if self.params.nil?
14
- error
15
- return
16
- end
14
+ if self.params.nil?
15
+ error
16
+ return
17
+ end
17
18
 
18
- history = $responses[self.to]
19
+ history = $responses[self.to]
19
20
 
20
- klass = self.params['class'] || 'LyresController'
21
- method = self.params['method'] || 'hot'
21
+ klass = self.params['class'] || 'LyresController'
22
+ method = self.params['method'] || 'hot'
22
23
 
23
- self.text = klass.constantize.new.send method.to_sym, self, history, self.params
24
+ self.text = klass.constantize.new.send method.to_sym, self, history, self.params
24
25
 
25
- end
26
+ end
26
27
 
27
- def register_response(params)
28
- $responses[self.to] = params
29
- end
28
+ def register_response(params)
29
+ $responses[self.to] = params
30
+ end
30
31
 
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
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
- def post(live=false)
49
+ def post(live=false)
49
50
 
50
- history = $responses[self.to]
51
+ history = $responses[self.to]
51
52
 
52
- msg = self.text
53
+ msg = self.text
53
54
 
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}"
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 "History - #{history.inspect}"
69
- puts
70
- puts "-" * 50
71
- end
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
- if live
75
- $twitter.post(msg)
76
- end
77
- end
75
+ if live
76
+ $twitter.post(msg)
77
+ end
78
+ end
79
+ end
78
80
  end
@@ -1,46 +1,48 @@
1
- class LyreBird::Route
2
- def initialize(options={})
1
+ module LyreBird
2
+ class Route
3
+ def initialize(options={})
3
4
 
4
- end
5
+ end
5
6
 
6
- def self.route(str='', debug=false)
7
+ def self.route(str='', debug=false)
7
8
 
8
- successes = []
9
- puts "Parsing '#{str}'" if DEBUG
10
- $lyrebird_routes.each do | r |
11
- route = r['str']
12
- clear = r['clear']
13
- puts "- Trying '#{route}'" if DEBUG
14
- regex = {}
15
- regex[:vars] = route.gsub(/(:[a-z]*)/, '(:[a-z]*)')
16
- puts "- REGEX VARS '#{regex[:vars]}'" if DEBUG
17
- vars = route.match(regex[:vars])
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
- puts "str = '#{str}'" if CONSOLE and vars.nil?
20
- puts "str.match(/#{regex[:vars]}/)" if CONSOLE and vars.nil?
20
+ puts "str = '#{str}'" if CONSOLE and vars.nil?
21
+ puts "str.match(/#{regex[:vars]}/)" if CONSOLE and vars.nil?
21
22
 
22
- puts "- NO VARS '#{vars.inspect}'" if DEBUG and vars.nil?
23
- next if vars.nil?
23
+ puts "- NO VARS '#{vars.inspect}'" if DEBUG and vars.nil?
24
+ next if vars.nil?
24
25
 
25
- regex[:values] = regex[:vars].gsub(/\:\[a\-z\]/, '.')
26
- puts "- Trying '#{regex[:values]}'" if DEBUG
27
- values = str.match(regex[:values])
26
+ regex[:values] = regex[:vars].gsub(/\:\[a\-z\]/, '.')
27
+ puts "- Trying '#{regex[:values]}'" if DEBUG
28
+ values = str.match(regex[:values])
28
29
 
29
- next if values.nil?
30
+ next if values.nil?
30
31
 
31
- params = {}.merge(r)
32
+ params = {}.merge(r)
32
33
 
33
- i = 1;
34
+ i = 1;
34
35
 
35
- vars[1,vars.size].each do | var |
36
- clean_name = var.gsub(/:/,'')
37
- params[clean_name.to_sym] = values[i]
38
- i += 1
39
- end
40
- params = (str.match(/good|ok|pretty good/).nil?) ? params : params.merge({:sort => :rating})
41
- puts "-- PASS '#{route}' got #{params.inspect}" if DEBUG
42
- successes.unshift(params)
43
- end
44
- return successes.last
45
- end
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
@@ -1,34 +1,36 @@
1
1
  require 'httparty'
2
2
 
3
- class LyreBird::Twitter
4
- include HTTParty
5
- base_uri 'twitter.com'
3
+ module LyreBird
4
+ class Twitter
5
+ include HTTParty
6
+ base_uri 'twitter.com'
6
7
 
7
- def initialize(u, p)
8
- @auth = {:username => u, :password => p}
9
- end
8
+ def initialize(u, p)
9
+ @auth = {:username => u, :password => p}
10
+ end
10
11
 
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
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
- # 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
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
- def post(text)
31
- options = { :query => {:status => text}, :basic_auth => @auth }
32
- self.class.post('/statuses/update.json', options)
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
- class LyreBird
6
+ module LyreBird
7
+ class Base
7
8
 
8
- attr_accessor :username
9
- attr_accessor :password
10
- attr_accessor :since_filename
9
+ attr_accessor :username
10
+ attr_accessor :password
11
+ attr_accessor :since_filename
11
12
 
12
- def initialize(options={})
13
- $responses = {}
14
- self.username = options[:username] || 'username'
15
- self.password = options[:password] || 'password'
16
- self.since_filename = options[:since_filename] || "lyre_bird_listen_since.txt"
17
- $lyrebird_routes = options[:routes] || []
18
- end
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
- def listen(options={})
21
- options = { :limit => 1, :sleep=> 5}.merge(options)
21
+ def listen(options={})
22
+ options = { :limit => 1, :sleep=> 5}.merge(options)
22
23
 
23
- responses = []
24
- :limit.to_i.times do
25
- responses.push(self.check(:post => true))
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
- def get_since
31
- since = 0;
32
- if File.exists?(self.since_filename)
33
- File.open(self.since_filename).each { |line|
34
- since = line
35
- }
36
- return since
37
- else
38
- return nil
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
- def set_since(since_id)
43
- puts "Modified SINCE_ID to - #{since_id}"
44
- since = File.open(self.since_filename, 'w') {|f| f.write(since_id) }
45
- since
46
- end
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
- def check(options={})
49
+ def check(options={})
49
50
 
50
- options = {:live=>true}.merge(options)
51
- responses = []
51
+ options = {:live=>true}.merge(options)
52
+ responses = []
52
53
 
53
- if options[:messages]
54
- puts "DOING AN OFFLINE LYREBIRD CHECK"
55
- mentions = options[:messages]
56
- else
57
- $twitter = LyreBird::Twitter.new(self.username,self.password)
58
- since = options[:since] || get_since()
59
- mentions = $twitter.mentions(:mentions,{ :since_id => since })
60
- end
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
- mentions = mentions.sort {|a,b| a['id']<=>b['id']}
63
- mentions.each do | tweet |
64
- puts "Tweet - #{tweet.keys.to_yaml}" if DEBUG
65
- req = LyreBird::Request.new(:tweet => tweet)
66
- if options[:post]
67
- req.response.post(options[:live])
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
- puts "NO NEW TWEETS @ #{Time.now}" unless mentions.size > 0
73
- set_since(mentions.last['id']) unless mentions.size == 0
74
- end
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simonreed-lyrebird
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.2.1
4
+ version: 0.1.0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Reed