knock-knock 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,12 +1,18 @@
1
+ == 0.1.4 2008-12-08
2
+
3
+ * New features
4
+ * Put method included
5
+ * Delete method included
6
+
1
7
  == 0.1.3 2008-12-07
2
8
 
3
9
  * New features
4
- * Post method supports
10
+ * Post method included
5
11
 
6
12
  == 0.1.2 2008-12-07
7
13
 
8
14
  * Bug fix
9
- * Fixing a problem with namespaces
15
+ * Fixing a problem with namespaces
10
16
 
11
17
  == 0.1.1 2008-12-07
12
18
 
data/Manifest.txt CHANGED
@@ -1,6 +1,5 @@
1
1
  History.txt
2
2
  Manifest.txt
3
- PostInstall.txt
4
3
  README.rdoc
5
4
  Rakefile
6
5
  lib/knock_knock.rb
data/README.rdoc CHANGED
@@ -5,6 +5,7 @@ KnockKnock was made to turn login with Google Authentication API easier.
5
5
  == Features
6
6
  * Singleton class for connection with Google.
7
7
  * Simple authentication and clear code.
8
+ * GET, POST, PUT, DELETE method implemented
8
9
 
9
10
  == Synopsis
10
11
 
data/Rakefile CHANGED
@@ -9,10 +9,11 @@ $hoe = Hoe.new('knock-knock', Bubble::KnockKnock::VERSION) do |p|
9
9
  # p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
10
10
  p.rubyforge_name = 'knock-knock' # TODO this is default value
11
11
  p.extra_deps = [
12
- ['activesupport','>= 2.0.2'],
12
+ ['activesupport','>= 2.0.2']
13
13
  ]
14
14
  p.extra_dev_deps = [
15
- ['newgem', ">= #{::Newgem::VERSION}"]
15
+ ['newgem', ">= #{::Newgem::VERSION}"],
16
+ ['xml-object', '>= 0.9.8']
16
17
  ]
17
18
 
18
19
  p.clean_globs |= %w[**/.DS_Store tmp *.log]
@@ -10,6 +10,8 @@ module Bubble
10
10
  # It's a Singleton class, turning the management of the connection easier. Just one connection will be created during all your script.
11
11
  class Connection
12
12
  include Singleton
13
+ include Bubble::KnockKnock
14
+ include Bubble::KnockKnock::Exceptions
13
15
 
14
16
  attr_accessor :email, :service
15
17
  attr_reader :password, :auth
@@ -48,7 +50,7 @@ module Bubble
48
50
  'Email' => @email,
49
51
  'Passwd' => @password,
50
52
  'service' => @service,
51
- 'source' => Bubble::KnockKnock::APP_NAME }
53
+ 'source' => APP_NAME }
52
54
 
53
55
  @http = Net::HTTP.new(@uri.host, 443)
54
56
  @http.use_ssl = true
@@ -1,14 +1,14 @@
1
1
  module Bubble
2
2
  module KnockKnock
3
-
4
- # For login errors
5
- class BadLogin < Exception; end
6
- # For requests to Google services without a connection
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
12
-
3
+ module Exceptions
4
+ # For login errors
5
+ class BadLogin < Exception; end
6
+ # For requests to Google services without a connection
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
12
+ end
13
13
  end
14
14
  end
@@ -2,13 +2,16 @@ module Bubble
2
2
  module KnockKnock
3
3
  # This class gives you an easy way to request informations from Google.
4
4
  class Request
5
+ include Bubble::KnockKnock
6
+ include Bubble::KnockKnock::Exceptions
7
+
5
8
  attr_reader :header
6
9
 
7
10
  # Finds the Singleton Connection and creates the header structure to requesting informations.
8
11
  def initialize
9
- raise Bubble::KnockKnock::UnstablishedConnection if Bubble::KnockKnock::Connection.instance.auth.nil?
12
+ raise UnstablishedConnection if Connection.instance.auth.nil?
10
13
 
11
- connection = Bubble::KnockKnock::Connection.instance
14
+ connection = Connection.instance
12
15
  @header = {'Cookie' => "Name=#{connection.auth};Auth=#{connection.auth};Domain=.google.com;Path=/;Expires=160000000000",
13
16
  'Content-length' => '0',
14
17
  'Authorization' => "GoogleLogin auth=#{connection.auth}"
@@ -23,8 +26,7 @@ module Bubble
23
26
  #
24
27
  # Request.get('http://www.google.com/m8/feeds/contacts/email%40gmail.com/full')
25
28
  def self.get(uri, params=nil)
26
- response, body = action(:get, uri, params)
27
-
29
+ response, body = action(:get, uri, params)
28
30
  body
29
31
  end
30
32
 
@@ -36,14 +38,32 @@ module Bubble
36
38
  #
37
39
  # Request.post('http://www.google.com/m8/feeds/contacts/email%40gmail.com/full', body)
38
40
  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
41
+ @response, @body = action(:post, uri, params)
42
+ handling
43
+ end
44
+
45
+ # Delete data at any Google Service.
46
+ # You just need to indicate the URI of the API and also get the entity's ID you're trying to delete.
47
+ # For more information on how to get the entity's ID,go to google code and check it out.
48
+ # === Example
49
+ # You must to be connected. Take a look at Connection for more information.
50
+ #
51
+ # Request.delete('http://www.google.com/m8/feeds/contacts/email%40gmail.com/full/6f669dbb0f3d75ce/1228698987605000')
52
+ def self.delete(uri,params=nil)
53
+ @response, body = action(:delete, uri, params)
54
+ handling
55
+ end
56
+
57
+ # Update data at any Google Service.
58
+ # You just need to indicate the URI of the API and also get the entity's ID you're trying to delete.
59
+ # For more information on how to get the entity's ID,go to google code and check it out.
60
+ # === Example
61
+ # You must to be connected. Take a look at Connection for more information.
62
+ #
63
+ # Request.put('http://www.google.com/m8/feeds/contacts/email%40gmail.com/full/6f669dbb0f3d75ce/1228698987605000')
64
+ def self.put(uri,params=nil)
65
+ @response, @body = action(:put, uri, params)
66
+ handling
47
67
  end
48
68
 
49
69
  protected
@@ -61,6 +81,10 @@ module Bubble
61
81
  @request.header.merge!('Content-Type' => 'application/atom+xml')
62
82
  when :get
63
83
  @request.header.merge!('Content-Type' => 'application/xml')
84
+ when :delete
85
+ @request.header.merge!('Content-Type' => 'application/atom+xml')
86
+ when :put
87
+ @request.header.merge!('X-HTTP-Method-Override' =>'PUT', 'Content-Type' => 'application/atom+xml')
64
88
  end
65
89
 
66
90
  @http = Net::HTTP.new(@uri.host, 443)
@@ -76,8 +100,24 @@ module Bubble
76
100
  @http.post(@uri.path, params, @request.header)
77
101
  when :get
78
102
  @http.get("#{@uri.path}?#{params}", @request.header)
103
+ when :delete
104
+ @http.delete(@uri.path, @request.header)
105
+ when :put
106
+ @http.post(@uri.path, params, @request.header)
79
107
  end
80
108
  end
109
+
110
+ # Returns the request's response, might be a error or XML
111
+ def self.handling
112
+
113
+ case @response.code.to_i
114
+ when 400; raise BadRequest
115
+ when 409; raise HTTPConflict
116
+ else; @body
117
+ end
118
+
119
+ end
120
+
81
121
  end
82
122
  end
83
123
  end
data/lib/knock_knock.rb CHANGED
@@ -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.3'
12
+ VERSION = '0.1.4'
13
13
  APP_NAME = 'KnockKnock Ruby Gem'
14
14
  end
15
15
  end
data/test/test_helper.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require 'stringio'
2
2
  require 'test/unit'
3
+ require File.dirname(__FILE__) + '/resources/contact'
3
4
  require File.dirname(__FILE__) + '/../lib/knock_knock'
@@ -1,12 +1,15 @@
1
1
  require File.dirname(__FILE__) + '/test_helper.rb'
2
2
 
3
3
  class TestKnockKnock < Test::Unit::TestCase # :nodoc:
4
+ include Bubble::KnockKnock
5
+ include Bubble::KnockKnock::Exceptions
6
+
4
7
  def setup
5
- @kk = Bubble::KnockKnock::Connection.instance
8
+ @kk = Connection.instance
6
9
  end
7
10
 
8
11
  def test_stablish_connection
9
- assert_raise(Bubble::KnockKnock::BadLogin) { @kk.connect('test@gmail.com', 'password', 'xapi') }
12
+ assert_raise(BadLogin) { @kk.connect('test@gmail.com', 'password', 'xapi') }
10
13
 
11
14
  @kk.connect('bubble.testing@gmail.com', 'bubblerocks', 'cp')
12
15
  assert @kk.auth
data/test/test_request.rb CHANGED
@@ -2,6 +2,7 @@ require File.dirname(__FILE__) + '/test_helper.rb'
2
2
 
3
3
  class Bubble::KnockKnock::TestRequest < Test::Unit::TestCase # :nodoc: #
4
4
  include Bubble::KnockKnock
5
+ include Bubble::KnockKnock::Exceptions
5
6
 
6
7
  def setup
7
8
  @atom = File.open(File.dirname(__FILE__) + "/fixtures/atom_feed.atom").readlines.join
@@ -10,18 +11,30 @@ class Bubble::KnockKnock::TestRequest < Test::Unit::TestCase # :nodoc: #
10
11
 
11
12
  def test_retrieving_data
12
13
  assert content = Request.get("http://www.google.com/m8/feeds/contacts/bubble.testing%40gmail.com/full")
13
- assert_match(/^<\?xml.*/, content)
14
+ assert_match(/^<\?xml.*$/, content)
14
15
  end
15
16
 
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.
17
+ def test_deleting_contact
18
+ create_contact
19
19
 
20
- assert_raise(HTTPConflict) do
21
- Request.post("http://www.google.com/m8/feeds/contacts/bubble.testing%40gmail.com/full", @atom)
22
- end
20
+ assert content = Request.delete(@contact.delete_uri)
21
+ assert_match(/^<\?xml.*$/, content)
23
22
  end
24
23
 
24
+ def test_updating_contact
25
+ create_contact
26
+ @contact.change_title
27
+
28
+ content = Request.put(@contact.edit_uri,@contact.content)
29
+ assert_match(/^<\?xml.*$/, content)
30
+
31
+ # Everytime you update something at Google, your edit url changes, which means
32
+ # you have to instanciate a new contact in order to work
33
+ @contact = Contact.new(content)
34
+ assert content = Request.delete(@contact.delete_uri)
35
+ assert_match(/^<\?xml.*$/, content)
36
+ end
37
+
25
38
  def test_bad_request
26
39
  bad_atom = @atom << "this_should_get a bad request"
27
40
 
@@ -34,4 +47,9 @@ class Bubble::KnockKnock::TestRequest < Test::Unit::TestCase # :nodoc: #
34
47
  def authenticate
35
48
  Connection.instance.connect('bubble.testing@gmail.com', 'bubblerocks', 'cp')
36
49
  end
50
+
51
+ def create_contact
52
+ @content = Request.post("http://www.google.com/m8/feeds/contacts/bubble.testing%40gmail.com/full", @atom)
53
+ @contact = Contact.new(@content)
54
+ end
37
55
  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.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno Azisaka Maciel
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-12-07 00:00:00 -02:00
12
+ date: 2008-12-08 00:00:00 -02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -32,6 +32,16 @@ dependencies:
32
32
  - !ruby/object:Gem::Version
33
33
  version: 1.1.0
34
34
  version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: xml-object
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.9.8
44
+ version:
35
45
  - !ruby/object:Gem::Dependency
36
46
  name: hoe
37
47
  type: :development
@@ -52,12 +62,10 @@ extensions: []
52
62
  extra_rdoc_files:
53
63
  - History.txt
54
64
  - Manifest.txt
55
- - PostInstall.txt
56
65
  - README.rdoc
57
66
  files:
58
67
  - History.txt
59
68
  - Manifest.txt
60
- - PostInstall.txt
61
69
  - README.rdoc
62
70
  - Rakefile
63
71
  - lib/knock_knock.rb
data/PostInstall.txt DELETED
File without changes