Commits

Richo Healey committed 17acf35 Merge

Merge branch 'rewrite'

  • Participants
  • Parent commits 105d413, 2cd88a0

Comments (0)

Files changed (5)

 # Include who tweeted...
 
 catch invalid options and return opts
+
+
+CONFIG FILE
+===========
+
+This really does need to change, but I'm not sure where on the "Can piss off a userbase" scale I am, (ie, do I have one)
+
+For now I'm going to leave it, and if it gets to 1.0 I'll fix it then.
 require 'twat'
 
 inst = Twat::Twat.new
-inst.run
+inst.cli_run
 
   VERSION = "#{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_PATCH}"
   class Twat
-    def run
+    def cli_run
       begin
-        action = Actions.new
         opts = ArgParse.new
-        action.send(opts.options[:action], opts)
+        actor = Actions.new
+        actor.config = config
+        actor.opts = opts
+        actor.send(opts.options[:action])
       rescue Usage
         opts.usage
       rescue NoSuchAccount

lib/twat/actions.rb

   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
-      twitter_auth(opts)
+    attr_accessor :config, :opts
+
+    def tweet
+      twitter_auth
 
       Twitter.update(opts.msg)
       #puts opts.msg
     end
 
-    def add(opts)
+    def add
       v = Config.consumer_info.map do |key, value|
         value
       end
       pin = gets.chomp
       begin
         access_token = token_request.get_access_token(oauth_verifier: pin)
-        cf[opts[:account]] = {
+        config[opts[:account]] = {
           oauth_token: access_token.token,
           oauth_token_secret: access_token.secret
         }
-        cf.save! 
+        config.save!
       rescue OAuth::Unauthorized
         puts "Couldn't authenticate you, did you enter the pin correctly?"
       end
     end
 
-    def delete(opts)
-      if cf.delete(opts[:account])
-        cf.save!
+    def delete
+      if config.delete(opts[:account])
+        config.save!
         puts "Successfully deleted"
       else
         puts "No such account"
       end
     end
 
-    def show(opts)
-      twitter_auth(opts)
+    def show
+      twitter_auth
       Twitter.home_timeline.each_with_index do |tweet, idx|
         puts "#{tweet.user.screen_name}: #{tweet.text}"
 
       end
     end
 
-    def version(opts)
+    def version
       puts "twat: #{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_PATCH}"
     end
 
     private
 
-    def cf
-      @cf ||= Config.new
-    end
-
-    def twitter_auth(opts)
-      conf = cf[opts[:account]]
+    def twitter_auth
       Twitter.configure do |twit|
-        conf.each do |key, value|
+        account.each do |key, value|
           twit.send("#{key}=", value)
         end
         Config.consumer_info.each do |key, value|
       end
     end
 
+    def account
+      @account ||= config[opts[:account]]
+    end
+
   end
 end

lib/twat/config.rb

-require 'ostruct'
-
 module Twat
-  class Config #< OpenStruct
+  class Twat
+    def configure(&block)
+      yield config
+
+      # If I understand correctly, I can check over what's
+      # happened here?
+    end
+
+    def config
+      @config ||= Config.new
+    end
+  end
+
+  class Config
 
     def config_path
       @config_path ||= ENV['TWAT_CONFIG'] || "#{ENV['HOME']}/.twatrc"
       end
     end
 
+    def method_missing(meth)
+      self[meth]
+    end
+
     def [](key)
       raise NoSuchAccount unless config.include?(key)
       config[key]