Sha256: 815dbd2712c8b8ea432b68e7452b1eb29538aa9e58bf31573fa56d35b56be9b2

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

module Ubiquitously
  module Twitter
    class Account < Ubiquitously::Service::Account
      uses :oauth
      
      def login
        # only login if we don't have cookies
        url = "http://localhost:4567/"
        hash = ::TwitterToken.authorize(url)
        options = {:url => hash[:url]}
        if cookies?
          raise "COOKIES"
          options.merge!(:headers => {"Cookie" => user.cookies_for(:twitter)})
          page = agent.get(options, [], url)
        else
          page = agent.get(options, [], url)
          form = page.forms.first
          form["session[username_or_email]"] = username
          form["session[password]"] = password
          page = form.submit
        end
        
        if page.uri.to_s =~ /http:\/\/api\.twitter\.com\/oauth\/authenticate/i
          if page.links.first.href !~ /http:\/\/localhost/
            page = page.forms.last.submit
          end
        end
        
        authorize!(page.title =~ /Redirecting you back to the application/i)
        
        location = URI.parse(page.links.first.href)
        verifier = Rack::Utils.parse_query(location.query)["oauth_verifier"]
        
        # do something with the oauth token, save it in the cookie?
        self.credentials = ::TwitterToken.access(
          :token => hash[:token],
          :secret => hash[:secret],
          :oauth_verifier => verifier
        )
        
        logged_in?
      end
    end
    
    class Post < Ubiquitously::Service::Post
      def create
        response = access_token.post("/statuses/update.json", {
          "status" => token[:description],
          "include_entities" => true
        })
        
        result = JSON.parse(response.body)
        
        if result.has_key?("error")
          raise result["error"].to_s
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ubiquitously-0.1.0 lib/ubiquitously/services/twitter.rb