Sha256: 06dfec6321114f58ad99bfe519cf307385d58f5bd1d8eaf508e17e07fc10b434

Contents?: true

Size: 1.58 KB

Versions: 4

Compression:

Stored size: 1.58 KB

Contents

module Tw
  class Client

    def initialize
    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
      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
      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
      print 'input PIN Number: '
      verifier = STDIN.gets.strip
      access_token = request_token.get_access_token(:oauth_verifier => verifier)
      self.auth('access_token' => access_token.token,
                'access_secret' => access_token.secret)
      u = Twitter.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}\""
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tw-0.1.1 lib/tw/client/auth.rb
tw-0.1.0 lib/tw/client/auth.rb
tw-0.0.3 lib/tw/client/auth.rb
tw-0.0.2 lib/tw/client/auth.rb