Class: Twat::Actions
- Inherits:
-
Object
show all
- Defined in:
- lib/twat/actions.rb,
lib/twat/actions/add.rb,
lib/twat/actions/show.rb,
lib/twat/actions/tweet.rb,
lib/twat/actions/delete.rb,
lib/twat/actions/follow.rb,
lib/twat/actions/version.rb,
lib/twat/actions/setoption.rb,
lib/twat/actions/user_feed.rb,
lib/twat/actions/follow_user.rb,
lib/twat/actions/updateconfig.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Actions.
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/twat/actions/follow.rb', line 46
def initialize
@failcount = 0
def reader.filter
case @buf.length
when 140
_print "[31m"
when 139
_print "[39m"
end
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
10
11
12
|
# File 'lib/twat/actions.rb', line 10
def method_missing(sym, *args, &block)
raise NoSuchCommand
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
8
9
10
|
# File 'lib/twat/actions.rb', line 8
def config
@config
end
|
#failcount ⇒ Object
Returns the value of attribute failcount.
8
9
10
|
# File 'lib/twat/actions.rb', line 8
def failcount
@failcount
end
|
#opts ⇒ Object
Returns the value of attribute opts.
8
9
10
|
# File 'lib/twat/actions.rb', line 8
def opts
@opts
end
|
Instance Method Details
#add ⇒ Object
Add is somewhat of a special case, everything else hangs off config for it’s magic, However we’re forced to do it manually here- config doesn’t know anything about it yet
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/twat/actions/add.rb', line 7
def add
endpoint = Endpoint.new(opts[:endpoint])
v = endpoint.consumer_info.map do |key, value|
value
end
oauth_options = { :site => endpoint.url }
oauth_options.merge!(endpoint.oauth_options)
oauth = OAuth::Consumer.new( v[0], v[1], oauth_options )
token_request = oauth.get_request_token()
reader.puts_above "Please authenticate the application at #{token_request.authorize_url} , then enter pin"
pin = reader.get_line
begin
access_token = token_request.get_access_token(oauth_verifier: pin)
account_settings = {
oauth_token: access_token.token,
oauth_token_secret: access_token.secret,
endpoint: opts[:endpoint]
}
config.create_unless_exists!
config.accounts[opts[:account]] = account_settings
config.save!
rescue OAuth::Unauthorized
reader.puts_above "Couldn't authenticate you, did you enter the pin correctly?"
end
end
|
#delete ⇒ Object
4
5
6
7
8
9
10
11
|
# File 'lib/twat/actions/delete.rb', line 4
def delete
if config.accounts.delete(opts[:account])
config.save!
puts "Successfully deleted"
else
puts "No such account"
end
end
|
#fail_or_bail ⇒ Object
59
60
61
62
63
64
65
66
|
# File 'lib/twat/actions/follow.rb', line 59
def fail_or_bail
if @failcount > 2
puts "3 consecutive failures, giving up"
else
@failcount += 1
return true
end
end
|
#follow ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/twat/actions/follow.rb', line 68
def follow
failcount = 0
@tweetstack = TweetStack.new
= Twitter.home_timeline(:count => 5)
while true do
begin
last_id = process_followed() if .any?
(config.polling_interval * POLLING_RESOLUTION).times do
begin
reader.tick
reader.each_line { |inp| handle_input(inp) }
rescue TweetTooLong
puts "Too long".red
end
sleep 1.0/POLLING_RESOLUTION
end
= Twitter.home_timeline(:since_id => last_id)
failcount = 0
rescue Interrupt
break
rescue Twitter::Error::ServiceUnavailable
break unless fail_or_bail
sleeptime = 60 * (@failcount + 1)
reader.puts_above "#{"(__-){".red}: the fail whale has been rolled out, sleeping for #{sleeptime} seconds"
sleep sleeptime
rescue Errno::ECONNRESET, Errno::ETIMEDOUT, SocketError
break unless fail_or_bail
end
end
end
|
#follow_user(user = nil) ⇒ Object
4
5
6
7
8
|
# File 'lib/twat/actions/follow_user.rb', line 4
def follow_user(user = nil)
Twitter.follow(user || opts[:user])
end
|
#setoption ⇒ Object
4
5
6
7
8
9
10
11
|
# File 'lib/twat/actions/setoption.rb', line 4
def setoption
k, v = opts[:optval].split("=")
raise RequiresOptVal unless v
options = Options.new
options.send(:"#{k}=", v)
puts "Successfully set #{k} as #{v}"
end
|
#show ⇒ Object
4
5
6
7
8
9
|
# File 'lib/twat/actions/show.rb', line 4
def show
Twitter.home_timeline(:count => opts[:count]).reverse.each do ||
format()
end
end
|
4
5
6
7
8
9
10
11
|
# File 'lib/twat/actions/tweet.rb', line 4
def
raise TweetTooLong if opts.msg.length > 140
Twitter.update(opts.msg)
end
|
#updateconfig ⇒ Object
4
5
6
|
# File 'lib/twat/actions/updateconfig.rb', line 4
def updateconfig
config.update!
end
|
#user_feed ⇒ Object
4
5
6
7
8
9
10
11
12
13
14
|
# File 'lib/twat/actions/user_feed.rb', line 4
def user_feed
begin
Twitter.user_timeline(opts[:user], :count => opts[:count]).each do ||
format()
end
rescue Twitter::NotFound
puts "#{opts[:user].bold.red} doesn't appear to be a valid user"
end
end
|