gmail_contatos 0.0.1 → 0.0.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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in gmail_contatos.gemspec
4
+ gemspec
data/Rakefile CHANGED
@@ -1,6 +1,2 @@
1
- require "rake/testtask"
2
-
3
- Rake::TestTask.new do |t|
4
- t.libs << "lib"
5
- t.test_files = Dir["test/**/*_test.rb"]
6
- end
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module GmailContatos
3
+ class ContactsController < GmailContatos::ApplicationController
4
+ def new
5
+ redirect_to Google::Authorization.build_auth_url("http://#{request.env["HTTP_HOST"]}/admin/gauthorize")
6
+ end
7
+
8
+ def authorize
9
+ token = Google::Authorization.exchange_singular_use_for_session_token(params[:token])
10
+
11
+ unless token == false
12
+ redirect_to "http://#{request.env["HTTP_HOST"]}/admin/lista?token=#{token}"
13
+ else
14
+ flash[:error] = "Sua tentativa de autenticação com o google falhou."
15
+ redirect_to root_url
16
+ end
17
+ end
18
+
19
+ def index
20
+ #render :text=>"aaa"
21
+ @contacts = GmailContatos::Contact.all(params[:token])
22
+ raise @contacts.inspect
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ module GmailContatos
2
+ module ApplicationHelper
3
+ def link_google
4
+ "meu link do google"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,38 @@
1
+ require 'net/http'
2
+ require 'rexml/document'
3
+ module Google
4
+ class GmailContatos::Contact
5
+ attr_accessor :name
6
+ attr_accessor :email
7
+ def initialize(name, email)
8
+ @name = name
9
+ @email = email
10
+ end
11
+
12
+ def self.all(token)
13
+ http = Net::HTTP.new('www.google.com', 80)
14
+ # by default Google returns 50? contacts at a time. Set max-results to very high number
15
+ # in order to retrieve more contacts
16
+ path = "/m8/feeds/contacts/default/base?max-results=10000"
17
+ headers = {'Authorization' => "AuthSub token=#{token}"}
18
+ resp = http.get2(path, headers)
19
+ data = resp.body
20
+ # extract the name and email address from the response data
21
+ xml = REXML::Document.new(data)
22
+ contacts = []
23
+ xml.elements.each('//entry') do |entry|
24
+ name = entry.elements['title'].text
25
+
26
+ gd_email = entry.elements['gd:email']
27
+ email = gd_email.attributes['address'] if gd_email
28
+ person = self.new(name, email)
29
+ contacts << person
30
+ end
31
+ return contacts
32
+ end
33
+ def persisted?
34
+ return false
35
+ end
36
+ end
37
+ end
38
+
File without changes
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ GmailContatos::Engine.routes.draw do
2
+ get "lista" => "contacts#index"
3
+ get "gauthorize" =>"contacts#authorize"
4
+ root :to=>"contacts#new"
5
+ end
@@ -0,0 +1,5 @@
1
+ module GmailContatos
2
+ class Engine < Rails::Engine
3
+ isolate_namespace GmailContatos
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module GmailContatos
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,8 @@
1
+ require "active_support/dependencies"
2
+ require "gmail_contatos/engine"
3
+ require "google/authorization"
4
+
5
+ module GmailContatos
6
+
7
+
8
+ end
@@ -0,0 +1,46 @@
1
+ module Google
2
+ class Authorization
3
+ def self.build_auth_url(return_url)
4
+ # The URL of the page that Google should redirect the user to after authentication.
5
+ return_url = return_url
6
+ # Indicates that the application is requesting a token to access contacts feeds.
7
+ scope_param = "http://www.google.com/m8/feeds/"
8
+ # Indicates whether the client is requesting a secure token.
9
+ secure_param = 0
10
+ # Indicates whether the token returned can be exchanged for a multi-use (session) token.
11
+ session_param = 1
12
+ # root url
13
+ root_url = "https://www.google.com/accounts/AuthSubRequest"
14
+
15
+ root_url + "?scope=#{scope_param}&session=#{session_param}&secure=#{secure_param}&next=#{return_url}"
16
+ end
17
+
18
+ def self.exchange_singular_use_for_session_token(token)
19
+ require 'net/http'
20
+ require 'net/https'
21
+ puts "exchange_singular_use_for_session_token: #{token}"
22
+ http = Net::HTTP.new('www.google.com', 443)
23
+ http.use_ssl = true
24
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
25
+ path = '/accounts/AuthSubSessionToken'
26
+ headers = {'Authorization' => "AuthSub token=\"#{token}\"", 'GData-Version' => "3.0"}
27
+ #headers = {'Authorization' => "AuthSub token=\"#{token}\""}
28
+ r1,r2,r3 = http.get2(path, headers)
29
+ puts "r1 b: #{r1.body}"
30
+ puts "r1 h: #{r1.header}"
31
+ puts "r3 to_h: #{r1.to_hash}"
32
+ #path = "/m8/feeds/contacts/default/full?max-results=10000"
33
+ if r1.code == "200"
34
+ token = ''
35
+ r1.body.split.each do |str|
36
+ if not (str =~ /Token=/).nil?
37
+ token = str.gsub(/Token=/, '')
38
+ end
39
+ end
40
+ return token
41
+ else
42
+ return false
43
+ end
44
+ end
45
+ end
46
+ end
data/lib/google.rb ADDED
@@ -0,0 +1,36 @@
1
+ module Google
2
+ class Contact < ActiveRecord::Base
3
+ attr_accessor :name
4
+ attr_accessor :email
5
+ def initialize(name, email)
6
+ @name = name
7
+ @email = email
8
+ end
9
+
10
+ def self.all(token)
11
+ # GET http://www.google.com/m8/feeds/contacts/default/base
12
+ require 'net/http'
13
+ require 'rexml/document'
14
+
15
+ http = Net::HTTP.new('www.google.com', 80)
16
+ # by default Google returns 50? contacts at a time. Set max-results to very high number
17
+ # in order to retrieve more contacts
18
+ path = "/m8/feeds/contacts/default/base?max-results=10000"
19
+ headers = {'Authorization' => "AuthSub token=#{token}"}
20
+ resp, data = http.get(path, headers)
21
+
22
+ # extract the name and email address from the response data
23
+ xml = REXML::Document.new(data)
24
+ contacts = []
25
+ xml.elements.each('//entry') do |entry|
26
+ name = entry.elements['title'].text
27
+
28
+ gd_email = entry.elements['gd:email']
29
+ email = gd_email.attributes['address'] if gd_email
30
+ person = self.new(name, email)
31
+ contacts << person
32
+ end
33
+ return contacts
34
+ end
35
+ end
36
+ end
metadata CHANGED
@@ -1,69 +1,57 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: gmail_contatos
3
- version: !ruby/object:Gem::Version
4
- hash: 29
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 1
10
- version: 0.0.1
11
6
  platform: ruby
12
- authors:
13
- - Janderson Ferreira
7
+ authors:
8
+ - Janderson F. Ferreira
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-03-06 00:00:00 -03:00
19
- default_executable:
12
+ date: 2012-03-13 00:00:00.000000000 Z
20
13
  dependencies: []
21
-
22
- description: A simple gem that says hello to the world!
23
- email:
14
+ description: ! ' Simples gem para sincronizar com Gmail Contatos '
15
+ email:
16
+ - ffjanderson@gmail.com
24
17
  executables: []
25
-
26
18
  extensions: []
27
-
28
19
  extra_rdoc_files: []
29
-
30
- files:
31
- - test/gmail_contatos_test.rb
20
+ files:
21
+ - Gemfile
32
22
  - Rakefile
33
- - gmail_contatos.gemspec
34
- has_rdoc: true
35
- homepage:
23
+ - app/controllers/gmail_contatos/contacts_controller.rb
24
+ - app/models/gmail_contatos/contact.rb
25
+ - app/views/gmail_contatos/index.html.erb
26
+ - app/helpers/application_helper.rb
27
+ - config/routes.rb
28
+ - lib/gmail_contatos.rb
29
+ - lib/google.rb
30
+ - lib/google/authorization.rb
31
+ - lib/gmail_contatos/engine.rb
32
+ - lib/gmail_contatos/version.rb
33
+ homepage: https://github.com/Janderson/gmail_contatos
36
34
  licenses: []
37
-
38
35
  post_install_message:
39
36
  rdoc_options: []
40
-
41
- require_paths:
37
+ require_paths:
42
38
  - lib
43
- required_ruby_version: !ruby/object:Gem::Requirement
39
+ required_ruby_version: !ruby/object:Gem::Requirement
44
40
  none: false
45
- requirements:
46
- - - ">="
47
- - !ruby/object:Gem::Version
48
- hash: 3
49
- segments:
50
- - 0
51
- version: "0"
52
- required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
46
  none: false
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- hash: 3
58
- segments:
59
- - 0
60
- version: "0"
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
61
51
  requirements: []
62
-
63
- rubyforge_project:
64
- rubygems_version: 1.5.2
52
+ rubyforge_project: gmail_contatos
53
+ rubygems_version: 1.8.15
65
54
  signing_key:
66
55
  specification_version: 3
67
- summary: Say hello!
56
+ summary: Simples gem para sincronizar com Gmail Contatos
68
57
  test_files: []
69
-
@@ -1,8 +0,0 @@
1
- Gem::Specification.new do |s|
2
- s.name = "gmail_contatos"
3
- s.version = "0.0.1"
4
- s.description = "A simple gem that says hello to the world!"
5
- s.summary = "Say hello!"
6
- s.author = "Janderson Ferreira"
7
- s.files = Dir["{lib/**/*.rb,README.rdoc,test/**/*.rb,Rakefile,*.gemspec}"]
8
- end
@@ -1,7 +0,0 @@
1
- require "test/unit"
2
- require "gmail_contatos" # Added
3
- class GmailContatosTest < Test::Unit::TestCase
4
- def test_say_hello_to_the_world
5
- assert_equal "Hello World!", GmailContatos.say
6
- end
7
- end