0
@@ -9,105 +9,73 @@ module Google
0
class LoginError < Exception; end
0
URL = 'http://www.google.com'
0
LOGIN_URL = 'https://www.google.com:443/accounts/ClientLogin'
0
- SOURCE = 'G
Reader Ruby API'
0
+ SOURCE = 'G
oogle Auth Base Ruby Gem'
0
- # Given an email and password it creates a new connection
0
- # which will be used for this class and all sub classes.
0
- # Raises Google::LoginError if login fails or if, god forbid,
0
- # google is having issues.
0
- def establish_connection(email, password)
0
- @@connection = new(email, password)
0
- # Returns the current connection
0
- # Changes the current connection to the one provided.
0
- # If in an app you store the connection in a session,
0
- # you can reuse it instead of establishing a new connection
0
- # Google::Base.connection = session[:connection] # => or whatever
0
- def connection=(new_connection)
0
- @@connection = new_connection
0
- # Makes a get request to a google service using
0
- # the session id from the connection's session
0
- # get('http://google.com/some/thing')
0
- # get('http://google.com/some/thing', :query_hash => {:q => 'test', :second => 'another'})
0
- # # makes request to http://google.com/some/thing?q=test&second=another
0
- # get('http://google.com/some/thing?ha=poo', :query_hash => {:q => 'test', :second => 'another'}, :qsi => '&')
0
- # # makes request to http://google.com/some/thing?ha=poo&q=test&second=another
0
- request 'get', url, options
0
- # Makes a post request to a google service using
0
- # the session id from the connection's session
0
- # post('http://google.com/some/thing', :form_data => {:one => '1', :two => '2'})
0
- # # makes a post request to http://google.com/some/thing with the post data set to one=1&two=2
0
- # post('http://google.com/some/thing', :raw_data => "some=thing&another=thing")
0
- # # makes a post request to http://google.com/some/thing with the post data set to some=thing&another=thing
0
- url = URI.parse(URI.escape(url))
0
- http = Net::HTTP.new(url.host, url.port)
0
- result = http.request_post(url.request_uri, options[:raw_data], @@connection.headers)
0
- request 'post', url, options
0
+ # Given an email and password it creates a new connection
0
+ # which will be used for this class and all sub classes.
0
+ # Raises Google::LoginError if login fails or if, god forbid,
0
+ # google is having issues.
0
+ def self.establish_connection(email, password)
0
+ @@connection = new(email, password)
0
+ # Returns the current connection
0
+ # Changes the current connection to the one provided.
0
+ # If in an app you store the connection in a session,
0
+ # you can reuse it instead of establishing a new connection
0
+ # Google::Base.connection = session[:connection] # => or whatever
0
+ def self.connection=(new_connection)
0
+ @@connection = new_connection
0
+ # Makes a get request to a google service using
0
+ # the session id from the connection's session
0
+ # get('http://google.com/some/thing')
0
+ # get('http://google.com/some/thing', :query_hash => {:q => 'test', :second => 'another'})
0
+ # # makes request to http://google.com/some/thing?q=test&second=another
0
+ # get('http://google.com/some/thing?ha=poo', :query_hash => {:q => 'test', :second => 'another'}, :qsi => '&')
0
+ # # makes request to http://google.com/some/thing?ha=poo&q=test&second=another
0
+ def self.get(url, o={})
0
+ request 'get', url, options
0
+ # Makes a post request to a google service using
0
+ # the session id from the connection's session
0
+ # post('http://google.com/some/thing', :form_data => {:one => '1', :two => '2'})
0
+ # # makes a post request to http://google.com/some/thing with the post data set to one=1&two=2
0
+ # post('http://google.com/some/thing', :raw_data => "some=thing&another=thing")
0
+ # # makes a post request to http://google.com/some/thing with the post data set to some=thing&another=thing
0
+ def self.post(url, o={})
0
+ url = URI.parse(URI.escape(url))
0
+ http = Net::HTTP.new(url.host, url.port)
0
+ http.use_ssl = true if url.port == 443
0
+ result = http.request_post(url.request_uri, options[:raw_data], @@connection.headers)
0
+ request 'post', url, options
0
- def request(method, url, o={})
0
- url += hash_to_query_string(options[:query_hash], options[:qsi]) unless options[:query_hash].nil?
0
- url = URI.parse(URI.escape(url))
0
- req = if method == 'post'
0
- Net::HTTP::Post.new(url.request_uri, @@connection.headers)
0
- Net::HTTP::Get.new(url.request_uri, @@connection.headers)
0
- req.set_form_data(options[:form_data]) if options[:form_data]
0
- http = Net::HTTP.new(url.host, url.port)
0
- result = http.start() { |conn| conn.request(req) }
0
- # Converts a hash to a query string
0
- # hash_to_query_string({:q => 'test', :num => 5}) # => '?q=test&num=5&'
0
- # hash_to_query_string({:q => 'test', :num => 5}, '&') # => '&q=test&num=5&'
0
- def hash_to_query_string(hash, initial_value="?")
0
- hash.inject(initial_value) { |qs, h| qs += "#{h[0]}=#{h[1]}&"; qs }
0
# Session id returned from google login request
0
@@ -151,6 +119,37 @@ module Google
0
+ def self.request(method, url, o={})
0
+ url += hash_to_query_string(options[:query_hash], options[:qsi]) unless options[:query_hash].nil?
0
+ url = URI.parse(URI.escape(url))
0
+ req = if method == 'post'
0
+ Net::HTTP::Post.new(url.request_uri, @@connection.headers)
0
+ Net::HTTP::Get.new(url.request_uri, @@connection.headers)
0
+ req.set_form_data(options[:form_data]) if options[:form_data]
0
+ http = Net::HTTP.new(url.host, url.port)
0
+ http.use_ssl = true if url.port == 443
0
+ result = http.start() { |conn| conn.request(req) }
0
+ # Converts a hash to a query string
0
+ # hash_to_query_string({:q => 'test', :num => 5}) # => '?q=test&num=5&'
0
+ # hash_to_query_string({:q => 'test', :num => 5}, '&') # => '&q=test&num=5&'
0
+ def self.hash_to_query_string(hash, initial_value="?")
0
+ hash.inject(initial_value) { |qs, h| qs += "#{h[0]}=#{h[1]}&"; qs }
0
matches = body.match(/SID=(.*)/)
0
matches.nil? ? nil : matches[0].gsub('SID=', '')
Comments
No one has commented yet.