Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 129 lines (99 sloc) 3.314 kb
9708516 @twe4ked Initial commit.
authored
1 ENV['RACK_ENV'] ||= 'development'
2
3 require 'bundler'
4 Bundler.require(:default, ENV['RACK_ENV'].to_sym)
5
6 require 'logger'
7 require 'sinatra/reloader' if Sinatra::Base.development?
8
9 class TmpFollow < Sinatra::Base
10 enable :sessions
11 use Rack::Flash
12
13 configure do
14 Dir.glob('./lib/*.rb') { |file| require file }
15
16 database_url = ENV['SHARED_DATABASE_URL'] || "postgres://#{ENV['USER']}@localhost/tmpfollow_#{development? ? 'development' : 'test'}"
17
18 DataMapper.setup(:default, database_url)
19 DataMapper::Logger.new($stdout, :debug)
20
21 Twitter.configure do |config|
22 config.consumer_key = ENV['TWITTER_CONSUMER_KEY']
23 config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET']
24 end
25
26 Compass.configuration do |config|
27 config.project_path = File.dirname(__FILE__)
28 config.sass_dir = 'views'
29 end
30
31 set :haml, {format: :html5}
32 end
33
4e2da24 @twe4ked Replace oauth gem with omniauth for Twitter authentication.
authored
34 use OmniAuth::Builder do
35 provider :twitter, ENV['TWITTER_CONSUMER_KEY'], ENV['TWITTER_CONSUMER_SECRET']
36 end
37
9708516 @twe4ked Initial commit.
authored
38 helpers do
39 def flashes
40 [:notice, :alert].map do |type|
41 "<p class='flash #{type}'>#{flash[type]}</p>" if flash[type] != nil
42 end.join("\n")
43 end
9151863 @twe4ked Add `authenticated?` helper method.
authored
44
45 def authenticated?
46 session[:oauth_token] != nil && session[:oauth_token_secret] != nil
47 end
9708516 @twe4ked Initial commit.
authored
48 end
49
50 get '/' do
51 haml :index
52 end
53
54 before '/follow' do
55 # validate username matches twitter username format
56 # TODO: remove the '@' if it exists
57 unless params[:user] =~ /^[A-Za-z0-9_]+/
58 flash[:alert] = 'Username invalid.'
59 redirect to '/'
60 end
61
62 # validate days is an integer
63 unless is_numeric?(params[:days]) || params[:days] == ''
64 flash[:alert] = 'Not a number.'
65 redirect to '/'
66 end
67 end
68
69 post '/follow' do
70 unfollow = Unfollow.new.tap do |u|
71 u.user = params[:user]
72 u.date = Date.today + params[:days].to_i
73 u.oauth_token = session[:oauth_token]
74 u.oauth_token_secret = session[:oauth_token_secret]
75 end
76
77 client = Twitter::Client.new(
78 :oauth_token => unfollow.oauth_token,
79 :oauth_token_secret => unfollow.oauth_token_secret
80 )
81 client.follow unfollow.user
82
83 if unfollow.save
84 flash[:notice] = "@#{unfollow.user} will be unfollowed on #{unfollow.date.strftime("%A, %b #{unfollow.date.day.ordinalize}")}!"
85 else
86 flash[:alert] = 'Not saved.'
87 end
88
89 redirect to '/'
90 end
91
4e2da24 @twe4ked Replace oauth gem with omniauth for Twitter authentication.
authored
92 get '/auth/:provider/callback' do
93 auth_hash = env['omniauth.auth'] # => OmniAuth::AuthHash
9708516 @twe4ked Initial commit.
authored
94
95 # store oauth_token and oauth_token_secret in the session
4e2da24 @twe4ked Replace oauth gem with omniauth for Twitter authentication.
authored
96 session[:oauth_token] = auth_hash[:credentials][:token]
97 session[:oauth_token_secret] = auth_hash[:credentials][:secret]
9708516 @twe4ked Initial commit.
authored
98
653bc54 @twe4ked Show the authenticated user with a 'log out' link.
authored
99 # store username
100 session[:username] = auth_hash[:info][:nickname]
101
4e2da24 @twe4ked Replace oauth gem with omniauth for Twitter authentication.
authored
102 flash[:notice] = 'Authenticated successfully.'
103 redirect to '/'
104 end
105
106 get '/auth/failure' do
107 flash[:alert] = 'Authentication failed.'
9708516 @twe4ked Initial commit.
authored
108 redirect to '/'
109 end
110
6ed1204 @twe4ked Tweak signing out.
authored
111 get '/signout' do
9708516 @twe4ked Initial commit.
authored
112 session[:oauth_token] = nil
113 session[:oauth_token_secret] = nil
114
6ed1204 @twe4ked Tweak signing out.
authored
115 flash[:alert] = 'Signed out successfully.'
9708516 @twe4ked Initial commit.
authored
116 redirect to '/'
117 end
118
119 get '/application.css' do
120 scss :application, Compass.sass_engine_options
121 end
122
123 private
124
125 def is_numeric?(str)
126 !!str.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/)
127 end
128 end
Something went wrong with that request. Please try again.