twat 0.6.1 → 0.6.2

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.
@@ -6,8 +6,6 @@ require 'yaml'
6
6
  require 'optparse'
7
7
  require 'oauth'
8
8
 
9
- require 'readline-ng'
10
-
11
9
  %w[config exceptions argparse actions migration options endpoint].each do |filename|
12
10
  require "twat/#{filename}"
13
11
  end
@@ -51,7 +49,7 @@ end
51
49
  module Twat
52
50
  VERSION_MAJOR = 0
53
51
  VERSION_MINOR = 6
54
- VERSION_PATCH = 1
52
+ VERSION_PATCH = 2
55
53
 
56
54
  VERSION = "#{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_PATCH}"
57
55
  class Twat
@@ -61,12 +59,14 @@ module Twat
61
59
  def cli_run
62
60
  # FIXME Handing in the opts like this feels dirty
63
61
  with_handled_exceptions(ArgParse.new) do |opts|
64
- actor = Actions.new
65
-
66
62
  if opts[:account] && opts[:action] != :add
67
63
  config.account = opts[:account]
68
64
  end
69
65
 
66
+ require 'readline-ng'
67
+ actor = Actions.new
68
+
69
+
70
70
  actor.config = config
71
71
  actor.opts = opts
72
72
  actor.send(opts.options[:action])
@@ -17,6 +17,10 @@ module Twat
17
17
  "%02d" % n
18
18
  end
19
19
 
20
+ def reader
21
+ @reader ||= ReadlineNG::Reader.new
22
+ end
23
+
20
24
  # Format a tweet all pretty like
21
25
  def format(twt, idx = nil)
22
26
  idx = pad(idx) if idx
@@ -15,8 +15,8 @@ module Twat
15
15
 
16
16
  oauth = OAuth::Consumer.new( v[0], v[1], oauth_options )
17
17
  token_request = oauth.get_request_token()
18
- puts "Please authenticate the application at #{token_request.authorize_url}, then enter pin"
19
- pin = STDIN.gets.chomp
18
+ reader.puts_above "Please authenticate the application at #{token_request.authorize_url} , then enter pin"
19
+ pin = reader.get_line
20
20
  begin
21
21
  access_token = token_request.get_access_token(oauth_verifier: pin)
22
22
  account_settings = {
@@ -24,10 +24,11 @@ module Twat
24
24
  oauth_token_secret: access_token.secret,
25
25
  endpoint: opts[:endpoint]
26
26
  }
27
+ config.create_unless_exists!
27
28
  config.accounts[opts[:account]] = account_settings
28
29
  config.save!
29
30
  rescue OAuth::Unauthorized
30
- puts "Couldn't authenticate you, did you enter the pin correctly?"
31
+ reader.puts_above "Couldn't authenticate you, did you enter the pin correctly?"
31
32
  end
32
33
  end
33
34
 
@@ -44,9 +44,9 @@ module Twat
44
44
  class Actions
45
45
 
46
46
  def initialize
47
- @reader = ReadlineNG::Reader.new
47
+ @failcount = 0
48
48
 
49
- def @reader.filter
49
+ def reader.filter
50
50
  case @buf.length
51
51
  when 140
52
52
  _print ""
@@ -56,6 +56,15 @@ module Twat
56
56
  end
57
57
  end
58
58
 
59
+ def fail_or_bail
60
+ if @failcount > 2
61
+ puts "3 consecutive failures, giving up"
62
+ else
63
+ @failcount += 1
64
+ return true
65
+ end
66
+ end
67
+
59
68
  def follow
60
69
  twitter_auth
61
70
  failcount = 0
@@ -68,8 +77,8 @@ module Twat
68
77
  last_id = process_followed(tweets) if tweets.any?
69
78
  (config.polling_interval * POLLING_RESOLUTION).times do
70
79
  begin
71
- @reader.tick
72
- @reader.each_line { |inp| handle_input(inp) }
80
+ reader.tick
81
+ reader.each_line { |inp| handle_input(inp) }
73
82
  rescue TweetTooLong
74
83
  puts "Too long".red
75
84
  end
@@ -79,23 +88,13 @@ module Twat
79
88
  failcount = 0
80
89
  rescue Interrupt
81
90
  break
82
- # rescue Twitter::ServiceUnavailable
83
- # if failcount > 2
84
- # puts "3 consecutive failures, giving up"
85
- # else
86
- # sleeptime = 60 * (failcount + 1)
87
- # print "(__-){".red
88
- # puts ": the fail whale has been rolled out, sleeping for #{sleeptime} seconds"
89
- # sleep sleeptime
90
- # failcount += 1
91
- # end
92
- rescue Errno::ECONNRESET
93
- rescue Errno::ETIMEDOUT
94
- if failcount > 2
95
- puts "3 consecutive failures, giving up"
96
- else
97
- failcount += 1
98
- end
91
+ rescue Twitter::Error::ServiceUnavailable
92
+ break unless fail_or_bail
93
+ sleeptime = 60 * (@failcount + 1)
94
+ reader.puts_above "#{"(__-){".red}: the fail whale has been rolled out, sleeping for #{sleeptime} seconds"
95
+ sleep sleeptime
96
+ rescue Errno::ECONNRESET, Errno::ETIMEDOUT, SocketError
97
+ break unless fail_or_bail
99
98
  end
100
99
  end
101
100
  end
@@ -112,7 +111,7 @@ module Twat
112
111
  tweets.reverse.each do |tweet|
113
112
  id = @tweetstack << tweet
114
113
  beep if config.beep? && tweet.text.mentions?(config.account_name)
115
- @reader.puts_above format(tweet, @tweetstack.last)
114
+ reader.puts_above format(tweet, @tweetstack.last)
116
115
  last_id = tweet.id
117
116
  end
118
117
 
@@ -130,7 +129,7 @@ module Twat
130
129
  when /follow (.*)/
131
130
  follow_user($1)
132
131
  when /test/
133
- @reader.puts_above "Testline!"
132
+ reader.puts_above "Testline!"
134
133
  else
135
134
  # Assume they want to tweet something
136
135
  raise TweetTooLong if inp.length > 140
@@ -18,6 +18,14 @@ module Twat
18
18
  @config_path ||= ENV['TWAT_CONFIG'] || "#{ENV['HOME']}/.twatrc"
19
19
  end
20
20
 
21
+ def create_unless_exists!
22
+ begin
23
+ config
24
+ rescue NoConfigFile
25
+ @config = { accounts: {} }
26
+ end
27
+ end
28
+
21
29
  def config
22
30
  begin
23
31
  @config ||= YAML.load_file(config_path)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.6.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-11 00:00:00.000000000 Z
12
+ date: 2012-02-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: twitter
16
- requirement: &12531360 !ruby/object:Gem::Requirement
16
+ requirement: &18009540 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *12531360
24
+ version_requirements: *18009540
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: oauth
27
- requirement: &13194720 !ruby/object:Gem::Requirement
27
+ requirement: &18008460 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *13194720
35
+ version_requirements: *18008460
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: readline-ng
38
- requirement: &13194200 !ruby/object:Gem::Requirement
38
+ requirement: &18332540 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *13194200
46
+ version_requirements: *18332540
47
47
  description: Command line tool for tweeting and whatnot
48
48
  email:
49
49
  - richo@psych0tik.net