Sha256: 07c3965fba3a0bbebf9506a3ee8b606fed7012487470271683f3b371c917750f

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

module TwiAuth
  # oauth communication wrapper
  class OAuth
    TWITTER_OAUTH_SPEC = {
            :site => 'https://api.twitter.com',
            :request_token_path => '/oauth/request_token',
            :access_token_path => '/oauth/access_token',
            :authorize_path => '/oauth/authorize'
    }

    # key for access token
    ACCESS_TOKEN = 'ACCESS_TOKEN'

    public
    def initialize(key, secret)
      @store = TwiAuth::Store.new
      @oauth_consumer = ::OAuth::Consumer.new(key, secret, TWITTER_OAUTH_SPEC)
      get_access_token
    end

    def get(path)
      @access_token.get(path)
    end

    def post(path, body = nil)
      @access_token.post(path, body)
    end

    def drop_access_token
      @store.delete(ACCESS_TOKEN)
    end

    private
    def get_request_token
      @oauth_consumer.get_request_token
    end

    def get_access_token
      pull_access_token
      if @access_token.nil?
        get_initial_access_token
      end
    end

    def get_initial_access_token
      request_token = get_request_token
      puts "point your browser to \"#{request_token.authorize_url}\" and enter the pin: "
      pin = STDIN.readline.chomp!

      if pin.nil? || pin.empty?
        raise Error
      end

      @access_token = request_token.get_access_token(:oauth_verifier => pin)
      persist_access_token
    end

    def pull_access_token
      @access_token = @store.get(ACCESS_TOKEN)
    end

    def persist_access_token
      @store.put(ACCESS_TOKEN,@access_token)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
twiauth-0.1.0 lib/twiauth/oauth.rb