hellhound-twitter 0.0.3
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/LICENSE +22 -0
- data/README +0 -0
- data/Rakefile +24 -0
- data/examples/simple-login-twitter.rb +42 -0
- data/lib/hellhound-twitter.rb +82 -0
- metadata +101 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2010 Pedro Del Gallego.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |s|
|
7
|
+
s.name = 'hellhound-twitter'
|
8
|
+
s.summary = 'A auth system that relay in twitter oauth'
|
9
|
+
s.email = 'pedro.delgallego@gmail.com'
|
10
|
+
s.homepage = 'http://github.com/pedrodelgallego/hellhound-twitter'
|
11
|
+
s.version = '0.0.3'
|
12
|
+
s.authors = ["Pedro Del Gallego"]
|
13
|
+
s.files = FileList['[A-Z]*', '{lib,spec,bin,examples}/**/*']
|
14
|
+
|
15
|
+
s.add_dependency 'rack'
|
16
|
+
s.add_dependency 'rack-oauth'
|
17
|
+
s.add_dependency 'sinatra'
|
18
|
+
|
19
|
+
s.extra_rdoc_files = %w( README.rdoc )
|
20
|
+
end
|
21
|
+
Jeweler::GemcutterTasks.new
|
22
|
+
rescue LoadError
|
23
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
24
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
%w( rubygems sinatra rack-oauth
|
2
|
+
json haml dm-core memcached
|
3
|
+
).each {|gem| require gem}
|
4
|
+
|
5
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "../lib/hellhound-twitter.rb"))
|
6
|
+
|
7
|
+
configure do
|
8
|
+
use Rack::OAuth, :key => '',
|
9
|
+
:secret => '',
|
10
|
+
:site => 'http://twitter.com'
|
11
|
+
|
12
|
+
# If you want to use the cache just uncomment the next line
|
13
|
+
# Sinatra::Hellhound::CACHE = Memcached.new
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
helpers do
|
18
|
+
include Rack::OAuth::Methods
|
19
|
+
include Sinatra::Hellhound::Helpers
|
20
|
+
|
21
|
+
def get_user twitter_id
|
22
|
+
# The user is not in the cache so do something to get it from there
|
23
|
+
session[:user]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
get "/" do
|
28
|
+
logged_in? ? "Hi #{current_user}! " : "Hi stranger"
|
29
|
+
end
|
30
|
+
|
31
|
+
get "/oauth_complete" do
|
32
|
+
create_or_retrive_user do |user_data|
|
33
|
+
# Do something smart to create you user and return it to the method
|
34
|
+
user_data["name"]
|
35
|
+
end
|
36
|
+
redirect "/"
|
37
|
+
end
|
38
|
+
|
39
|
+
get "/logout" do
|
40
|
+
logout!
|
41
|
+
redirect "/"
|
42
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'rack-oauth'
|
3
|
+
|
4
|
+
module Sinatra
|
5
|
+
module Hellhound
|
6
|
+
module Helpers
|
7
|
+
|
8
|
+
##
|
9
|
+
# Returns the user from the database, it can be overwritten to
|
10
|
+
# adapat to the other strategies
|
11
|
+
#
|
12
|
+
def get_user twitter_id
|
13
|
+
raise Exception, "You need to override this method"
|
14
|
+
end
|
15
|
+
|
16
|
+
##
|
17
|
+
# Returns the current_user, it's an instance of <tt>user</tt> model
|
18
|
+
#
|
19
|
+
def current_user
|
20
|
+
if session[:user]
|
21
|
+
Sinatra::Hellhound::CACHE ? Sinatra::Hellhound::CACHE.get("user-#{session[:user]}") : get_user(session[:user])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
##
|
26
|
+
# Returns true if +current_user+ is logged and active.
|
27
|
+
#
|
28
|
+
def logged_in?
|
29
|
+
!! session[:user]
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
##
|
34
|
+
# Returns the user that logged out and remove user from the
|
35
|
+
# cache and the session
|
36
|
+
#
|
37
|
+
def logout!
|
38
|
+
user = session[:user]
|
39
|
+
session.clear
|
40
|
+
user
|
41
|
+
end
|
42
|
+
|
43
|
+
##
|
44
|
+
# Retrive the user from the database or create a new one if it
|
45
|
+
# doesn't exists.
|
46
|
+
#
|
47
|
+
# If you want to store the user in memcached them you need to
|
48
|
+
# return the user at the end of the block
|
49
|
+
#
|
50
|
+
# I.e
|
51
|
+
#
|
52
|
+
# create_or_retrive_user do |user_data| #
|
53
|
+
# user = User.find :twitter_id => user_data["id"].to_s
|
54
|
+
# User.create :twitter_id => user_data["id"].to_s unless user
|
55
|
+
# user
|
56
|
+
# end
|
57
|
+
#
|
58
|
+
def create_or_retrive_user(&block)
|
59
|
+
user_data = get_credentials
|
60
|
+
session[:user] = user_data["id"].to_s
|
61
|
+
user = yield user_data
|
62
|
+
|
63
|
+
Sinatra::Hellhound::CACHE.set "user-#{session[:user]}", user if Sinatra::Hellhound::CACHE
|
64
|
+
end
|
65
|
+
|
66
|
+
##
|
67
|
+
# Returns a Hash with the user data.
|
68
|
+
#
|
69
|
+
def get_credentials
|
70
|
+
user_data = JSON.parse(get_access_token.get('/account/verify_credentials.json').body)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
Sinatra::Hellhound::CACHE = nil
|
75
|
+
|
76
|
+
def self.registered(app)
|
77
|
+
app.enable :sessions
|
78
|
+
end
|
79
|
+
end # Module Hellhound
|
80
|
+
|
81
|
+
register Hellhound
|
82
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hellhound-twitter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Pedro Del Gallego
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-12 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rack
|
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
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: rack-oauth
|
34
|
+
prerelease: false
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
segments:
|
40
|
+
- 0
|
41
|
+
version: "0"
|
42
|
+
type: :runtime
|
43
|
+
version_requirements: *id002
|
44
|
+
- !ruby/object:Gem::Dependency
|
45
|
+
name: sinatra
|
46
|
+
prerelease: false
|
47
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
type: :runtime
|
55
|
+
version_requirements: *id003
|
56
|
+
description:
|
57
|
+
email: pedro.delgallego@gmail.com
|
58
|
+
executables: []
|
59
|
+
|
60
|
+
extensions: []
|
61
|
+
|
62
|
+
extra_rdoc_files: []
|
63
|
+
|
64
|
+
files:
|
65
|
+
- LICENSE
|
66
|
+
- README
|
67
|
+
- Rakefile
|
68
|
+
- examples/simple-login-twitter.rb
|
69
|
+
- lib/hellhound-twitter.rb
|
70
|
+
has_rdoc: true
|
71
|
+
homepage: http://github.com/pedrodelgallego/hellhound-twitter
|
72
|
+
licenses: []
|
73
|
+
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options:
|
76
|
+
- --charset=UTF-8
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
requirements: []
|
94
|
+
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 1.3.6
|
97
|
+
signing_key:
|
98
|
+
specification_version: 3
|
99
|
+
summary: A auth system that relay in twitter oauth
|
100
|
+
test_files:
|
101
|
+
- examples/simple-login-twitter.rb
|