googlebase 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,4 @@
1
+ * 0.2.0 - added ssl support for requests
1
2
  * 0.1.3 - addded URI.escape around the built urls that are being requested
2
3
  * 0.1.2 - added raw_data option to post method
3
4
  * 0.1.1 - added post request method
data/README.txt CHANGED
@@ -13,6 +13,7 @@ The code below shows how to use the gem by itself. It checks if username and pas
13
13
  require 'google/base'
14
14
  Google::Base.establish_connection('username', 'password')
15
15
  Google::Base.get('http://google.com/reader/path/to/whatever/')
16
+ Google::Base.get('https://google.com:443/analytics/home/') # to make an ssl request
16
17
 
17
18
  ===Inheritance
18
19
 
@@ -9,105 +9,73 @@ module Google
9
9
  class LoginError < Exception; end
10
10
  URL = 'http://www.google.com'
11
11
  LOGIN_URL = 'https://www.google.com:443/accounts/ClientLogin'
12
- SOURCE = 'GReader Ruby API'
12
+ SOURCE = 'Google Auth Base Ruby Gem'
13
13
 
14
14
  class Base
15
- class << self
16
- # Given an email and password it creates a new connection
17
- # which will be used for this class and all sub classes.
18
- #
19
- # Raises Google::LoginError if login fails or if, god forbid,
20
- # google is having issues.
21
- def establish_connection(email, password)
22
- @@connection = new(email, password)
23
- end
24
-
25
- # Returns the current connection
26
- def connection
27
- @@connection
28
- end
29
-
30
- # Changes the current connection to the one provided.
31
- # If in an app you store the connection in a session,
32
- # you can reuse it instead of establishing a new connection
33
- # with each request.
34
- #
35
- # Usage:
36
- # Google::Base.connection = session[:connection] # => or whatever
37
- def connection=(new_connection)
38
- @@connection = new_connection
39
- end
40
-
41
- # Makes a get request to a google service using
42
- # the session id from the connection's session
43
- #
44
- # Usage:
45
- # get('http://google.com/some/thing')
46
- # get('http://google.com/some/thing', :query_hash => {:q => 'test', :second => 'another'})
47
- # # makes request to http://google.com/some/thing?q=test&second=another
48
- # get('http://google.com/some/thing?ha=poo', :query_hash => {:q => 'test', :second => 'another'}, :qsi => '&')
49
- # # makes request to http://google.com/some/thing?ha=poo&q=test&second=another
50
- def get(url, o={})
51
- options = {
52
- :query_hash => nil,
53
- :qsi => '?'
54
- }.merge(o)
55
- request 'get', url, options
56
- end
57
-
58
- # Makes a post request to a google service using
59
- # the session id from the connection's session
60
- #
61
- # Usage:
62
- # post('http://google.com/some/thing', :form_data => {:one => '1', :two => '2'})
63
- # # makes a post request to http://google.com/some/thing with the post data set to one=1&two=2
64
- # post('http://google.com/some/thing', :raw_data => "some=thing&another=thing")
65
- # # makes a post request to http://google.com/some/thing with the post data set to some=thing&another=thing
66
- def post(url, o={})
67
- options = {
68
- :form_data => nil,
69
- :raw_data => nil,
70
- }.merge(o)
71
- if options[:raw_data]
72
- url = URI.parse(URI.escape(url))
73
- http = Net::HTTP.new(url.host, url.port)
74
- result = http.request_post(url.request_uri, options[:raw_data], @@connection.headers)
75
- result.body
76
- else
77
- request 'post', url, options
78
- end
15
+ # Given an email and password it creates a new connection
16
+ # which will be used for this class and all sub classes.
17
+ #
18
+ # Raises Google::LoginError if login fails or if, god forbid,
19
+ # google is having issues.
20
+ def self.establish_connection(email, password)
21
+ @@connection = new(email, password)
22
+ end
23
+
24
+ # Returns the current connection
25
+ def self.connection
26
+ @@connection
27
+ end
28
+
29
+ # Changes the current connection to the one provided.
30
+ # If in an app you store the connection in a session,
31
+ # you can reuse it instead of establishing a new connection
32
+ # with each request.
33
+ #
34
+ # Usage:
35
+ # Google::Base.connection = session[:connection] # => or whatever
36
+ def self.connection=(new_connection)
37
+ @@connection = new_connection
38
+ end
39
+
40
+ # Makes a get request to a google service using
41
+ # the session id from the connection's session
42
+ #
43
+ # Usage:
44
+ # get('http://google.com/some/thing')
45
+ # get('http://google.com/some/thing', :query_hash => {:q => 'test', :second => 'another'})
46
+ # # makes request to http://google.com/some/thing?q=test&second=another
47
+ # get('http://google.com/some/thing?ha=poo', :query_hash => {:q => 'test', :second => 'another'}, :qsi => '&')
48
+ # # makes request to http://google.com/some/thing?ha=poo&q=test&second=another
49
+ def self.get(url, o={})
50
+ options = {
51
+ :query_hash => nil,
52
+ :qsi => '?'
53
+ }.merge(o)
54
+ request 'get', url, options
55
+ end
56
+
57
+ # Makes a post request to a google service using
58
+ # the session id from the connection's session
59
+ #
60
+ # Usage:
61
+ # post('http://google.com/some/thing', :form_data => {:one => '1', :two => '2'})
62
+ # # makes a post request to http://google.com/some/thing with the post data set to one=1&two=2
63
+ # post('http://google.com/some/thing', :raw_data => "some=thing&another=thing")
64
+ # # makes a post request to http://google.com/some/thing with the post data set to some=thing&another=thing
65
+ def self.post(url, o={})
66
+ options = {
67
+ :form_data => nil,
68
+ :raw_data => nil,
69
+ }.merge(o)
70
+ if options[:raw_data]
71
+ url = URI.parse(URI.escape(url))
72
+ http = Net::HTTP.new(url.host, url.port)
73
+ http.use_ssl = true if url.port == 443
74
+ result = http.request_post(url.request_uri, options[:raw_data], @@connection.headers)
75
+ result.body
76
+ else
77
+ request 'post', url, options
79
78
  end
80
-
81
- private
82
- def request(method, url, o={})
83
- options = {
84
- :form_data => nil,
85
- :query_hash => nil,
86
- :qsi => '?'
87
- }.merge(o)
88
-
89
- url += hash_to_query_string(options[:query_hash], options[:qsi]) unless options[:query_hash].nil?
90
- url = URI.parse(URI.escape(url))
91
- req = if method == 'post'
92
- Net::HTTP::Post.new(url.request_uri, @@connection.headers)
93
- else
94
- Net::HTTP::Get.new(url.request_uri, @@connection.headers)
95
- end
96
- req.set_form_data(options[:form_data]) if options[:form_data]
97
-
98
- http = Net::HTTP.new(url.host, url.port)
99
- result = http.start() { |conn| conn.request(req) }
100
- result.body
101
- end
102
-
103
- # Converts a hash to a query string
104
- #
105
- # Usage:
106
- # hash_to_query_string({:q => 'test', :num => 5}) # => '?q=test&num=5&'
107
- # hash_to_query_string({:q => 'test', :num => 5}, '&') # => '&q=test&num=5&'
108
- def hash_to_query_string(hash, initial_value="?")
109
- hash.inject(initial_value) { |qs, h| qs += "#{h[0]}=#{h[1]}&"; qs }
110
- end
111
79
  end
112
80
 
113
81
  # Session id returned from google login request
@@ -151,6 +119,37 @@ module Google
151
119
  end
152
120
 
153
121
  private
122
+ def self.request(method, url, o={})
123
+ options = {
124
+ :form_data => nil,
125
+ :query_hash => nil,
126
+ :qsi => '?'
127
+ }.merge(o)
128
+
129
+ url += hash_to_query_string(options[:query_hash], options[:qsi]) unless options[:query_hash].nil?
130
+ url = URI.parse(URI.escape(url))
131
+ req = if method == 'post'
132
+ Net::HTTP::Post.new(url.request_uri, @@connection.headers)
133
+ else
134
+ Net::HTTP::Get.new(url.request_uri, @@connection.headers)
135
+ end
136
+ req.set_form_data(options[:form_data]) if options[:form_data]
137
+
138
+ http = Net::HTTP.new(url.host, url.port)
139
+ http.use_ssl = true if url.port == 443
140
+ result = http.start() { |conn| conn.request(req) }
141
+ result.body
142
+ end
143
+
144
+ # Converts a hash to a query string
145
+ #
146
+ # Usage:
147
+ # hash_to_query_string({:q => 'test', :num => 5}) # => '?q=test&num=5&'
148
+ # hash_to_query_string({:q => 'test', :num => 5}, '&') # => '&q=test&num=5&'
149
+ def self.hash_to_query_string(hash, initial_value="?")
150
+ hash.inject(initial_value) { |qs, h| qs += "#{h[0]}=#{h[1]}&"; qs }
151
+ end
152
+
154
153
  def extract_sid(body)
155
154
  matches = body.match(/SID=(.*)/)
156
155
  matches.nil? ? nil : matches[0].gsub('SID=', '')
@@ -1,8 +1,8 @@
1
1
  module Google #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
- MINOR = 1
5
- TINY = 3
4
+ MINOR = 2
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -1,11 +1,35 @@
1
- # require File.dirname(__FILE__) + '/test_helper.rb'
2
- #
3
- # class TestGooglebase < Test::Unit::TestCase
4
- #
5
- # def setup
6
- # end
7
- #
8
- # def test_truth
9
- # assert true
10
- # end
11
- # end
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ # 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.
4
+
5
+ class TestGooglebase < Test::Unit::TestCase
6
+
7
+ def setup
8
+ end
9
+
10
+ def test_should_make_http_get_request
11
+ html = Google::Base.get('http://www.google.com/reader/user-info')
12
+ assert html =~ /webgroup@nd\.edu/
13
+ end
14
+
15
+ def test_should_make_https_get_request
16
+ html = Google::Base.get('https://www.google.com:443/analytics/home/')
17
+ assert html =~ /webgroup\.nd\.edu/
18
+ end
19
+
20
+ def test_should_make_http_post_request
21
+
22
+ end
23
+
24
+ def test_should_make_https_post_request
25
+
26
+ end
27
+
28
+ def test_should_make_http_post_request_with_raw_data
29
+
30
+ end
31
+
32
+ def test_should_make_https_post_request_with_raw_data
33
+
34
+ end
35
+ end
@@ -1,2 +1,6 @@
1
1
  require 'test/unit'
2
- require File.dirname(__FILE__) + '/../lib/googlebase'
2
+ require 'yaml'
3
+ require File.dirname(__FILE__) + '/../lib/google/base'
4
+
5
+ config = YAML::load(open(File.join(ENV['HOME'], '.statwhore')))
6
+ Google::Base.establish_connection(config[:username], config[:password])
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: googlebase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ""
6
6
  authors:
7
7
  - John
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2007-12-26 00:00:00 -05:00
12
+ date: 2008-02-14 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15