Module: Spherebird
- Defined in:
- lib/spherebird.rb
Instance Method Summary collapse
- #config_file_path ⇒ Object
- #on_twitter_profile_update ⇒ Object
- #profile_cache_lifetime ⇒ Object
- #twitter ⇒ Object
- #twitter_authorized? ⇒ Boolean
- #twitter_logged_in? ⇒ Boolean
- #twitter_login_state ⇒ Object
- #twitter_oauth ⇒ Object
- #twitter_oauth_config ⇒ Object
- #twitter_oauth_consumer ⇒ Object
- #twitter_oauth_request(oauth_token = nil) ⇒ Object
-
#twitter_profile(options = {}) ⇒ Object
options: force_update (BOOL).
- #twitter_session_clear ⇒ Object
- #twitter_session_read(key) ⇒ Object
- #twitter_session_write(key, value) ⇒ Object
Instance Method Details
#config_file_path ⇒ Object
6 7 8 |
# File 'lib/spherebird.rb', line 6 def config_file_path "config/twitter_oauth.yml" end |
#on_twitter_profile_update ⇒ Object
161 162 |
# File 'lib/spherebird.rb', line 161 def on_twitter_profile_update end |
#profile_cache_lifetime ⇒ Object
10 11 12 |
# File 'lib/spherebird.rb', line 10 def profile_cache_lifetime 1800 end |
#twitter ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/spherebird.rb', line 111 def twitter return @twitter if @twitter Twitter.configure do |config| config.consumer_key = twitter_oauth_config['consumer_token'] config.consumer_secret = twitter_oauth_config['consumer_secret'] config.oauth_token = twitter_session_read(:access_token) config.oauth_token_secret = twitter_session_read(:access_token_secret) if twitter_oauth_config['proxy'] config.proxy = twitter_oauth_config['proxy'] end end @twitter = Twitter::Client.new end |
#twitter_authorized? ⇒ Boolean
56 57 58 |
# File 'lib/spherebird.rb', line 56 def twitter_login_state == "authorized" end |
#twitter_logged_in? ⇒ Boolean
52 53 54 |
# File 'lib/spherebird.rb', line 52 def twitter_logged_in? twitter_login_state == "logged_in" end |
#twitter_login_state ⇒ Object
48 49 50 |
# File 'lib/spherebird.rb', line 48 def twitter_login_state twitter_session_read(:oauth_state) end |
#twitter_oauth ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/spherebird.rb', line 60 def twitter_oauth if twitter_logged_in? redirect_to twitter_oauth_config['logged_in_url'] elsif @request_token = OAuth::RequestToken.new(twitter_oauth_consumer, twitter_session_read(:request_token), twitter_session_read(:request_token_secret)) begin @access_token = @request_token.get_access_token() rescue OAuth::Unauthorized => e twitter_session_clear redirect_to :controller => 'oauth', :action => 'index' return rescue => e raise e end twitter_session_write :access_token, @access_token.token twitter_session_write :access_token_secret, @access_token.secret twitter_session_write :oauth_state, "logged_in" redirect_to :controller => 'users', :action => 'profile' else request_token = twitter_oauth_consumer.get_request_token( :oauth_callback => twitter_oauth_config['callback_url'] ) twitter_session_clear twitter_session_write :request_token, request_token.token twitter_session_write :request_token_secret, request_token.secret redirect_to request_token. end end |
#twitter_oauth_config ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/spherebird.rb', line 14 def twitter_oauth_config if @twitter_oauth_config.nil? if File.exists?(config_file_path) twitter_oauth_configs = YAML::load_file(config_file_path) @twitter_oauth_config = twitter_oauth_configs[Rails.env] else raise <<-EXCEPTION Twitter-OAuth Configurate File (#{config_file_path}) is missing. Example: EXCEPTION end end @twitter_oauth_config end |
#twitter_oauth_consumer ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/spherebird.rb', line 30 def twitter_oauth_consumer = { :site => "http://twitter.com", :request_token_url => "https://api.twitter.com/oauth/request_token", :authorize_url => "https://api.twitter.com/oauth/authorize", :access_token_url => "https://api.twitter.com/oauth/access_token", } if twitter_oauth_config['proxy'] [:proxy] = twitter_oauth_config['proxy'] end puts OAuth::Consumer.new( twitter_oauth_config['consumer_token'], twitter_oauth_config['consumer_secret'], ) end |
#twitter_oauth_request(oauth_token = nil) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/spherebird.rb', line 104 def twitter_oauth_request (oauth_token = nil) if oauth_token.nil? || oauth_token == twitter_session_read(:request_token) twitter_session_write :oauth_state, "authorized" end twitter_oauth end |
#twitter_profile(options = {}) ⇒ Object
options:
force_update (BOOL)
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/spherebird.rb', line 131 def twitter_profile( = {}) profile = twitter_session_read(:profile) expiry = twitter_session_read(:profile_expiry_date) if [:force_update] || profile.nil? || expiry.nil? || (expiry - Time.now).to_i < 0 twitter_session_write(:profile_expiry_date, Time.now + profile_cache_lifetime) begin profile = twitter.verify_credentials rescue Twitter::Error::Unauthorized return nil end twitter_session_write(:profile, profile) on_twitter_profile_update end return profile end |
#twitter_session_clear ⇒ Object
157 158 159 |
# File 'lib/spherebird.rb', line 157 def twitter_session_clear session[:twitter] = nil end |
#twitter_session_read(key) ⇒ Object
147 148 149 150 |
# File 'lib/spherebird.rb', line 147 def twitter_session_read (key) session[:twitter] ||= {} session[:twitter][key] end |
#twitter_session_write(key, value) ⇒ Object
152 153 154 155 |
# File 'lib/spherebird.rb', line 152 def twitter_session_write (key, value) session[:twitter] ||= {} session[:twitter][key] = value end |