knock-knock 0.1.1 → 0.1.2
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 +5 -0
- data/README.rdoc +2 -2
- data/lib/bubble/knock_knock/connection.rb +55 -50
- data/lib/bubble/knock_knock/exceptions.rb +10 -4
- data/lib/bubble/knock_knock/hash.rb +10 -6
- data/lib/bubble/knock_knock/request.rb +29 -25
- data/lib/knock_knock.rb +2 -2
- data/test/test_knock_knock.rb +4 -6
- metadata +2 -2
data/History.txt
CHANGED
data/README.rdoc
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
= KnockKnock
|
2
2
|
|
3
|
-
KnockKnock was made
|
3
|
+
KnockKnock was made to turn login with Google Authentication API easier.
|
4
4
|
|
5
5
|
== Features
|
6
6
|
* Singleton class for connection with Google.
|
@@ -25,7 +25,7 @@ KnockKnock was made for turn login with Google Authentication API more easy.
|
|
25
25
|
|
26
26
|
(The MIT License)
|
27
27
|
|
28
|
-
Copyright (c) 2008
|
28
|
+
Copyright (c) 2008 Bubble[http://bubble.com.br]
|
29
29
|
|
30
30
|
Permission is hereby granted, free of charge, to any person obtaining
|
31
31
|
a copy of this software and associated documentation files (the
|
@@ -3,66 +3,71 @@ require 'uri'
|
|
3
3
|
require 'net/http'
|
4
4
|
require 'net/https'
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
module Bubble
|
7
|
+
module KnockKnock
|
8
|
+
|
9
|
+
# This class make the connection between Ruby and Google.
|
10
|
+
# It's a Singleton class, turning the management of the connection easier. Just one connection will be created during all your script.
|
11
|
+
class Connection
|
12
|
+
include Singleton
|
10
13
|
|
11
|
-
|
12
|
-
|
14
|
+
attr_accessor :email, :service
|
15
|
+
attr_reader :password, :auth
|
13
16
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
17
|
+
# This method creates the connection. First of all it set up the environment to make the connection and after it stablish the connection with Google
|
18
|
+
# The service should be (i.e.):
|
19
|
+
# * [cp] Google Mail Contact Book
|
20
|
+
# * [analytics] Google Analytics
|
21
|
+
#
|
22
|
+
# === Example
|
23
|
+
# require 'rubygems'
|
24
|
+
# require 'knock_knock'
|
25
|
+
#
|
26
|
+
# Bubble::KnockKnock::Connection.connect('email@gmail.com', 'password', 'cp')
|
27
|
+
def connect(email, password, service)
|
28
|
+
@email = email
|
29
|
+
@password = password
|
30
|
+
@service = service
|
28
31
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
+
setup
|
33
|
+
stablish
|
34
|
+
end
|
32
35
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
36
|
+
# Retrieve the Auth Token required for authentications on all Google Services.
|
37
|
+
def auth
|
38
|
+
@auth
|
39
|
+
end
|
37
40
|
|
38
|
-
|
41
|
+
protected
|
39
42
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
+
# It gives the correct values to attributes and variables required to make the connection.
|
44
|
+
def setup
|
45
|
+
@uri = URI.parse('https://www.google.com/accounts/ClientLogin')
|
43
46
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
47
|
+
@query = { 'accountType' => 'GOOGLE',
|
48
|
+
'Email' => @email,
|
49
|
+
'Passwd' => @password,
|
50
|
+
'service' => @service,
|
51
|
+
'source' => Bubble::KnockKnock::APP_NAME }
|
49
52
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
+
@http = Net::HTTP.new(@uri.host, 443)
|
54
|
+
@http.use_ssl = true
|
55
|
+
end
|
53
56
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
+
# Makes the connection with Google. If the login and password are wrong a BadLogin exception occurs.
|
58
|
+
def stablish
|
59
|
+
response, body = @http.post(@uri.path, @query.to_uri)
|
57
60
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
61
|
+
case response.code.to_i
|
62
|
+
when 403; raise BadLogin
|
63
|
+
else; parse(body)
|
64
|
+
end
|
65
|
+
end
|
63
66
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
+
# Gives the Auth Token from authentication request.
|
68
|
+
def parse(body)
|
69
|
+
@auth = body.match(/Auth=(.*)/)[1]
|
70
|
+
end
|
71
|
+
end
|
67
72
|
end
|
68
73
|
end
|
@@ -1,4 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
module Bubble
|
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
|
+
|
9
|
+
end
|
10
|
+
end
|
@@ -1,11 +1,15 @@
|
|
1
|
-
|
2
|
-
module
|
1
|
+
module Bubble
|
2
|
+
module KnockKnock
|
3
|
+
# A collection of util methods for KnockKnock gem
|
4
|
+
module Bubble::KnockKnock::Hash
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
# Transforms the hash into a string which will be used to transmit data with the requests.
|
7
|
+
def to_uri
|
8
|
+
self.map { |k,v| "#{k}=#{v}"}.join('&')
|
9
|
+
end
|
8
10
|
|
11
|
+
end
|
12
|
+
end
|
9
13
|
end
|
10
14
|
|
11
15
|
class Hash
|
@@ -1,34 +1,38 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module Bubble
|
2
|
+
module KnockKnock
|
3
|
+
# This class gives you an easy way to request informations from Google.
|
4
|
+
class Request
|
5
|
+
attr_reader :header
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
-
|
7
|
+
# Finds the Singleton Connection and creates the header structure to requesting informations.
|
8
|
+
def initialize
|
9
|
+
raise Bubble::KnockKnock::UnstablishedConnection if Bubble::KnockKnock::Connection.instance.auth.nil?
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
connection = Bubble::KnockKnock::Connection.instance
|
12
|
+
@header = {'Cookie' => "Name=#{connection.auth};Auth=#{connection.auth};Domain=.google.com;Path=/;Expires=160000000000",
|
13
|
+
'Content-length' => '0',
|
14
|
+
'Authorization' => "GoogleLogin auth=#{connection.auth}" }
|
15
|
+
end
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
17
|
+
# Get the data from any Google Service.
|
18
|
+
# You just need to indicate the URI of the API and the attributes must that be sent with the request.
|
19
|
+
# The response's content will be returned if all occur as well.
|
20
|
+
# === Example
|
21
|
+
# You must to be connected. Take a look in Connection for more information.
|
22
|
+
#
|
23
|
+
# Request.get('http://www.google.com/m8/feeds/contacts/email%40gmail.com/full')
|
24
|
+
def self.get(uri, query=nil)
|
25
|
+
@request = self.new
|
24
26
|
|
25
|
-
|
27
|
+
uri = URI.parse(uri)
|
26
28
|
|
27
|
-
|
28
|
-
|
29
|
+
http = Net::HTTP.new(uri.host, 443)
|
30
|
+
http.use_ssl = true
|
29
31
|
|
30
|
-
|
32
|
+
response, body = http.get("#{uri.path}?#{query}", @request.header)
|
31
33
|
|
32
|
-
|
34
|
+
body
|
35
|
+
end
|
36
|
+
end
|
33
37
|
end
|
34
38
|
end
|
data/lib/knock_knock.rb
CHANGED
@@ -7,9 +7,9 @@ require 'active_support'
|
|
7
7
|
# A project developed by Bubble[http://bubble.com.br]
|
8
8
|
module Bubble
|
9
9
|
|
10
|
-
# For more information take a look
|
10
|
+
# For more information take a look at the README
|
11
11
|
module KnockKnock
|
12
|
-
VERSION = '0.1.
|
12
|
+
VERSION = '0.1.2'
|
13
13
|
APP_NAME = 'KnockKnock Ruby Gem'
|
14
14
|
end
|
15
15
|
end
|
data/test/test_knock_knock.rb
CHANGED
@@ -1,14 +1,12 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
2
|
|
3
3
|
class TestKnockKnock < Test::Unit::TestCase # :nodoc:
|
4
|
-
include Bubble::KnockKnock
|
5
|
-
|
6
4
|
def setup
|
7
|
-
@kk = Connection.instance
|
5
|
+
@kk = Bubble::KnockKnock::Connection.instance
|
8
6
|
end
|
9
7
|
|
10
8
|
def test_stablish_connection
|
11
|
-
assert_raise(BadLogin) { @kk.connect('test@gmail.com', 'password', 'xapi') }
|
9
|
+
assert_raise(Bubble::KnockKnock::BadLogin) { @kk.connect('test@gmail.com', 'password', 'xapi') }
|
12
10
|
|
13
11
|
authenticate
|
14
12
|
assert @kk.auth
|
@@ -17,9 +15,9 @@ class TestKnockKnock < Test::Unit::TestCase # :nodoc:
|
|
17
15
|
def test_retrieving_data
|
18
16
|
authenticate
|
19
17
|
|
20
|
-
assert body = Request.get("http://www.google.com/m8/feeds/contacts/bubble.testing%40gmail.com/full")
|
18
|
+
assert body = Bubble::KnockKnock::Request.get("http://www.google.com/m8/feeds/contacts/bubble.testing%40gmail.com/full")
|
21
19
|
assert_match(/^<\?xml.*/, body)
|
22
|
-
assert_equal(@kk.auth, Connection.instance.auth)
|
20
|
+
assert_equal(@kk.auth, Bubble::KnockKnock::Connection.instance.auth)
|
23
21
|
end
|
24
22
|
|
25
23
|
private
|
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.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bruno Azisaka Maciel
|
@@ -71,7 +71,7 @@ files:
|
|
71
71
|
- test/test_helper.rb
|
72
72
|
- test/test_knock_knock.rb
|
73
73
|
has_rdoc: true
|
74
|
-
homepage: KnockKnock was made
|
74
|
+
homepage: KnockKnock was made to turn login with Google Authentication API easier.
|
75
75
|
post_install_message:
|
76
76
|
rdoc_options:
|
77
77
|
- --main
|