public
Description: Google Base Auth Class is a base for authenticating to google and making requests to google services.
Homepage: http://googlebase.rubyforge.org/
Clone URL: git://github.com/jnunemaker/googlebase.git
added ssl support for get and post methods
jnunemaker (author)
Wed Feb 13 22:58:28 -0800 2008
commit  5be561cd10c11f6f0fe1aebc222466c72077dbf4
tree    2216ec04ef5283f1162de4390a3b6aab5a3b8d02
parent  a283e7af91014baa709c48c492afdc77406461c8
...
 
1
2
3
...
1
2
3
4
0
@@ -1,3 +1,4 @@
0
+* 0.2.0 - added ssl support for requests
0
 * 0.1.3 - addded URI.escape around the built urls that are being requested
0
 * 0.1.2 - added raw_data option to post method
0
 * 0.1.1 - added post request method
...
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
...
151
152
153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
155
156
...
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
80
81
...
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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 = 'GReader Ruby API'
0
+ SOURCE = 'Google Auth Base Ruby Gem'
0
   
0
   class Base
0
- class << self
0
- # Given an email and password it creates a new connection
0
- # which will be used for this class and all sub classes.
0
- #
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
- end
0
-
0
- # Returns the current connection
0
- def connection
0
- @@connection
0
- end
0
-
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
- # with each request.
0
- #
0
- # Usage:
0
- # Google::Base.connection = session[:connection] # => or whatever
0
- def connection=(new_connection)
0
- @@connection = new_connection
0
- end
0
-
0
- # Makes a get request to a google service using
0
- # the session id from the connection's session
0
- #
0
- # Usage:
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 get(url, o={})
0
- options = {
0
- :query_hash => nil,
0
- :qsi => '?'
0
- }.merge(o)
0
- request 'get', url, options
0
- end
0
-
0
- # Makes a post request to a google service using
0
- # the session id from the connection's session
0
- #
0
- # Usage:
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 post(url, o={})
0
- options = {
0
- :form_data => nil,
0
- :raw_data => nil,
0
- }.merge(o)
0
- if options[:raw_data]
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
- result.body
0
- else
0
- request 'post', url, options
0
- end
0
+ # Given an email and password it creates a new connection
0
+ # which will be used for this class and all sub classes.
0
+ #
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
+ end
0
+
0
+ # Returns the current connection
0
+ def self.connection
0
+ @@connection
0
+ end
0
+
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
+ # with each request.
0
+ #
0
+ # Usage:
0
+ # Google::Base.connection = session[:connection] # => or whatever
0
+ def self.connection=(new_connection)
0
+ @@connection = new_connection
0
+ end
0
+
0
+ # Makes a get request to a google service using
0
+ # the session id from the connection's session
0
+ #
0
+ # Usage:
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
+ options = {
0
+ :query_hash => nil,
0
+ :qsi => '?'
0
+ }.merge(o)
0
+ request 'get', url, options
0
+ end
0
+
0
+ # Makes a post request to a google service using
0
+ # the session id from the connection's session
0
+ #
0
+ # Usage:
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
+ options = {
0
+ :form_data => nil,
0
+ :raw_data => nil,
0
+ }.merge(o)
0
+ if options[:raw_data]
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
+ result.body
0
+ else
0
+ request 'post', url, options
0
       end
0
-
0
- private
0
- def request(method, url, o={})
0
- options = {
0
- :form_data => nil,
0
- :query_hash => nil,
0
- :qsi => '?'
0
- }.merge(o)
0
-
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
- else
0
- Net::HTTP::Get.new(url.request_uri, @@connection.headers)
0
- end
0
- req.set_form_data(options[:form_data]) if options[:form_data]
0
-
0
- http = Net::HTTP.new(url.host, url.port)
0
- result = http.start() { |conn| conn.request(req) }
0
- result.body
0
- end
0
-
0
- # Converts a hash to a query string
0
- #
0
- # Usage:
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
- end
0
     end
0
     
0
     # Session id returned from google login request
0
@@ -151,6 +119,37 @@ module Google
0
     end
0
     
0
     private
0
+ def self.request(method, url, o={})
0
+ options = {
0
+ :form_data => nil,
0
+ :query_hash => nil,
0
+ :qsi => '?'
0
+ }.merge(o)
0
+
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
+ else
0
+ Net::HTTP::Get.new(url.request_uri, @@connection.headers)
0
+ end
0
+ req.set_form_data(options[:form_data]) if options[:form_data]
0
+
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
+ result.body
0
+ end
0
+
0
+ # Converts a hash to a query string
0
+ #
0
+ # Usage:
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
+ end
0
+
0
       def extract_sid(body)
0
         matches = body.match(/SID=(.*)/)
0
         matches.nil? ? nil : matches[0].gsub('SID=', '')
...
1
2
3
4
5
 
 
6
7
8
...
1
2
3
 
 
4
5
6
7
8
0
@@ -1,8 +1,8 @@
0
 module Google #:nodoc:
0
   module VERSION #:nodoc:
0
     MAJOR = 0
0
- MINOR = 1
0
- TINY = 3
0
+ MINOR = 2
0
+ TINY = 0
0
 
0
     STRING = [MAJOR, MINOR, TINY].join('.')
0
   end
...
1
2
3
4
5
6
7
8
9
10
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
...
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
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
0
@@ -1,11 +1,35 @@
0
-# require File.dirname(__FILE__) + '/test_helper.rb'
0
-#
0
-# class TestGooglebase < Test::Unit::TestCase
0
-#
0
-# def setup
0
-# end
0
-#
0
-# def test_truth
0
-# assert true
0
-# end
0
-# end
0
+require File.dirname(__FILE__) + '/test_helper.rb'
0
+
0
+# 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.
0
+
0
+class TestGooglebase < Test::Unit::TestCase
0
+
0
+ def setup
0
+ end
0
+
0
+ def test_should_make_http_get_request
0
+ html = Google::Base.get('http://www.google.com/reader/user-info')
0
+ assert html =~ /webgroup@nd\.edu/
0
+ end
0
+
0
+ def test_should_make_https_get_request
0
+ html = Google::Base.get('https://www.google.com:443/analytics/home/')
0
+ assert html =~ /webgroup\.nd\.edu/
0
+ end
0
+
0
+ def test_should_make_http_post_request
0
+
0
+ end
0
+
0
+ def test_should_make_https_post_request
0
+
0
+ end
0
+
0
+ def test_should_make_http_post_request_with_raw_data
0
+
0
+ end
0
+
0
+ def test_should_make_https_post_request_with_raw_data
0
+
0
+ end
0
+end
0
\ No newline at end of file
...
1
2
 
 
 
 
 
3
...
1
 
2
3
4
5
6
7
0
@@ -1,2 +1,6 @@
0
 require 'test/unit'
0
-require File.dirname(__FILE__) + '/../lib/googlebase'
0
+require 'yaml'
0
+require File.dirname(__FILE__) + '/../lib/google/base'
0
+
0
+config = YAML::load(open(File.join(ENV['HOME'], '.statwhore')))
0
+Google::Base.establish_connection(config[:username], config[:password])
0
\ No newline at end of file

Comments

    No one has commented yet.