knock-knock 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ == 0.1 2008-12-06
2
+
3
+ * Singleton class for connection with Google
4
+ * Request class for retrieve data from Google
data/Manifest.txt ADDED
@@ -0,0 +1,15 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/knock_knock.rb
7
+ lib/bubble/knock_knock/connection.rb
8
+ lib/bubble/knock_knock/exceptions.rb
9
+ lib/bubble/knock_knock/hash.rb
10
+ lib/bubble/knock_knock/request.rb
11
+ script/console
12
+ script/destroy
13
+ script/generate
14
+ test/test_helper.rb
15
+ test/test_knock_knock.rb
data/PostInstall.txt ADDED
File without changes
data/README.rdoc ADDED
@@ -0,0 +1,47 @@
1
+ = KnockKnock
2
+
3
+ KnockKnock was made for turn login with Google Authentication API more easy.
4
+
5
+ == Features
6
+ * Singleton class for connection with Google.
7
+ * Simple authentication and clear code.
8
+
9
+ == Synopsis
10
+
11
+ require 'rubygems'
12
+ require 'knock_knock'
13
+
14
+ include Bubble::KnockKnock
15
+
16
+ Connection.connect('email@gmail.com', 'password', 'cp')
17
+
18
+ print Request.get('http://www.google.com/m8/feeds/contacts/email%40gmail.com/full')
19
+
20
+ == Install
21
+
22
+ sudo gem install knock-knock
23
+
24
+ == License
25
+
26
+ (The MIT License)
27
+
28
+ Copyright (c) 2008 Bruno Azisaka Maciel
29
+
30
+ Permission is hereby granted, free of charge, to any person obtaining
31
+ a copy of this software and associated documentation files (the
32
+ 'Software'), to deal in the Software without restriction, including
33
+ without limitation the rights to use, copy, modify, merge, publish,
34
+ distribute, sublicense, and/or sell copies of the Software, and to
35
+ permit persons to whom the Software is furnished to do so, subject to
36
+ the following conditions:
37
+
38
+ The above copyright notice and this permission notice shall be
39
+ included in all copies or substantial portions of the Software.
40
+
41
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
42
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
43
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
44
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
45
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
46
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
47
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/knock_knock'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('knock-knock', Bubble::KnockKnock::VERSION) do |p|
7
+ p.developer('Bruno Azisaka Maciel', 'bruno@dookie.com.br')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ # p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
10
+ p.rubyforge_name = 'dookie' # TODO this is default value
11
+ p.extra_deps = [
12
+ ['activesupport','>= 2.0.2'],
13
+ ]
14
+ p.extra_dev_deps = [
15
+ ['newgem', ">= #{::Newgem::VERSION}"]
16
+ ]
17
+
18
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
19
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
20
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
21
+ p.rsync_args = '-av --delete --ignore-errors'
22
+ end
23
+
24
+ require 'newgem/tasks' # load /tasks/*.rake
25
+ Dir['tasks/**/*.rake'].each { |t| load t }
26
+
27
+ # TODO - want other tests/tasks run by default? Add them to the list
28
+ # task :default => [:spec, :features]
@@ -0,0 +1,68 @@
1
+ require 'singleton'
2
+ require 'uri'
3
+ require 'net/http'
4
+ require 'net/https'
5
+
6
+ # This class make the connection between Ruby and Google.
7
+ # It's a Singleton class, turning the management of the connection more easy. Just one connection will be created during all your script.
8
+ class Bubble::KnockKnock::Connection
9
+ include Singleton
10
+
11
+ attr_accessor :email, :service
12
+ attr_reader :password, :auth
13
+
14
+ # This method create the connection. First of all it set up the environment for make the connection and after it stablish the connection with Google
15
+ # The service should be (i.e.):
16
+ # * [cp] Google Mail Contact Book
17
+ # * [analytics] Google Analytics
18
+ #
19
+ # === Example
20
+ # require 'rubygems'
21
+ # require 'knock_knock'
22
+ #
23
+ # Bubble::KnockKnock::Connection.connect('email@gmail.com', 'password', 'cp')
24
+ def connect(email, password, service)
25
+ @email = email
26
+ @password = password
27
+ @service = service
28
+
29
+ setup
30
+ stablish
31
+ end
32
+
33
+ # Retrieve the Auth Token required for authentications on all Google Services.
34
+ def auth
35
+ @auth
36
+ end
37
+
38
+ protected
39
+
40
+ # It gives the correct values to attributes and variables required for make the connection.
41
+ def setup
42
+ @uri = URI.parse('https://www.google.com/accounts/ClientLogin')
43
+
44
+ @query = { 'accountType' => 'GOOGLE',
45
+ 'Email' => @email,
46
+ 'Passwd' => @password,
47
+ 'service' => @service,
48
+ 'source' => APP_NAME }
49
+
50
+ @http = Net::HTTP.new(@uri.host, 443)
51
+ @http.use_ssl = true
52
+ end
53
+
54
+ # Makes the connection with Google. If the login and password are wrong an BadLogin exception occours.
55
+ def stablish
56
+ response, body = @http.post(@uri.path, @query.to_uri)
57
+
58
+ case response.code.to_i
59
+ when 403; raise BadLogin
60
+ else; parse(body)
61
+ end
62
+ end
63
+
64
+ # Gives the Auth Token from authentication request.
65
+ def parse(body)
66
+ @auth = body.match(/Auth=(.*)/)[1]
67
+ end
68
+ end
@@ -0,0 +1,4 @@
1
+ # For login errors
2
+ class Bubble::KnockKnock::BadLogin < Exception; end
3
+ # For requests to Google services without a connection
4
+ class Bubble::KnockKnock::UnstablishedConnection < Exception; end
@@ -0,0 +1,13 @@
1
+ # A collection of util methods for KnockKnock gem
2
+ module Bubble::KnockKnock::Hash
3
+
4
+ # Transforms the hash into to a string which will be used to transmit data with the requests.
5
+ def to_uri
6
+ self.map { |k,v| "#{k}=#{v}"}.join('&')
7
+ end
8
+
9
+ end
10
+
11
+ class Hash
12
+ include Bubble::KnockKnock::Hash
13
+ end
@@ -0,0 +1,34 @@
1
+ # This class gives you a easy way to request informations from Google.
2
+ class Bubble::KnockKnock::Request
3
+ attr_reader :header
4
+
5
+ # Finds the Singleton Connection and creates the header structure to requesting informations.
6
+ def initialize
7
+ raise UnstablishedConnection if Connection.instance.auth.nil?
8
+
9
+ connection = Connection.instance
10
+ @header = {'Cookie' => "Name=#{connection.auth};Auth=#{connection.auth};Domain=.google.com;Path=/;Expires=160000000000",
11
+ 'Content-length' => '0',
12
+ 'Authorization' => "GoogleLogin auth=#{connection.auth}" }
13
+ end
14
+
15
+ # Get the data from any Google Service.
16
+ # You just need to indicate the URI of the API and the attributes must be sent with the request.
17
+ # The content of the response will be returned if all occour as well.
18
+ # === Example
19
+ # You must to be connected. Take a look in Connection for more information.
20
+ #
21
+ # Request.get('http://www.google.com/m8/feeds/contacts/email%40gmail.com/full')
22
+ def self.get(uri, query=nil)
23
+ @request = self.new
24
+
25
+ uri = URI.parse(uri)
26
+
27
+ http = Net::HTTP.new(uri.host, 443)
28
+ http.use_ssl = true
29
+
30
+ response, body = http.get("#{uri.path}?#{query}", @request.header)
31
+
32
+ body
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'rubygems'
5
+ require 'active_support'
6
+
7
+ # A project developed by Bubble[http://bubble.com.br]
8
+ module Bubble
9
+
10
+ # For more information take a look on README
11
+ module KnockKnock
12
+ VERSION = '0.1'
13
+ APP_NAME = 'KnockKnock Ruby Gem'
14
+ end
15
+ end
16
+
17
+ require File.dirname(__FILE__) + '/bubble/knock_knock/exceptions'
18
+ require File.dirname(__FILE__) + '/bubble/knock_knock/hash'
19
+ require File.dirname(__FILE__) + '/bubble/knock_knock/connection'
20
+ require File.dirname(__FILE__) + '/bubble/knock_knock/request'
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/knock_knock.rb'}"
9
+ puts "Loading knock_knock gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
data/test/test_hash.rb ADDED
@@ -0,0 +1,12 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class Bubble::KnockKnock::TestHash < Test::Unit::TestCase # :nodoc:
4
+ def setup
5
+ @data = { :group=>'hour',
6
+ :order=>'days_since_last_visit,bounce_rate' }
7
+ end
8
+
9
+ def test_to_uri
10
+ assert_equal('group=hour&order=days_since_last_visit,bounce_rate', @data.to_uri)
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/knock_knock'
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestKnockKnock < Test::Unit::TestCase # :nodoc:
4
+ include Bubble::KnockKnock
5
+
6
+ def setup
7
+ @kk = Connection.instance
8
+ end
9
+
10
+ def test_stablish_connection
11
+ assert_raise(BadLogin) { @kk.connect('test@gmail.com', 'password', 'xapi') }
12
+
13
+ authenticate
14
+ assert @kk.auth
15
+ end
16
+
17
+ def test_retrieving_data
18
+ authenticate
19
+
20
+ assert body = Request.get("http://www.google.com/m8/feeds/contacts/bubble.testing%40gmail.com/full")
21
+ assert_match(/^<\?xml.*/, body)
22
+ assert_equal(@kk.auth, Connection.instance.auth)
23
+ end
24
+
25
+ private
26
+ def authenticate
27
+ @kk.connect('bubble.testing@gmail.com', 'bubblerocks', 'cp')
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knock-knock
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Bruno Azisaka Maciel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-07 00:00:00 -02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.0.2
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: newgem
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.0
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: hoe
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.8.0
44
+ version:
45
+ description: ""
46
+ email:
47
+ - bruno@dookie.com.br
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files:
53
+ - History.txt
54
+ - Manifest.txt
55
+ - PostInstall.txt
56
+ - README.rdoc
57
+ files:
58
+ - History.txt
59
+ - Manifest.txt
60
+ - PostInstall.txt
61
+ - README.rdoc
62
+ - Rakefile
63
+ - lib/knock_knock.rb
64
+ - lib/bubble/knock_knock/connection.rb
65
+ - lib/bubble/knock_knock/exceptions.rb
66
+ - lib/bubble/knock_knock/hash.rb
67
+ - lib/bubble/knock_knock/request.rb
68
+ - script/console
69
+ - script/destroy
70
+ - script/generate
71
+ - test/test_helper.rb
72
+ - test/test_knock_knock.rb
73
+ has_rdoc: true
74
+ homepage: KnockKnock was made for turn login with Google Authentication API more easy.
75
+ post_install_message:
76
+ rdoc_options:
77
+ - --main
78
+ - README.rdoc
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "0"
86
+ version:
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: "0"
92
+ version:
93
+ requirements: []
94
+
95
+ rubyforge_project: dookie
96
+ rubygems_version: 1.3.0
97
+ signing_key:
98
+ specification_version: 2
99
+ summary: ""
100
+ test_files:
101
+ - test/test_hash.rb
102
+ - test/test_helper.rb
103
+ - test/test_knock_knock.rb