Commits

Richo Healey committed c91c613 Merge

Merge branch 'master' into rewrite

Comments (0)

Files changed (5)

+twat -l -> <newsfeed>
+twat -l [count] -> <count entries from newsfeed>
+# Include who tweeted...
+
+catch invalid options and return opts
 require 'optparse'
 require 'oauth'
 
-Dir.glob("twat/*").each do |filename|
-  require filename
+%w[config exceptions argparse actions].each do |filename|
+  require "twat/#{filename}"
 end
 
 module Twat
   VERSION_MAJOR = 0
-  VERSION_MINOR = 2
-  VERSION_PATCH = 5
+  VERSION_MINOR = 3
+  VERSION_PATCH = 0
 
   VERSION = "#{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_PATCH}"
   class Twat
       rescue NoConfigFile
         puts "No config file, create one with twat -a [user|nick]"
         opts.usage
+      rescue InvalidCredentials
+        puts "Invalid credentials, try reauthenticating with"
+        puts "twat -a #{opts[:account]}"
       end
     end
   end

lib/twat/actions.rb

 
     def tweet(opts)
       # This is all broken, we should know what options we have before this happend
-      conf = cf[opts[:account]]
-
-      Twitter.configure do |twit|
-        conf.each do |key, value|
-          twit.send("#{key}=", value)
-        end
-        Config.consumer_info.each do |key, value|
-          twit.send("#{key}=", value)
-        end
-      end
+      twitter_auth(opts)
 
       Twitter.update(opts.msg)
       #puts opts.msg
       end
     end
 
+    def show(opts)
+      twitter_auth(opts)
+      Twitter.home_timeline.each_with_index do |tweet, idx|
+        puts "#{tweet.user.screen_name}: #{tweet.text}"
+
+        break if idx == opts[:count]
+      end
+    end
+
+    def version(opts)
+      puts "twat: #{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_PATCH}"
+    end
+
     private
 
     def cf
       @cf ||= Config.new
     end
 
+    def twitter_auth(opts)
+      conf = cf[opts[:account]]
+      Twitter.configure do |twit|
+        conf.each do |key, value|
+          twit.send("#{key}=", value)
+        end
+        Config.consumer_info.each do |key, value|
+          twit.send("#{key}=", value)
+        end
+      end
+    end
+
   end
 end

lib/twat/argparse.rb

         options[:action] = :tweet
         options[:account] = :default
         opts.banner = "Usage: twat <tweet>"
-        opts.on('-n', '--account ACCOUNT', 'Tweet from ACCOUNT (or default)') do |acct|
+
+        opts.on('-n', '--account ACCOUNT', 'Use ACCOUNT (or default)') do |acct| #{{{ --account ACCOUNT
           options[:account] = acct.to_sym
-        end
-        opts.on('-a', '--add ACCOUNT', 'Configure and authorise ACCOUNT') do |acct|
+        end # }}}
+        opts.on('-a', '--add ACCOUNT', 'Configure and authorise ACCOUNT') do |acct| #{{{ --add ACCOUNT
           options[:account] = acct.to_sym
           options[:action] = :add
-        end
-        opts.on('-d', '--delete ACCOUNT', 'Delete ACCOUNT') do |acct|
+        end #}}}
+        opts.on('-d', '--delete ACCOUNT', 'Delete ACCOUNT') do |acct| #{{{ --delete ACCOUNT
           options[:account] = acct.to_sym
           options[:action] = :delete
-        end
-        #opts.on( '-a' '--add ACCOUNT' ) do |acct|
-        #end
-        opts.on('-h', '--help', 'Display this screen') do
+        end #}}}
+        opts.on('-h', '--help', 'Display this screen') do #{{{ --help
           puts opts
           exit
-        end
+        end #}}}
+        opts.on('-l', '--list [COUNT]', 'Display [count] tweets from your newsfeed') do |count| #{{{ --list ACCOUNT
+          options[:count] = count || 10
+          options[:action] = :show
+        end #}}}
+        opts.on('-v', '--version', 'Display version info') do #{{{ --version
+          options[:action] = :version
+        end #}}}
       end
+
       @optparser.parse!
       REQUIRED.each do |req|
         usage unless options[req]
     end
 
     def options
-      @configthingfucken ||= getopts
+      begin
+        @configthingfucken ||= getopts
+      rescue OptionParser::InvalidOption
+        usage "Unknown option"
+      end
     end
 
   end

lib/twat/exceptions.rb

   class NoSuchAccount < Exception; end
   class NoConfigFile < Exception; end
   class Usage < Exception; end
+  class InvalidCredentials < Exception; end
 end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.