googlebase 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt CHANGED
@@ -1 +1,4 @@
1
+ * 0.1.1 - added post request method
2
+ - refactored get to take a hash which will automatically be converted to a query string
3
+ - added in a bit more documentation for methods including the new post method
1
4
  * 0.1.0 - initial release
data/lib/google/base.rb CHANGED
@@ -14,7 +14,10 @@ module Google
14
14
  class Base
15
15
  class << self
16
16
  # Given an email and password it creates a new connection
17
- # which will be used for this class and all sub classes
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.
18
21
  def establish_connection(email, password)
19
22
  @@connection = new(email, password)
20
23
  end
@@ -24,27 +27,77 @@ module Google
24
27
  @@connection
25
28
  end
26
29
 
27
- # Changes the current connection to the one provided
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
28
37
  def connection=(new_connection)
29
38
  @@connection = new_connection
30
39
  end
31
40
 
32
41
  # Makes a get request to a google service using
33
42
  # the session id from the connection's session
34
- def get(url)
35
- request 'get', url
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 form data set to one=1&two=2
64
+ def post(url, o={})
65
+ options = {
66
+ :form_data => nil,
67
+ }.merge(o)
68
+ request 'post', url, options
36
69
  end
37
70
 
38
71
  private
39
- # This will eventually implement get and post
40
- # but I haven't needed post yet
41
- def request(method, url)
42
- url = URI.parse(url)
43
- req = Net::HTTP::Get.new(url.request_uri, @@connection.headers)
72
+ def request(method, url, o={})
73
+ options = {
74
+ :form_data => nil,
75
+ :query_hash => nil,
76
+ :qsi => '?'
77
+ }.merge(o)
78
+
79
+ url += hash_to_query_string(options[:query_hash], options[:qsi]) unless options[:query_hash].nil?
80
+ url = URI.parse(url)
81
+ req = if method == 'post'
82
+ Net::HTTP::Post.new(url.request_uri, @@connection.headers)
83
+ else
84
+ Net::HTTP::Get.new(url.request_uri, @@connection.headers)
85
+ end
86
+ req.set_form_data(options[:form_data]) if options[:form_data]
87
+
44
88
  http = Net::HTTP.new(url.host, url.port)
45
89
  result = http.start() { |conn| conn.request(req) }
46
90
  result.body
47
91
  end
92
+
93
+ # Converts a hash to a query string
94
+ #
95
+ # Usage:
96
+ # hash_to_query_string({:q => 'test', :num => 5}) # => '?q=test&num=5&'
97
+ # hash_to_query_string({:q => 'test', :num => 5}, '&') # => '&q=test&num=5&'
98
+ def hash_to_query_string(hash, initial_value="?")
99
+ hash.inject(initial_value) { |qs, h| qs += "#{h[0]}=#{h[1]}&"; qs }
100
+ end
48
101
  end
49
102
 
50
103
  # Session id returned from google login request
@@ -2,7 +2,7 @@ module Google #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 0
5
+ TINY = 1
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
metadata CHANGED
@@ -1,33 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.4
3
- specification_version: 1
4
2
  name: googlebase
5
3
  version: !ruby/object:Gem::Version
6
- version: 0.1.0
7
- date: 2007-11-19 00:00:00 -05:00
8
- summary: Base class which handles authentication and requests for google services
9
- require_paths:
10
- - lib
11
- email: nunemaker@gmail.com
12
- homepage: http://googlebase.rubyforge.org
13
- rubyforge_project: googlebase
14
- description: Base class which handles authentication and requests for google services
15
- autorequire:
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: true
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
25
- platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
4
+ version: 0.1.1
5
+ platform: ""
29
6
  authors:
30
7
  - John
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2007-11-25 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Base class which handles authentication and requests for google services
17
+ email: nunemaker@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - History.txt
24
+ - License.txt
25
+ - Manifest.txt
26
+ - README.txt
31
27
  files:
32
28
  - History.txt
33
29
  - License.txt
@@ -49,22 +45,33 @@ files:
49
45
  - test/test_helper.rb
50
46
  - website/css/common.css
51
47
  - website/index.html
52
- test_files:
53
- - test/test_googlebase.rb
54
- - test/test_helper.rb
48
+ has_rdoc: true
49
+ homepage: http://googlebase.rubyforge.org
50
+ post_install_message:
55
51
  rdoc_options:
56
52
  - --main
57
53
  - README.txt
58
- extra_rdoc_files:
59
- - History.txt
60
- - License.txt
61
- - Manifest.txt
62
- - README.txt
63
- executables: []
64
-
65
- extensions: []
66
-
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
67
68
  requirements: []
68
69
 
69
- dependencies: []
70
-
70
+ rubyforge_project: googlebase
71
+ rubygems_version: 0.9.5
72
+ signing_key:
73
+ specification_version: 2
74
+ summary: Base class which handles authentication and requests for google services
75
+ test_files:
76
+ - test/test_googlebase.rb
77
+ - test/test_helper.rb