szimek-twitter_rails 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Szymon Nowak
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.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ = twitter_rails
2
+
3
+ Bunch of extensions to Twitter gem to simplify usage in Rails apps.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 Szymon Nowak. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "twitter_rails"
8
+ gem.summary = %Q{Extension to Twitter gem to simplify usage with Rails}
9
+ gem.email = "szymon.nowak@u2i.com"
10
+ gem.homepage = "http://github.com/szimek/twitter_rails"
11
+ gem.authors = ["Szymon Nowak"]
12
+
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+
40
+ task :default => :test
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ if File.exist?('VERSION.yml')
45
+ config = YAML.load(File.read('VERSION.yml'))
46
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "twitter_rails #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 0
4
+ :patch: 3
@@ -0,0 +1,5 @@
1
+ Twitter::Base.class_eval do
2
+ def user(id=nil)
3
+ id ? perform_get("/users/show/#{id}.json") : verify_credentials
4
+ end
5
+ end
@@ -0,0 +1,66 @@
1
+ module Twitter
2
+ module Rails
3
+ module Controller
4
+ def self.included?(controller)
5
+ controller.helper_method :twitter_session, :logged_in_via_twitter?
6
+ end
7
+
8
+ protected
9
+ def twitter_session
10
+ @twitter_session
11
+ end
12
+
13
+ def create_twitter_session
14
+ request_access_token if request_token_available?
15
+
16
+ if access_token_available?
17
+ twitter_oauth.authorize_from_access(session['twitter_atoken'], session['twitter_asecret'])
18
+ @twitter_session = Twitter::Base.new(twitter_oauth)
19
+ end
20
+ end
21
+
22
+ def logged_in_via_twitter?
23
+ !!twitter_session
24
+ end
25
+
26
+ def create_twitter_session_from_user_tokens(user=current_user)
27
+ if user && !logged_in_via_twitter? && user.twitter_atoken && user.twitter_asecret
28
+ twitter_oauth.authorize_from_access(user.twitter_atoken, user.twitter_asecret)
29
+ twitter_session = Twitter::Base.new(twitter_oauth)
30
+ end
31
+ end
32
+
33
+ def clear_twitter_session_information
34
+ session[:twitter_atoken] = nil
35
+ session[:twitter_asecret] = nil
36
+ end
37
+
38
+ def clear_user_twitter_tokens(user=current_user)
39
+ user.update_attributes(:twitter_atoken => nil,
40
+ :twitter_asecret => nil)
41
+ end
42
+
43
+ def request_access_token
44
+ oauth = self.twitter_oauth
45
+ oauth.authorize_from_request(session['twitter_rtoken'], session['twitter_rsecret'])
46
+
47
+ session['twitter_rtoken'] = nil
48
+ session['twitter_rsecret'] = nil
49
+ session['twitter_atoken'] = oauth.access_token.token
50
+ session['twitter_asecret'] = oauth.access_token.secret
51
+ end
52
+
53
+ def request_token_available?
54
+ session['twitter_rtoken'] && session['twitter_rsecret']
55
+ end
56
+
57
+ def access_token_available?
58
+ session['twitter_atoken'] && session['twitter_asecret']
59
+ end
60
+
61
+ def twitter_oauth
62
+ @twitter_oauth ||= Twitter::OAuth.new(ENV['TWITTER_CONSUMER_TOKEN'], ENV['TWITTER_CONSUMER_SECRET'])
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,17 @@
1
+ module Twitter
2
+ module Rails
3
+ module Helpers
4
+
5
+ def twitter_profile_pic(user, options={})
6
+ id = user.respond_to?(:twitter_id) ? user.twitter_id : user
7
+ begin
8
+ image_url = Twitter.user(id).profile_image_url
9
+ rescue
10
+ image_url = "http://static.twitter.com/images/default_profile_normal.png"
11
+ end
12
+ image_tag(image_url, options)
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,33 @@
1
+ module Twitter
2
+ module Rails
3
+ class << self
4
+ def load_configuration(twitter_yaml_file)
5
+ twitter_yaml_file = ::Rails.root + 'config' + 'twitter.yml'
6
+
7
+ if File.exists?(twitter_yaml_file)
8
+ twitter_config = YAML.load(File.read(twitter_yaml_file))[RAILS_ENV]
9
+ ENV['TWITTER_CONSUMER_TOKEN'] = twitter_config['token']
10
+ ENV['TWITTER_CONSUMER_SECRET'] = twitter_config['secret']
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ class ActionController::Base
18
+ class << self
19
+ def inherited_with_twitter(subclass)
20
+ inherited_without_twitter(subclass)
21
+ if subclass.to_s == "ApplicationController"
22
+ subclass.class_eval do
23
+ include Twitter::Rails::Controller
24
+ helper Twitter::Rails::Helpers
25
+ end
26
+ end
27
+ end
28
+ alias_method_chain :inherited, :twitter
29
+ end
30
+ end
31
+
32
+ twitter_config = Rails.root + 'config' + 'twitter.yml'
33
+ Twitter::Rails.load_configuration(twitter_config)
@@ -0,0 +1,6 @@
1
+ if defined? Rails
2
+ require 'twitter/base'
3
+ require 'twitter/rails/controller'
4
+ require 'twitter/rails/helpers'
5
+ require 'twitter/rails/initializer'
6
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'twitter_rails'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class TwitterRailsTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: szimek-twitter_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Szymon Nowak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-20 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: szymon.nowak@u2i.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - LICENSE
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION.yml
30
+ - lib/twitter/base.rb
31
+ - lib/twitter/rails/controller.rb
32
+ - lib/twitter/rails/helpers.rb
33
+ - lib/twitter/rails/initializer.rb
34
+ - lib/twitter_rails.rb
35
+ - test/test_helper.rb
36
+ - test/twitter_rails_test.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/szimek/twitter_rails
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.2.0
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Extension to Twitter gem to simplify usage with Rails
63
+ test_files:
64
+ - test/test_helper.rb
65
+ - test/twitter_rails_test.rb