Skip to content
Browse files

added ssl support for get and post methods

  • Loading branch information...
1 parent a283e7a commit 5be561cd10c11f6f0fe1aebc222466c72077dbf4 @jnunemaker committed Feb 14, 2008
Showing with 138 additions and 110 deletions.
  1. +1 −0 History.txt
  2. +95 −96 lib/google/base.rb
  3. +2 −2 lib/google/version.rb
  4. +35 −11 test/test_googlebase.rb
  5. +5 −1 test/test_helper.rb
View
1 History.txt
@@ -1,3 +1,4 @@
+* 0.2.0 - added ssl support for requests
* 0.1.3 - addded URI.escape around the built urls that are being requested
* 0.1.2 - added raw_data option to post method
* 0.1.1 - added post request method
View
191 lib/google/base.rb
@@ -9,105 +9,73 @@ module Google
class LoginError < Exception; end
URL = 'http://www.google.com'
LOGIN_URL = 'https://www.google.com:443/accounts/ClientLogin'
- SOURCE = 'GReader Ruby API'
+ SOURCE = 'Google Auth Base Ruby Gem'
class Base
- class << self
- # 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.
- def establish_connection(email, password)
- @@connection = new(email, password)
- end
-
- # Returns the current connection
- def connection
- @@connection
- end
-
- # 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
- def connection=(new_connection)
- @@connection = new_connection
- end
-
- # 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
- def get(url, o={})
- options = {
- :query_hash => nil,
- :qsi => '?'
- }.merge(o)
- request 'get', url, options
- end
-
- # 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
- def 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)
- result = http.request_post(url.request_uri, options[:raw_data], @@connection.headers)
- result.body
- else
- request 'post', url, options
- end
+ # 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.
+ def self.establish_connection(email, password)
+ @@connection = new(email, password)
+ end
+
+ # Returns the current connection
+ def self.connection
+ @@connection
+ end
+
+ # 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
+ def self.connection=(new_connection)
+ @@connection = new_connection
+ end
+
+ # 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
+ def self.get(url, o={})
+ options = {
+ :query_hash => nil,
+ :qsi => '?'
+ }.merge(o)
+ request 'get', url, options
+ end
+
+ # 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
+ 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
-
- private
- def request(method, url, o={})
- options = {
- :form_data => nil,
- :query_hash => nil,
- :qsi => '?'
- }.merge(o)
-
- url += hash_to_query_string(options[:query_hash], options[:qsi]) unless options[:query_hash].nil?
- url = URI.parse(URI.escape(url))
- req = if method == 'post'
- Net::HTTP::Post.new(url.request_uri, @@connection.headers)
- else
- Net::HTTP::Get.new(url.request_uri, @@connection.headers)
- end
- req.set_form_data(options[:form_data]) if options[:form_data]
-
- http = Net::HTTP.new(url.host, url.port)
- result = http.start() { |conn| conn.request(req) }
- result.body
- end
-
- # Converts a hash to a query string
- #
- # Usage:
- # hash_to_query_string({:q => 'test', :num => 5}) # => '?q=test&num=5&'
- # hash_to_query_string({:q => 'test', :num => 5}, '&') # => '&q=test&num=5&'
- def hash_to_query_string(hash, initial_value="?")
- hash.inject(initial_value) { |qs, h| qs += "#{h[0]}=#{h[1]}&"; qs }
- end
end
# Session id returned from google login request
@@ -151,6 +119,37 @@ def headers
end
private
+ def self.request(method, url, o={})
+ options = {
+ :form_data => nil,
+ :query_hash => nil,
+ :qsi => '?'
+ }.merge(o)
+
+ url += hash_to_query_string(options[:query_hash], options[:qsi]) unless options[:query_hash].nil?
+ url = URI.parse(URI.escape(url))
+ req = if method == 'post'
+ Net::HTTP::Post.new(url.request_uri, @@connection.headers)
+ else
+ Net::HTTP::Get.new(url.request_uri, @@connection.headers)
+ end
+ req.set_form_data(options[:form_data]) if options[:form_data]
+
+ http = Net::HTTP.new(url.host, url.port)
+ http.use_ssl = true if url.port == 443
+ result = http.start() { |conn| conn.request(req) }
+ result.body
+ end
+
+ # Converts a hash to a query string
+ #
+ # Usage:
+ # hash_to_query_string({:q => 'test', :num => 5}) # => '?q=test&num=5&'
+ # hash_to_query_string({:q => 'test', :num => 5}, '&') # => '&q=test&num=5&'
+ def self.hash_to_query_string(hash, initial_value="?")
+ hash.inject(initial_value) { |qs, h| qs += "#{h[0]}=#{h[1]}&"; qs }
+ end
+
def extract_sid(body)
matches = body.match(/SID=(.*)/)
matches.nil? ? nil : matches[0].gsub('SID=', '')
View
4 lib/google/version.rb
@@ -1,8 +1,8 @@
module Google #:nodoc:
module VERSION #:nodoc:
MAJOR = 0
- MINOR = 1
- TINY = 3
+ MINOR = 2
+ TINY = 0
STRING = [MAJOR, MINOR, TINY].join('.')
end
View
46 test/test_googlebase.rb
@@ -1,11 +1,35 @@
-# require File.dirname(__FILE__) + '/test_helper.rb'
-#
-# class TestGooglebase < Test::Unit::TestCase
-#
-# def setup
-# end
-#
-# def test_truth
-# assert true
-# end
-# end
+require File.dirname(__FILE__) + '/test_helper.rb'
+
+# I am fully aware these are lacking. Haven't had the time to figure out best way to test this since it is in essence a hack.
+
+class TestGooglebase < Test::Unit::TestCase
+
+ def setup
+ end
+
+ def test_should_make_http_get_request
+ html = Google::Base.get('http://www.google.com/reader/user-info')
+ assert html =~ /webgroup@nd\.edu/
+ end
+
+ def test_should_make_https_get_request
+ html = Google::Base.get('https://www.google.com:443/analytics/home/')
+ assert html =~ /webgroup\.nd\.edu/
+ end
+
+ def test_should_make_http_post_request
+
+ end
+
+ def test_should_make_https_post_request
+
+ end
+
+ def test_should_make_http_post_request_with_raw_data
+
+ end
+
+ def test_should_make_https_post_request_with_raw_data
+
+ end
+end
View
6 test/test_helper.rb
@@ -1,2 +1,6 @@
require 'test/unit'
-require File.dirname(__FILE__) + '/../lib/googlebase'
+require 'yaml'
+require File.dirname(__FILE__) + '/../lib/google/base'
+
+config = YAML::load(open(File.join(ENV['HOME'], '.statwhore')))
+Google::Base.establish_connection(config[:username], config[:password])

0 comments on commit 5be561c

Please sign in to comment.
Something went wrong with that request. Please try again.