Class: Google::Base
- Inherits:
-
Object
- Object
- Google::Base
- Defined in:
- lib/google/base.rb
Instance Attribute Summary collapse
-
#sid ⇒ Object
Session id returned from google login request.
Class Method Summary collapse
-
.connection ⇒ Object
Returns the current connection.
-
.connection=(new_connection) ⇒ Object
Changes the current connection to the one provided.
-
.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.
-
.get(url, o = {}) ⇒ Object
Makes a get request to a google service using the session id from the connection’s session.
-
.post(url, o = {}) ⇒ Object
Makes a post request to a google service using the session id from the connection’s session.
Instance Method Summary collapse
-
#headers ⇒ Object
Outputs the headers that are needed to make an authenticated request.
-
#initialize(email, password) ⇒ Base
constructor
Creates a new instance of the connection class using the given email and password and attempts to login.
-
#logged_in? ⇒ Boolean
Returns true or false based on whether or not the session id is set.
-
#login ⇒ Object
Makes authentication request to google and sets the sid to be passed in a cookie with each authenticated request.
Constructor Details
#initialize(email, password) ⇒ Base
Creates a new instance of the connection class using the given email and password and attempts to login
118 119 120 121 |
# File 'lib/google/base.rb', line 118 def initialize(email, password) @email, @password = email, password login end |
Instance Attribute Details
#sid ⇒ Object
Session id returned from google login request
114 115 116 |
# File 'lib/google/base.rb', line 114 def sid @sid end |
Class Method Details
.connection ⇒ Object
Returns the current connection
26 27 28 |
# File 'lib/google/base.rb', line 26 def 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
37 38 39 |
# File 'lib/google/base.rb', line 37 def 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.
21 22 23 |
# File 'lib/google/base.rb', line 21 def 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
50 51 52 53 54 55 56 |
# File 'lib/google/base.rb', line 50 def get(url, o={}) = { :query_hash => nil, :qsi => '?' }.merge(o) request 'get', url, 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
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/google/base.rb', line 66 def post(url, o={}) = { :form_data => nil, :raw_data => nil, }.merge(o) if [:raw_data] url = URI.parse(URI.escape(url)) http = Net::HTTP.new(url.host, url.port) result = http.request_post(url.request_uri, [:raw_data], @@connection.headers) result.body else request 'post', url, end end |
Instance Method Details
#headers ⇒ Object
Outputs the headers that are needed to make an authenticated request
149 150 151 |
# File 'lib/google/base.rb', line 149 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
144 145 146 |
# File 'lib/google/base.rb', line 144 def logged_in? @sid ? true : false end |
#login ⇒ Object
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
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/google/base.rb', line 127 def login url = URI.parse(LOGIN_URL) req = Net::HTTP::Post.new(url.request_uri) req.set_form_data({ '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 |