Commits

Richo Healey committed 6ce1949 Merge

Merge branch 'newsfeed'

Comments (0)

Files changed (4)

       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.from_user}: #{tweet.text}"
+
+        break if idx == opts[:count]
+      end
+    end
+
     def version(opts)
       puts "twat: #{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_PATCH}"
     end
       @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[:account] = :default
         opts.banner = "Usage: twat <tweet>"
 
-        opts.on('-n', '--account ACCOUNT', 'Tweet from ACCOUNT (or default)') do |acct| #{{{ --account ACCOUNT
+        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| #{{{ --add ACCOUNT
           puts opts
           exit
         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 #}}}

lib/twat/exceptions.rb

   class NoSuchAccount < Exception; end
   class NoConfigFile < Exception; end
   class Usage < Exception; end
+  class InvalidCredentials < Exception; end
 end