/
accounts_controller.rb
84 lines (73 loc) · 3.46 KB
/
accounts_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
##
# Donate Your Account (donateyouraccount.com)
# Copyright (C) 2011 Kyle Shank (kyle.shank@gmail.com)
# http://www.gnu.org/licenses/agpl.html
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##
class AccountsController < ApplicationController
include AccountsHelper
before_filter :login_required, :only => [:index]
def index
@donated_statuses = Status.donated_through_account(current_account).desc.paginate(:page => params[:page], :per_page=>10)
@campaign = current_account.campaign
@status = Status.new
@campaigns = Campaign.suggest_for(current_account.id).desc.limit(4)
end
def new
redirect_to get_twitter_request_token.authorize_url.gsub("authorize","authenticate")
end
def create
consumer = OAuth::Consumer.new(Twitter.consumer_key, Twitter.consumer_secret, {:site=>"http://twitter.com" })
request_token = OAuth::RequestToken.new(consumer, session[:request_token], session[:request_token_secret])
begin
access_token = request_token.get_access_token(:oauth_verifier => params[:oauth_verifier])
response = consumer.request(:get, '/account/verify_credentials.json', access_token, { :scheme => :query_string })
case response
when Net::HTTPSuccess
user_info = JSON.parse(response.body)
@account = Account.first(:conditions => {:uid => user_info["id"]})
unless @account
@account = Account.new(
:uid => user_info["id"],
:screen_name => user_info["screen_name"],
:token => access_token.token,
:secret => access_token.secret
)
end
@account.name = user_info["name"]
@account.followers = (user_info["followers_count"] || 0).to_i
@account.url = user_info["url"]
@account.description = user_info["description"]
@account.location = user_info["location"]
@account.profile_sidebar_border_color = user_info["profile_sidebar_border_color"]
@account.profile_sidebar_fill_color = user_info["profile_sidebar_fill_color"]
@account.profile_link_color = user_info["profile_link_color"]
@account.profile_image_url = user_info["profile_image_url"]
@account.profile_background_color = user_info["profile_background_color"]
@account.profile_background_image_url = user_info["profile_background_image_url"]
@account.profile_text_color = user_info["profile_text_color"]
@account.profile_background_tile = user_info["profile_background_tile"]
@account.profile_use_background_image = user_info["profile_use_background_image"]
if @account.save
self.current_account=@account
end
redirect_back_or_default dashboard_path
end
rescue OAuth::Unauthorized
request.flash.now["notice"] = "Oops! OAuth Unauthorized error."
redirect_to "/"
end
end
end