Sha256: 031d44175d2597e4fd0295d820af09f38f39a2bf67012d40d40586fef1ed4c11

Contents?: true

Size: 1.98 KB

Versions: 11

Compression:

Stored size: 1.98 KB

Contents

module Tw
  class Client
    def self.client
      @@client
    end

    def self.client=(client)
      @@client = client
    end

    def auth(user=nil)
      self.class.client = @rest_client = Auth.auth(user)
    end
  end

  class Auth

    def self.auth(user=nil)
      user = get_or_regist_user user
      return Twitter::REST::Client.new do |c|
        c.consumer_key = Conf['consumer_key']
        c.consumer_secret = Conf['consumer_secret']
        c.access_token = user['access_token']
        c.access_token_secret = user['access_secret']
      end
    end

    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 => 'https://api.twitter.com')
      request_token = consumer.get_request_token
      puts "open #{request_token.authorize_url}"
      begin
        Launchy.open request_token.authorize_url
      rescue
        STDERR.puts "couldn't open web-browser"
      end
      print 'input PIN Number: '
      verifier = STDIN.gets.strip
      access_token = request_token.get_access_token(:oauth_verifier => verifier)
      client = Tw::Client.new.auth('access_token' => access_token.token,
                      'access_secret' => access_token.secret)
      u = client.user
      Conf['users'][u.screen_name] = {
        'access_token' => access_token.token,
        'access_secret' => access_token.secret,
        '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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
tw-1.3.1 lib/tw/client/auth.rb
tw-1.3.0 lib/tw/client/auth.rb
tw-1.2.1 lib/tw/client/auth.rb
tw-1.2.0 lib/tw/client/auth.rb
tw-1.1.1 lib/tw/client/auth.rb
tw-1.1.0 lib/tw/client/auth.rb
tw-1.0.12 lib/tw/client/auth.rb
tw-1.0.11 lib/tw/client/auth.rb
tw-1.0.10 lib/tw/client/auth.rb
tw-1.0.9 lib/tw/client/auth.rb
tw-1.0.8 lib/tw/client/auth.rb