twitter_connect 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +57 -0
- data/README_rails3.md +57 -0
- data/Rakefile +36 -0
- data/VERSION +1 -0
- data/init.rb +4 -0
- data/install.rb +1 -0
- data/lib/app/controllers/twitter_connects_controller.rb +32 -0
- data/lib/app/helpers/twitter_connects_helper.rb +10 -0
- data/lib/app/views/twitter_connects/callback.html.erb +15 -0
- data/lib/public/javascripts/twitter_connect.js +60 -0
- data/lib/twitter_connect.rb +42 -0
- data/rails/init.rb +1 -0
- data/tasks/twitter_connect_tasks.rake +4 -0
- data/test/test_helper.rb +5 -0
- data/test/twitter_connect_test.rb +8 -0
- data/uninstall.rb +1 -0
- metadata +91 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 [Richard Huang]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# TwitterConnect
|
2
|
+
|
3
|
+
facebook connect style twitter oauth
|
4
|
+
|
5
|
+
## Demo
|
6
|
+
|
7
|
+
[http://twitter-connect.heroku.com](http://twitter-connect.heroku.com)
|
8
|
+
|
9
|
+
## Install
|
10
|
+
|
11
|
+
<pre><code>script/plugin install git://github.com/flyerhzm/twitter_connect.git</code></pre>
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
1 define <code>config/twitter.yml</code>
|
16
|
+
<pre><code>
|
17
|
+
development:
|
18
|
+
consumer_token:
|
19
|
+
consumer_secret:
|
20
|
+
logout_url:
|
21
|
+
production:
|
22
|
+
consumer_token:
|
23
|
+
consumer_secret:
|
24
|
+
logout_url:
|
25
|
+
</code></pre>
|
26
|
+
consumer_token and consumer_secret are fetched after you registered a twitter app.
|
27
|
+
logout_url is where the app go to when user logs out or the session expired.
|
28
|
+
|
29
|
+
2 define routes
|
30
|
+
<pre><code>
|
31
|
+
map.twitter_oauth '/twitter_oauth', :controller => 'twitter_connects', :action => 'oauth'
|
32
|
+
map.twitter_callback '/twitter_callback', :controller => 'twitter_connects', :action => 'callback'
|
33
|
+
map.twitter_logout '/twitter_logout', :controller => 'twitter_connects', :action => 'logout'
|
34
|
+
</code></pre>
|
35
|
+
|
36
|
+
3 add javascript to html header
|
37
|
+
<pre><code>
|
38
|
+
<%= javascript_include_tag 'twitter_connect' %>
|
39
|
+
</code></pre>
|
40
|
+
|
41
|
+
4 add twitter login link on view page
|
42
|
+
<pre><code>
|
43
|
+
<%= tc_login_button "window.location.reload()" %>
|
44
|
+
</code></pre>
|
45
|
+
Or:
|
46
|
+
<pre><code>
|
47
|
+
<%= tc_login_button "window.location.href = '#{tweets_path}'", :class_name => 'twitter_button' %>
|
48
|
+
</code></pre>
|
49
|
+
<code>tc_login_button</code> has two parameters, first is the callback javascript, second is customized parameter :text or :class_name
|
50
|
+
|
51
|
+
5 add twitter logout link on view page
|
52
|
+
<pre><code>
|
53
|
+
<%= link_to "Logout from Twitter", twitter_logout_path %>
|
54
|
+
</code></pre>
|
55
|
+
|
56
|
+
|
57
|
+
Copyright (c) 2010 [Richard Huang], released under the MIT license
|
data/README_rails3.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# TwitterConnect
|
2
|
+
|
3
|
+
facebook connect style twitter oauth
|
4
|
+
|
5
|
+
## Demo
|
6
|
+
|
7
|
+
[http://twitter-connect.heroku.com](http://twitter-connect.heroku.com)
|
8
|
+
|
9
|
+
## Install
|
10
|
+
|
11
|
+
<pre><code>rails plugin install git://github.com/flyerhzm/twitter_connect.git</code></pre>
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
1 define <code>config/twitter.yml</code>
|
16
|
+
<pre><code>
|
17
|
+
development:
|
18
|
+
consumer_token:
|
19
|
+
consumer_secret:
|
20
|
+
logout_url:
|
21
|
+
production:
|
22
|
+
consumer_token:
|
23
|
+
consumer_secret:
|
24
|
+
logout_url:
|
25
|
+
</code></pre>
|
26
|
+
consumer_token and consumer_secret are fetched after you registered a twitter app.
|
27
|
+
logout_url is where the app go to when user logs out or the session expired.
|
28
|
+
|
29
|
+
2 define routes
|
30
|
+
<pre><code>
|
31
|
+
match '/twitter_oauth', :to => "twitter_connects#oauth", :as => :twitter_oauth
|
32
|
+
match '/twitter_callback', :to => "twitter_connects#callback", :as => :twitter_callback
|
33
|
+
match '/twitter_logout', :to => "twitter_connects#logout", :as => :twitter_logout
|
34
|
+
</code></pre>
|
35
|
+
|
36
|
+
3 add javascript to html header
|
37
|
+
<pre><code>
|
38
|
+
<%= javascript_include_tag 'twitter_connect' %>
|
39
|
+
</code></pre>
|
40
|
+
|
41
|
+
4 add twitter login link on view page
|
42
|
+
<pre><code>
|
43
|
+
<%= tc_login_button "window.location.reload()" %>
|
44
|
+
</code></pre>
|
45
|
+
Or:
|
46
|
+
<pre><code>
|
47
|
+
<%= tc_login_button "window.location.href = '#{tweets_path}'", :class_name => 'twitter_button' %>
|
48
|
+
</code></pre>
|
49
|
+
<code>tc_login_button</code> has two parameters, first is the callback javascript, second is customized parameter :text or :class_name
|
50
|
+
|
51
|
+
5 add twitter logout link on view page
|
52
|
+
<pre><code>
|
53
|
+
<%= link_to "Logout from Twitter", twitter_logout_path %>
|
54
|
+
</code></pre>
|
55
|
+
|
56
|
+
|
57
|
+
Copyright (c) 2010 [Richard Huang], released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'jeweler'
|
5
|
+
|
6
|
+
desc 'Default: run unit tests.'
|
7
|
+
task :default => :test
|
8
|
+
|
9
|
+
desc 'Test the twitter_connect plugin.'
|
10
|
+
Rake::TestTask.new(:test) do |t|
|
11
|
+
t.libs << 'lib'
|
12
|
+
t.libs << 'test'
|
13
|
+
t.pattern = 'test/**/*_test.rb'
|
14
|
+
t.verbose = true
|
15
|
+
end
|
16
|
+
|
17
|
+
desc 'Generate documentation for the twitter_connect plugin.'
|
18
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
19
|
+
rdoc.rdoc_dir = 'rdoc'
|
20
|
+
rdoc.title = 'TwitterConnect'
|
21
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
22
|
+
rdoc.rdoc_files.include('README')
|
23
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
|
+
end
|
25
|
+
|
26
|
+
Jeweler::Tasks.new do |gemspec|
|
27
|
+
gemspec.name = "twitter_connect"
|
28
|
+
gemspec.summary = "facebook connect style twitter oauth"
|
29
|
+
gemspec.description = "facebook connect style twitter oauth"
|
30
|
+
gemspec.email = "flyerhzm@gmail.com"
|
31
|
+
gemspec.homepage = "http://github.com/flyerhzm/twitter_connect"
|
32
|
+
gemspec.authors = ["Richard Huang"]
|
33
|
+
gemspec.files.exclude '.gitignore'
|
34
|
+
gemspec.add_dependency 'twitter'
|
35
|
+
end
|
36
|
+
Jeweler::GemcutterTasks.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/init.rb
ADDED
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class TwitterConnectsController < ActionController::Base
|
2
|
+
def oauth
|
3
|
+
oauth = get_oauth
|
4
|
+
request_token = oauth.set_callback_url twitter_callback_url
|
5
|
+
session[:rtoken] = request_token.token
|
6
|
+
session[:rsecret] = request_token.secret
|
7
|
+
|
8
|
+
redirect_to request_token.authorize_url
|
9
|
+
end
|
10
|
+
|
11
|
+
def callback
|
12
|
+
oauth = get_oauth
|
13
|
+
oauth.authorize_from_request(session[:rtoken], session[:rsecret], params[:oauth_verifier])
|
14
|
+
session[:rtoken] = nil
|
15
|
+
session[:rsecret] = nil
|
16
|
+
session[:atoken] = oauth.access_token.token
|
17
|
+
session[:asecret] = oauth.access_token.secret
|
18
|
+
render :file => File.dirname(__FILE__) + '/../views/twitter_connects/callback.html.erb'
|
19
|
+
end
|
20
|
+
|
21
|
+
def logout
|
22
|
+
session[:atoken] = nil
|
23
|
+
session[:asecret] = nil
|
24
|
+
|
25
|
+
redirect_to TwitterConnect.configuration['logout_url'] || root_url
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def get_oauth
|
30
|
+
oauth = Twitter::OAuth.new(TwitterConnect.configuration['consumer_token'], TwitterConnect.configuration['consumer_secret'])
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module TwitterConnectsHelper
|
2
|
+
|
3
|
+
def tc_login_button(login_callback, options={})
|
4
|
+
if session[:atoken] and session[:asecret]
|
5
|
+
link_to options[:text] || 'Sign in with Twitter', '#', :onclick => login_callback, :class => "#{options[:class_name]}"
|
6
|
+
else
|
7
|
+
link_to options[:text] || 'Sign in with Twitter', twitter_oauth_url, :onlogin => login_callback, :class => "#{options[:class_name]} twitter_oauth"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<?xml version='1.0' encoding='utf-8' ?>
|
2
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
3
|
+
<html lang='en' xml:lang='en' xmlns='http://www.w3.org/1999/xhtml'>
|
4
|
+
<head>
|
5
|
+
<meta content='text/html;charset=UTF-8' http-equiv='content-type' />
|
6
|
+
</head>
|
7
|
+
<body>
|
8
|
+
<script type='text/javascript'>
|
9
|
+
//<![CDATA[
|
10
|
+
self.close();
|
11
|
+
//]]>
|
12
|
+
</script>
|
13
|
+
</body>
|
14
|
+
|
15
|
+
</html>
|
@@ -0,0 +1,60 @@
|
|
1
|
+
if (!TwitterConnect) {
|
2
|
+
var TwitterConnect = {};
|
3
|
+
}
|
4
|
+
TwitterConnect.Twitter = new function() {
|
5
|
+
var self = this;
|
6
|
+
|
7
|
+
this.setOauthUrl = function(url) {
|
8
|
+
self.oauth_url = url;
|
9
|
+
}
|
10
|
+
|
11
|
+
this.setCallback = function(callback) {
|
12
|
+
self.callback = callback;
|
13
|
+
}
|
14
|
+
|
15
|
+
this.startTwitterConnect = function() {
|
16
|
+
var popupParams = 'location=0,status=0,width=800,height=400';
|
17
|
+
self._twitterWindow = window.open(self.oauth_url, 'twitterConnectWindow', popupParams);
|
18
|
+
self._twitterInterval = window.setInterval(self.completeTwitterConnect, 500);
|
19
|
+
}
|
20
|
+
|
21
|
+
this.completeTwitterConnect = function() {
|
22
|
+
if (self._twitterWindow.closed) {
|
23
|
+
window.clearInterval(self._twitterInterval);
|
24
|
+
eval(self.callback);
|
25
|
+
}
|
26
|
+
}
|
27
|
+
};
|
28
|
+
|
29
|
+
function _loadTwitterConnect() {
|
30
|
+
if (document.getElementsByClassName == undefined) {
|
31
|
+
document.getElementsByClassName = function(className) {
|
32
|
+
var hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
|
33
|
+
var allElements = document.getElementsByTagName("*");
|
34
|
+
var results = [];
|
35
|
+
|
36
|
+
var element;
|
37
|
+
for (var i = 0; (element = allElements[i]) != null; i++) {
|
38
|
+
var elementClass = element.className;
|
39
|
+
if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass))
|
40
|
+
results.push(element);
|
41
|
+
}
|
42
|
+
|
43
|
+
return results;
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
var oauths = document.getElementsByClassName('twitter_oauth');
|
48
|
+
for (var i = 0; i < oauths.length; i++) {
|
49
|
+
var oauth = oauths[i];
|
50
|
+
oauth.onclick = function() {
|
51
|
+
TwitterConnect.Twitter.setOauthUrl(oauth.getAttribute('href'));
|
52
|
+
TwitterConnect.Twitter.setCallback(oauth.getAttribute('onlogin'));
|
53
|
+
TwitterConnect.Twitter.startTwitterConnect();
|
54
|
+
return false;
|
55
|
+
}
|
56
|
+
}
|
57
|
+
};
|
58
|
+
|
59
|
+
_loadSuper = window.onload;
|
60
|
+
window.onload = (typeof window.onload != 'function') ? _loadTwitterConnect : function() { _loadSuper(); _loadTwitterConnect(); };
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module TwitterConnect
|
2
|
+
@consumer_token = nil
|
3
|
+
@consumer_secret = nil
|
4
|
+
|
5
|
+
class <<self
|
6
|
+
def load_configuration(twitter_yaml_file)
|
7
|
+
return false unless File.exist?(twitter_yaml_file)
|
8
|
+
@configuration = YAML.load_file(twitter_yaml_file)[RAILS_ENV]
|
9
|
+
end
|
10
|
+
|
11
|
+
def configuration
|
12
|
+
@configuration
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'app/controllers/twitter_connects_controller'
|
18
|
+
require 'app/helpers/twitter_connects_helper'
|
19
|
+
|
20
|
+
module ActionController
|
21
|
+
class Base
|
22
|
+
helper TwitterConnectsHelper
|
23
|
+
|
24
|
+
def set_twitter_client
|
25
|
+
begin
|
26
|
+
oauth = Twitter::OAuth.new(TwitterConnect.configuration['consumer_token'], TwitterConnect.configuration['consumer_secret'])
|
27
|
+
oauth.authorize_from_access(session[:atoken], session[:asecret])
|
28
|
+
@twitter_client = Twitter::Base.new(oauth)
|
29
|
+
rescue Twitter::Unauthorized
|
30
|
+
redirect_to TwitterConnect.configuration['logout_url'] || root_url
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
attr_reader :twitter_client
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
require 'ftools'
|
39
|
+
|
40
|
+
unless File.exist?(File.join(Rails.root, "/public/javascripts/twitter_connect.js"))
|
41
|
+
File.copy(File.join(File.dirname(__FILE__), "/public/javascripts/twitter_connect.js"), File.join(Rails.root, "/public/javascripts/twitter_connect.js"))
|
42
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__),'../init.rb')
|
data/test/test_helper.rb
ADDED
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: twitter_connect
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Richard Huang
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-03-31 00:00:00 +08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: twitter
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :runtime
|
31
|
+
version_requirements: *id001
|
32
|
+
description: facebook connect style twitter oauth
|
33
|
+
email: flyerhzm@gmail.com
|
34
|
+
executables: []
|
35
|
+
|
36
|
+
extensions: []
|
37
|
+
|
38
|
+
extra_rdoc_files:
|
39
|
+
- README.md
|
40
|
+
- README_rails3.md
|
41
|
+
files:
|
42
|
+
- MIT-LICENSE
|
43
|
+
- README.md
|
44
|
+
- README_rails3.md
|
45
|
+
- Rakefile
|
46
|
+
- VERSION
|
47
|
+
- init.rb
|
48
|
+
- install.rb
|
49
|
+
- lib/app/controllers/twitter_connects_controller.rb
|
50
|
+
- lib/app/helpers/twitter_connects_helper.rb
|
51
|
+
- lib/app/views/twitter_connects/callback.html.erb
|
52
|
+
- lib/public/javascripts/twitter_connect.js
|
53
|
+
- lib/twitter_connect.rb
|
54
|
+
- rails/init.rb
|
55
|
+
- tasks/twitter_connect_tasks.rake
|
56
|
+
- test/test_helper.rb
|
57
|
+
- test/twitter_connect_test.rb
|
58
|
+
- uninstall.rb
|
59
|
+
has_rdoc: true
|
60
|
+
homepage: http://github.com/flyerhzm/twitter_connect
|
61
|
+
licenses: []
|
62
|
+
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options:
|
65
|
+
- --charset=UTF-8
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
version: "0"
|
82
|
+
requirements: []
|
83
|
+
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 1.3.6
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: facebook connect style twitter oauth
|
89
|
+
test_files:
|
90
|
+
- test/test_helper.rb
|
91
|
+
- test/twitter_connect_test.rb
|