Class: Ubiquitously::Twitter::Account

Inherits:
Service::Account show all
Defined in:
lib/ubiquitously/services/twitter.rb

Instance Attribute Summary

Attributes inherited from Service::Account

#credentials, #ip, #logged_in, #password, #user, #username

Instance Method Summary collapse

Methods inherited from Service::Account

#access_token, #agent, #cookies?, #credentials?, #initialize

Methods included from Account::Authorizable

included

Methods included from Account::Loggable

included

Methods included from Account::Restful

included

Methods inherited from Base

#apply, #debug?

Methods included from SubclassableCallbacks

included, override

Constructor Details

This class inherits a constructor from Ubiquitously::Service::Account

Instance Method Details

#loginObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ubiquitously/services/twitter.rb', line 6

def 
  # 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