Sha256: a5560b88087e397e044bccfd540e2b48f46862abe76932a38753c1e9aabced66

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 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'
    }

    ACCESS_TOKEN = 'access_token.yml'

    public
    def initialize(key, secret)
      @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
      if ::File.exists?(ACCESS_TOKEN)
        ::File.delete(ACCESS_TOKEN)
      end
    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
      if ::File.exists?(ACCESS_TOKEN)
        @access_token = ::YAML.load_file(ACCESS_TOKEN)
      end
    end

    def persist_access_token
      ::File.open(ACCESS_TOKEN, 'w') do |out|
        YAML.dump(@access_token, out)
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
twiauth-0.0.2 lib/twiauth/oauth.rb
twiauth-0.0.1 lib/twiauth/oauth.rb