googlebase 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +1 -0
- data/README.txt +1 -0
- data/lib/google/base.rb +95 -96
- data/lib/google/version.rb +2 -2
- data/test/test_googlebase.rb +35 -11
- data/test/test_helper.rb +5 -1
- metadata +2 -2
data/History.txt
CHANGED
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
|
|
data/lib/google/base.rb
CHANGED
@@ -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 = '
|
12
|
+
SOURCE = 'Google Auth Base Ruby Gem'
|
13
13
|
|
14
14
|
class Base
|
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
|
-
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=', '')
|
data/lib/google/version.rb
CHANGED
data/test/test_googlebase.rb
CHANGED
@@ -1,11 +1,35 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
#
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
data/test/test_helper.rb
CHANGED
@@ -1,2 +1,6 @@
|
|
1
1
|
require 'test/unit'
|
2
|
-
require
|
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.
|
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:
|
12
|
+
date: 2008-02-14 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|