lib/tw/client/auth.rb in tw-0.1.1 vs lib/tw/client/auth.rb in tw-0.2.0

- old
+ new

@@ -1,32 +1,38 @@ module Tw class Client - - def initialize + def auth(user=nil) + Auth.auth user end + end - def auth(user=nil) - user = Conf['default_user'] unless user - if user == nil and Conf['users'].empty? - add_user - return - end - raise ArgumentError, "Argument must be instance of String or Hash." unless [Hash, String].include? user.class - if user.class == String - raise ArgumentError, "user \"#{user}\" not exists." unless Conf['users'].include? user - user = Conf['users'][user] - end + class Auth + + def self.auth(user=nil) + user = get_or_regist_user user Twitter.configure do |c| c.consumer_key = Conf['consumer_key'] c.consumer_secret = Conf['consumer_secret'] c.oauth_token = user['access_token'] c.oauth_token_secret = user['access_secret'] end end - def add_user + def self.get_or_regist_user(user) + return user if user.kind_of? Hash + if user.kind_of? String + raise ArgumentError, "user \"#{user}\" not exists." unless Conf['users'].include? user + return Conf['users'][user] + end + unless user + return Conf['users'][ Conf['default_user'] ] if Conf['default_user'] + return regist_user + end + end + + def self.regist_user consumer = OAuth::Consumer.new(Conf['consumer_key'], Conf['consumer_secret'], :site => 'http://twitter.com') request_token = consumer.get_request_token puts cmd = "open #{request_token.authorize_url}" system cmd @@ -42,9 +48,10 @@ 'id' => u.id } Conf['default_user'] = u.screen_name unless Conf['default_user'] Conf.save puts "add \"@#{u.screen_name}\"" + return Conf['users'][u.screen_name] end end end