Commits

Richo Healey  committed 5891ffe

Finished, it more or less works

  • Participants
  • Parent commits 0e5b7e2

Comments (0)

Files changed (5)

 source :rubygems
 
 gem "twitter"
+gem "oauth"

File lib/actions.rb

 module Twat
+  OAUTH_TOKENS = [ :oauth_token, :oauth_token_secret ]
+  CONSUMER_TOKENS = [ :consumer_key, :consumer_secret ]
   class Actions
+
     def tweet(opts)
       # This is all broken, we should know what options we have before this happend
-      cf = Config.new
       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.update(opts.msg)
+      #puts opts.msg
+    end
+
+    def add(opts)
+      v = Config.consumer_info.map do |key, value|
+        value
       end
+      # FIXME probably allow something other than 
+      # twitter
+      oauth = OAuth::Consumer.new( v[0], v[1],
+              { site: "http://twitter.com" })
+      token_request = oauth.get_request_token()
+      puts "Please authenticate the application at #{token_request.authorize_url}, then enter pin"
+      pin = gets.chomp
+      access_token = token_request.get_access_token(oauth_verifier: pin)
+      cf[opts[:account]] = {
+        oauth_token: access_token.token,
+        oauth_token_secret: access_token.secret
+      }
+      cf.save!
+    end
+
+
+    private
 
-      #Twitter.update(tweet)
-      puts opts.msg
+    def cf
+      @cf ||= Config.new
     end
 
   end

File lib/argparse.rb

 module Twat
+  REQUIRED = [:account]
+  MSG_REQUIRED = [:tweet]
+
   class ArgParse
 
     def usage(additional=nil)
     end
 
     def getopts
-      required = [:account]
       options = Hash.new
       @optparser = OptionParser.new do |opts|
         options[:action] = :tweet
         end
       end
       @optparser.parse!
-      required.each do |req|
+      REQUIRED.each do |req|
         usage unless options[req]
       end
-      options[:msg] = msg
+      if MSG_REQUIRED.include?(options[:action])
+        options[:msg] = msg
+      end
       options
     end
 

File lib/config.rb

       config[key] = value
     end
 
+    def self.consumer_info
+      {
+        consumer_key: "jtI2q3Z4NIJAehBG4ntPIQ",
+        consumer_secret: "H6vQOGGn9qVJJa9lWyTd2s8ZZRXG4kh3SMfvZ2uxOXE"
+      }
+    end
+
   end
 end
 require 'twitter'
 require 'yaml'
 require 'optparse'
+require 'oauth'
 
 %w[config exceptions argparse actions].each do |filename|
   require "#{root}/lib/#{filename}"