Class: Google::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/google/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email, password) ⇒ Base

Creates a new instance of the connection class using the given email and password and attempts to login



86
87
88
89
# File 'lib/google/base.rb', line 86

def initialize(email, password)
  @email, @password = email, password
  
end

Instance Attribute Details

#sidObject

Session id returned from google login request



82
83
84
# File 'lib/google/base.rb', line 82

def sid
  @sid
end

Class Method Details

.connectionObject

Returns the current connection



25
26
27
# File 'lib/google/base.rb', line 25

def self.connection
  @@connection
end

.connection=(new_connection) ⇒ Object

Changes the current connection to the one provided. If in an app you store the connection in a session, you can reuse it instead of establishing a new connection with each request.

Usage:

Google::Base.connection = session[:connection] # => or whatever


36
37
38
# File 'lib/google/base.rb', line 36

def self.connection=(new_connection)
  @@connection = new_connection
end

.establish_connection(email, password) ⇒ Object

Given an email and password it creates a new connection which will be used for this class and all sub classes.

Raises Google::LoginError if login fails or if, god forbid, google is having issues.



20
21
22
# File 'lib/google/base.rb', line 20

def self.establish_connection(email, password)
  @@connection = new(email, password)
end

.get(url, o = {}) ⇒ Object

Makes a get request to a google service using the session id from the connection’s session

Usage:

get('http://google.com/some/thing')
get('http://google.com/some/thing', :query_hash => {:q => 'test', :second => 'another'})
  # makes request to http://google.com/some/thing?q=test&second=another
get('http://google.com/some/thing?ha=poo', :query_hash => {:q => 'test', :second => 'another'}, :qsi => '&')
  # makes request to http://google.com/some/thing?ha=poo&q=test&second=another


49
50
51
52
53
54
55
# File 'lib/google/base.rb', line 49

def self.get(url, o={})
  options = {
    :query_hash => nil,
    :qsi => '?'
  }.merge(o)
  request 'get', url, options
end

.logged_in?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/google/base.rb', line 117

def self.logged_in?
  defined?(@@connection) && @@connection.logged_in?
end

.post(url, o = {}) ⇒ Object

Makes a post request to a google service using the session id from the connection’s session

Usage:

post('http://google.com/some/thing', :form_data => {:one => '1', :two => '2'})
  # makes a post request to http://google.com/some/thing with the post data set to one=1&two=2
post('http://google.com/some/thing', :raw_data => "some=thing&another=thing")
  # makes a post request to http://google.com/some/thing with the post data set to some=thing&another=thing


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/google/base.rb', line 65

def self.post(url, o={})
  options = {
    :form_data => nil,
    :raw_data => nil,
  }.merge(o)
  if options[:raw_data]
    url    = URI.parse(URI.escape(url))
    http   = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true if url.port == 443
    result = http.request_post(url.request_uri, options[:raw_data], @@connection.headers)
    result.body
  else
    request 'post', url, options
  end
end

Instance Method Details

#headersObject

Outputs the headers that are needed to make an authenticated request



122
123
124
# File 'lib/google/base.rb', line 122

def headers
  {'Cookie' => "Name=#{@sid};SID=#{@sid};Domain=.google.com;Path=/;Expires=160000000000"}
end

#logged_in?Boolean

Returns true or false based on whether or not the session id is set

Returns:

  • (Boolean)


113
114
115
# File 'lib/google/base.rb', line 113

def logged_in?
  @sid ? true : false
end

#loginObject

Makes authentication request to google and sets the sid to be passed in a cookie with each authenticated request.

Raises Google::LoginError if login is unsuccessful

Raises:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/google/base.rb', line 95

def 
  url = URI.parse(LOGIN_URL)
  req = Net::HTTP::Post.new(url.request_uri)
  req.set_form_data({
    'accountType' => 'HOSTED_OR_GOOGLE',
    'Email'    => @email, 
    'Passwd'   => @password, 
    'source'   => SOURCE, 
    'continue' => URL,
  })
  http         = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  result       = http.start() { |conn| conn.request(req) }
  @sid         = extract_sid(result.body)
  raise LoginError, "Most likely your username and password are wrong." unless logged_in?
end