-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.rb
154 lines (124 loc) · 3.69 KB
/
application.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
require 'rubygems'
require 'bundler/setup'
require 'torquebox'
require 'sinatra'
require 'sinatra-twitter-oauth'
require 'sass'
require 'rack-flash'
require 'sinatra/url_for'
require File.join(File.dirname(__FILE__), 'environment')
require 'helpers'
module Chirpr
class Application < Sinatra::Base
register Sinatra::TwitterOAuth
use Rack::Flash
helpers Sinatra::UrlForHelper
helpers Chirpr::Helpers
error do
e = request.env['sinatra.error']
puts e.backtrace.join("\n")
'Application error'
end
configure do
set :twitter_oauth_config, :key => SiteConfig.twitter_key,
:secret => SiteConfig.twitter_secret,
:callback => SiteConfig.twitter_callback,
:login_template => {:haml=>:login}
end
['/home', '/friends', '/followers', '/follow', '/unfollow', '/chirp'].each do |path|
before path do
login_required
end
end
before do
puts "ENV['oauth_key'] and ENV['oauth_secret'] not set. Can't do much without them." unless configured?
# Will be null if not logged in
@profile = get_profile( screen_name )
end
get '/' do
haml :root
end
get '/profiles' do
@profiles = Profile.all
haml :profiles
end
get '/home' do
haml :home
end
get '/friends' do
haml :friends
end
get '/followers' do
haml :followers
end
post '/follow' do
if @friend = Profile.get(params[:id])
@profile.follow( @friend )
flash.now[:notice] = "You are now following #{@friend.name}"
else
flash.now[:notice] = "Sorry. Can't seem to find the person you're looking for."
end
haml :home
end
post '/unfollow' do
if @friend = Profile.get(params[:id])
@profile.unfollow( @friend )
flash.now[:notice] = "You are no longer following #{@friend.name}"
else
flash.now[:notice] = "Sorry. Can't seem to find the person you're looking for."
end
haml :home
end
post '/chirp' do
@chirp = @profile.chirps.create(:message=>params[:message], :created_at=>Time.now)
unless @chirp.valid?
flash[:notice] = "Hrm. Something went wrong. Sorry about that.<br>"
flash[:notice] << @chirp.errors[:message].first
end
redirect to("/#{screen_name}")
end
get '/main.css' do
scss :main
end
# This is a wildcard route - it has to be the last one
get '/:username' do
if profile = get_profile( params[:username] )
@chirps = profile ? profile.chirps.reverse : []
haml :chirps
else
flash.now[:notice] = "Hrm. Can't find that user"
haml :root
end
end
end
end
# Monkey patch this thing so we can create new profiles when needed
module Sinatra::TwitterOAuth
module Helpers
def get_profile( name )
return nil unless name
Profile.first( :name => name.downcase )
end
def get_or_create_profile( name )
name.downcase!
@profile = get_profile( name )
unless @profile
@profile = Profile.create!( :name => name, :created_at => Time.now, :updated_at => Time.now )
end
@profile.icon_url = session[:user]["profile_image_url"]
@profile.save
end
def authenticate!
access_token = get_access_token
if @client.authorized?
session[:access_token] = access_token.token
session[:secret_token] = access_token.secret
session[:user] = @client.info
get_or_create_profile( @client.info["screen_name"] )
session[:user]
else
nil
end
end
end
end