twat 0.3.5 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,7 @@ require 'yaml'
6
6
  require 'optparse'
7
7
  require 'oauth'
8
8
 
9
- %w[config exceptions argparse actions].each do |filename|
9
+ %w[config exceptions argparse actions migration].each do |filename|
10
10
  require "twat/#{filename}"
11
11
  end
12
12
 
@@ -15,6 +15,18 @@ class String
15
15
  "#{self}"
16
16
  end
17
17
 
18
+ def green
19
+ "#{self}"
20
+ end
21
+
22
+ def yellow
23
+ "#{self}"
24
+ end
25
+
26
+ def blue
27
+ "#{self}"
28
+ end
29
+
18
30
  def cyan
19
31
  "#{self}"
20
32
  end
@@ -26,8 +38,8 @@ end
26
38
 
27
39
  module Twat
28
40
  VERSION_MAJOR = 0
29
- VERSION_MINOR = 3
30
- VERSION_PATCH = 5
41
+ VERSION_MINOR = 4
42
+ VERSION_PATCH = 0
31
43
 
32
44
  VERSION = "#{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_PATCH}"
33
45
  class Twat
@@ -43,6 +55,8 @@ module Twat
43
55
  rescue NoSuchAccount
44
56
  puts "No such account"
45
57
  opts.usage
58
+ rescue NoDefaultAccount
59
+ puts "No default account configured."
46
60
  rescue NoSuchCommand
47
61
  puts "No such command"
48
62
  opts.usage
@@ -52,6 +66,8 @@ module Twat
52
66
  rescue InvalidCredentials
53
67
  puts "Invalid credentials, try reauthenticating with"
54
68
  puts "twat -a #{opts[:account]}"
69
+ rescue ConfigVersionIncorrect
70
+ puts "Your config file is out of date. Run with --update-config to rememdy"
55
71
  end
56
72
  end
57
73
  end
@@ -25,7 +25,7 @@ module Twat
25
25
  pin = gets.chomp
26
26
  begin
27
27
  access_token = token_request.get_access_token(oauth_verifier: pin)
28
- config[opts[:account]] = {
28
+ config.accounts[opts[:account]] = {
29
29
  oauth_token: access_token.token,
30
30
  oauth_token_secret: access_token.secret
31
31
  }
@@ -36,7 +36,7 @@ module Twat
36
36
  end
37
37
 
38
38
  def delete
39
- if config.delete(opts[:account])
39
+ if config.accounts.delete(opts[:account])
40
40
  config.save!
41
41
  puts "Successfully deleted"
42
42
  else
@@ -44,10 +44,24 @@ module Twat
44
44
  end
45
45
  end
46
46
 
47
+ def setdefault
48
+ unless config.accounts.include?(opts[:account])
49
+ raise NoSuchAccount
50
+ end
51
+
52
+ config[:default] = opts[:account]
53
+ config.save!
54
+ puts "Successfully set #{opts[:account]} as default"
55
+ end
56
+
57
+ def updateconfig
58
+ config.update!
59
+ end
60
+
47
61
  def show
48
62
  twitter_auth
49
63
  Twitter.home_timeline.each_with_index do |tweet, idx|
50
- puts "#{tweet.user.screen_name.bold.cyan}: #{tweet.text}"
64
+ format(tweet)
51
65
 
52
66
  break if idx == opts[:count]
53
67
  end
@@ -75,6 +89,16 @@ module Twat
75
89
 
76
90
  private
77
91
 
92
+ # Format a tweet all pretty like
93
+ def format(twt)
94
+ # if config.color
95
+ if twt.user.screen_name == account_name.to_s
96
+ puts "#{twt.user.screen_name.bold.blue}: #{twt.text}"
97
+ else
98
+ puts "#{twt.user.screen_name.bold.cyan}: #{twt.text}"
99
+ end
100
+ end
101
+
78
102
  def twitter_auth
79
103
  Twitter.configure do |twit|
80
104
  account.each do |key, value|
@@ -87,7 +111,16 @@ module Twat
87
111
  end
88
112
 
89
113
  def account
90
- @account ||= config[opts[:account]]
114
+ @account = config.accounts[account_name]
115
+ end
116
+
117
+ def account_name
118
+ @account_name ||=
119
+ if opts.include?(:account)
120
+ opts[:account]
121
+ else
122
+ config.default_account
123
+ end
91
124
  end
92
125
 
93
126
  end
@@ -1,5 +1,5 @@
1
1
  module Twat
2
- REQUIRED = [:account]
2
+ REQUIRED = []
3
3
  MSG_REQUIRED = [:tweet]
4
4
 
5
5
  class ArgParse
@@ -14,7 +14,6 @@ module Twat
14
14
  options = Hash.new
15
15
  @optparser = OptionParser.new do |opts|
16
16
  options[:action] = :tweet
17
- options[:account] = :default
18
17
  opts.banner = "Usage: twat <tweet>"
19
18
 
20
19
  opts.on('-n', '--account ACCOUNT', 'Use ACCOUNT (or default)') do |acct| #{{{ --account ACCOUNT
@@ -43,6 +42,13 @@ module Twat
43
42
  options[:user] = (user || :default)
44
43
  options[:action] = :user_feed
45
44
  end #}}}
45
+ opts.on("--set-default ACCOUNT", 'Set ACCOUNT as default') do |acct| #{{{ --set-default ACCOUNT
46
+ options[:action] = :setdefault
47
+ options[:account] = acct.to_sym
48
+ end #}}}
49
+ opts.on("--update-config", "Update config to latest version") do #{{{ --update-config
50
+ options[:action] = :updateconfig
51
+ end #}}}
46
52
  end
47
53
 
48
54
  @optparser.parse!
@@ -64,6 +70,10 @@ module Twat
64
70
  options[key]
65
71
  end
66
72
 
73
+ def include?(key)
74
+ options.include?(key)
75
+ end
76
+
67
77
  def options
68
78
  begin
69
79
  @configthingfucken ||= getopts
@@ -21,11 +21,18 @@ module Twat
21
21
  def config
22
22
  begin
23
23
  @config ||= YAML.load_file(config_path)
24
+ validate!(@config)
24
25
  rescue Errno::ENOENT
25
26
  raise NoConfigFile
26
27
  end
27
28
  end
28
29
 
30
+ def validate!(conf)
31
+ # TODO Proper checks, instead of a series of hackish checks
32
+ raise ConfigVersionIncorrect unless conf.include?(:accounts)
33
+ conf
34
+ end
35
+
29
36
  def save!
30
37
  File.open(config_path, 'w') do |conf|
31
38
  conf.chmod(0600)
@@ -37,17 +44,13 @@ module Twat
37
44
  self[meth]
38
45
  end
39
46
 
40
- def [](key)
41
- raise NoSuchAccount unless config.include?(key)
42
- config[key]
43
- end
44
-
45
- def []=(key, value)
46
- config[key] = value
47
+ def accounts
48
+ return config[:accounts]
47
49
  end
48
50
 
49
- def delete(key)
50
- config.delete(key)
51
+ def default_account
52
+ raise NoDefaultAccount unless config.include?(:default)
53
+ return config[:default].to_sym
51
54
  end
52
55
 
53
56
  def self.consumer_info
@@ -57,5 +60,17 @@ module Twat
57
60
  }
58
61
  end
59
62
 
63
+ def []=(k, v)
64
+ config[k] = v
65
+ end
66
+
67
+ # update! migrates an old config file to the current API
68
+ # it does this by calling a sequence of migration functions in order
69
+ # which rebuild the config in stages, saving and leaving it in a
70
+ # consistent step at each point
71
+ def update!
72
+ Migrate.new.migrate!(config_path)
73
+ end
74
+
60
75
  end
61
76
  end
@@ -1,7 +1,9 @@
1
1
  module Twat
2
2
  class NoSuchAccount < Exception; end
3
+ class NoDefaultAccount < Exception; end
3
4
  class NoSuchCommand < Exception; end
4
5
  class NoConfigFile < Exception; end
5
6
  class Usage < Exception; end
6
7
  class InvalidCredentials < Exception; end
8
+ class ConfigVersionIncorrect < Exception; end
7
9
  end
@@ -0,0 +1,39 @@
1
+ module Twat
2
+ class Migrate
3
+ def migrate!(filename)
4
+ @file = filename
5
+ migration_1
6
+ end
7
+
8
+ def migration_1
9
+ new = { accounts: {} }
10
+ default = nil
11
+ current = YAML.load_file(@file)
12
+ current.each do |k, v|
13
+ k = k.to_sym
14
+ if k == :default
15
+ default = v[:oauth_token]
16
+ else
17
+ new[:accounts][k] = v
18
+ end
19
+ end
20
+
21
+ new[:accounts].each do |k, v|
22
+ if v[:oauth_token] == default
23
+ new[:default] = k
24
+ break
25
+ end
26
+ end
27
+
28
+ save(new)
29
+ end
30
+
31
+ def save(cf)
32
+ File.open(@file, 'w') do |conf|
33
+ conf.chmod(0600)
34
+ conf.puts(cf.to_yaml)
35
+ end
36
+ end
37
+
38
+ end
39
+ end
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.3.5
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-09-30 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: twitter
16
- requirement: &82825950 !ruby/object:Gem::Requirement
16
+ requirement: &26665120 !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: *82825950
24
+ version_requirements: *26665120
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: oauth
27
- requirement: &82825650 !ruby/object:Gem::Requirement
27
+ requirement: &26664500 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *82825650
35
+ version_requirements: *26664500
36
36
  description: Command line tool for tweeting and whatnot
37
37
  email:
38
38
  - richo@psych0tik.net
@@ -53,6 +53,7 @@ files:
53
53
  - lib/twat/argparse.rb
54
54
  - lib/twat/config.rb
55
55
  - lib/twat/exceptions.rb
56
+ - lib/twat/migration.rb
56
57
  - twat.gemspec
57
58
  homepage: http://github.com/richoH/twat
58
59
  licenses: []