knock-knock 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,8 @@
1
+ == 0.1.3 2008-12-07
2
+
3
+ * New features
4
+ * Post method supports
5
+
1
6
  == 0.1.2 2008-12-07
2
7
 
3
8
  * Bug fix
@@ -5,6 +5,10 @@ module Bubble
5
5
  class BadLogin < Exception; end
6
6
  # For requests to Google services without a connection
7
7
  class UnstablishedConnection < Exception; end
8
+ # Bad request at Google returns 400
9
+ class BadRequest < Exception; end
10
+ # When a 409 (HTTP Conflict) occurs. It happens when, for example, you try to create a contact but it exists.
11
+ class HTTPConflict < Exception; end
8
12
 
9
13
  end
10
14
  end
@@ -5,7 +5,7 @@ module Bubble
5
5
 
6
6
  # Transforms the hash into a string which will be used to transmit data with the requests.
7
7
  def to_uri
8
- self.map { |k,v| "#{k}=#{v}"}.join('&')
8
+ self.sort_by { |k,v| k.to_s }.map { |k,v| "#{k}=#{v}"}.join('&')
9
9
  end
10
10
 
11
11
  end
@@ -11,7 +11,8 @@ module Bubble
11
11
  connection = Bubble::KnockKnock::Connection.instance
12
12
  @header = {'Cookie' => "Name=#{connection.auth};Auth=#{connection.auth};Domain=.google.com;Path=/;Expires=160000000000",
13
13
  'Content-length' => '0',
14
- 'Authorization' => "GoogleLogin auth=#{connection.auth}" }
14
+ 'Authorization' => "GoogleLogin auth=#{connection.auth}"
15
+ }
15
16
  end
16
17
 
17
18
  # Get the data from any Google Service.
@@ -21,18 +22,62 @@ module Bubble
21
22
  # You must to be connected. Take a look in Connection for more information.
22
23
  #
23
24
  # Request.get('http://www.google.com/m8/feeds/contacts/email%40gmail.com/full')
24
- def self.get(uri, query=nil)
25
- @request = self.new
26
-
27
- uri = URI.parse(uri)
28
-
29
- http = Net::HTTP.new(uri.host, 443)
30
- http.use_ssl = true
31
-
32
- response, body = http.get("#{uri.path}?#{query}", @request.header)
25
+ def self.get(uri, params=nil)
26
+ response, body = action(:get, uri, params)
33
27
 
34
28
  body
35
29
  end
30
+
31
+ # Post data to any Google Service.
32
+ # You just need to indicate the URI of the API and the attributes must that be sent in the request's body.
33
+ # A Status code 201 CREATED will be return in case everything goes as planned!
34
+ # === Example
35
+ # You must to be connected. Take a look at Connection for more information.
36
+ #
37
+ # Request.post('http://www.google.com/m8/feeds/contacts/email%40gmail.com/full', body)
38
+ def self.post(uri, params=nil)
39
+ response, body = action(:post, uri, params)
40
+
41
+ case response.code.to_i
42
+ when 400; raise BadRequest
43
+ when 409; raise HTTPConflict
44
+ when 201; body
45
+ else; response.code << ' => ' << body
46
+ end
47
+ end
48
+
49
+ protected
50
+
51
+ # Generates the basic setup for both GET and POST requests(later on PUT and DELETE also). Any request at Google's services
52
+ # must have SSL activated.
53
+ # POST requests at Google always use atom+xml as their content type default.
54
+ def self.setup(http_method, uri)
55
+
56
+ @request = self.new
57
+ @uri = URI.parse(uri)
58
+
59
+ case http_method
60
+ when :post
61
+ @request.header.merge!('Content-Type' => 'application/atom+xml')
62
+ when :get
63
+ @request.header.merge!('Content-Type' => 'application/xml')
64
+ end
65
+
66
+ @http = Net::HTTP.new(@uri.host, 443)
67
+ @http.use_ssl = true
68
+ end
69
+
70
+ # Responsible by makes the right request when the developer points the HTTP Method (Post, Get), adding the parameters and the right header.
71
+ def self.action(http_method, uri, params)
72
+ setup(http_method, uri)
73
+
74
+ case http_method
75
+ when :post
76
+ @http.post(@uri.path, params, @request.header)
77
+ when :get
78
+ @http.get("#{@uri.path}?#{params}", @request.header)
79
+ end
80
+ end
36
81
  end
37
82
  end
38
83
  end
@@ -9,7 +9,7 @@ module Bubble
9
9
 
10
10
  # For more information take a look at the README
11
11
  module KnockKnock
12
- VERSION = '0.1.2'
12
+ VERSION = '0.1.3'
13
13
  APP_NAME = 'KnockKnock Ruby Gem'
14
14
  end
15
15
  end
@@ -8,20 +8,7 @@ class TestKnockKnock < Test::Unit::TestCase # :nodoc:
8
8
  def test_stablish_connection
9
9
  assert_raise(Bubble::KnockKnock::BadLogin) { @kk.connect('test@gmail.com', 'password', 'xapi') }
10
10
 
11
- authenticate
12
- assert @kk.auth
13
- end
14
-
15
- def test_retrieving_data
16
- authenticate
17
-
18
- assert body = Bubble::KnockKnock::Request.get("http://www.google.com/m8/feeds/contacts/bubble.testing%40gmail.com/full")
19
- assert_match(/^<\?xml.*/, body)
20
- assert_equal(@kk.auth, Bubble::KnockKnock::Connection.instance.auth)
21
- end
22
-
23
- private
24
- def authenticate
25
11
  @kk.connect('bubble.testing@gmail.com', 'bubblerocks', 'cp')
12
+ assert @kk.auth
26
13
  end
27
- end
14
+ end
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class Bubble::KnockKnock::TestRequest < Test::Unit::TestCase # :nodoc: #
4
+ include Bubble::KnockKnock
5
+
6
+ def setup
7
+ @atom = File.open(File.dirname(__FILE__) + "/fixtures/atom_feed.atom").readlines.join
8
+ authenticate
9
+ end
10
+
11
+ def test_retrieving_data
12
+ assert content = Request.get("http://www.google.com/m8/feeds/contacts/bubble.testing%40gmail.com/full")
13
+ assert_match(/^<\?xml.*/, content)
14
+ end
15
+
16
+ def test_retrieving_post_feed
17
+ # Cause the contact is all right created, it can't be created again. Then a 409 occurs (HTTP Conflict).
18
+ # We need to implement the DELETE HTTP Method to delete this contact.
19
+
20
+ assert_raise(HTTPConflict) do
21
+ Request.post("http://www.google.com/m8/feeds/contacts/bubble.testing%40gmail.com/full", @atom)
22
+ end
23
+ end
24
+
25
+ def test_bad_request
26
+ bad_atom = @atom << "this_should_get a bad request"
27
+
28
+ assert_raise(BadRequest) do
29
+ Request.post("http://www.google.com/m8/feeds/contacts/bubble.testing%40gmail.com/full", bad_atom)
30
+ end
31
+ end
32
+
33
+ private
34
+ def authenticate
35
+ Connection.instance.connect('bubble.testing@gmail.com', 'bubblerocks', 'cp')
36
+ end
37
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knock-knock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno Azisaka Maciel
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  requirements: []
94
94
 
95
95
  rubyforge_project: knock-knock
96
- rubygems_version: 1.3.0
96
+ rubygems_version: 1.3.1
97
97
  signing_key:
98
98
  specification_version: 2
99
99
  summary: ""
@@ -101,3 +101,4 @@ test_files:
101
101
  - test/test_hash.rb
102
102
  - test/test_helper.rb
103
103
  - test/test_knock_knock.rb
104
+ - test/test_request.rb