/
tmpfollow.rb
121 lines (94 loc) · 3.05 KB
/
tmpfollow.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
ENV['RACK_ENV'] ||= 'development'
require 'bundler'
Bundler.require(:default, ENV['RACK_ENV'].to_sym)
require 'logger'
require 'sinatra/reloader' if Sinatra::Base.development?
class TmpFollow < Sinatra::Base
enable :sessions
use Rack::Flash
configure do
Dir.glob('./lib/*.rb') { |file| require file }
database_url = ENV['SHARED_DATABASE_URL'] || "postgres://#{ENV['USER']}@localhost/tmpfollow_#{development? ? 'development' : 'test'}"
DataMapper.setup(:default, database_url)
DataMapper::Logger.new($stdout, :debug)
Twitter.configure do |config|
config.consumer_key = ENV['TWITTER_CONSUMER_KEY']
config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET']
end
Compass.configuration do |config|
config.project_path = File.dirname(__FILE__)
config.sass_dir = 'views'
end
set :haml, {format: :html5}
end
use OmniAuth::Builder do
provider :twitter, ENV['TWITTER_CONSUMER_KEY'], ENV['TWITTER_CONSUMER_SECRET']
end
helpers do
def flashes
[:notice, :alert].map do |type|
"<p class='flash #{type}'>#{flash[type]}</p>" if flash[type] != nil
end.join("\n")
end
end
get '/' do
haml :index
end
before '/follow' do
# validate username matches twitter username format
# TODO: remove the '@' if it exists
unless params[:user] =~ /^[A-Za-z0-9_]+/
flash[:alert] = 'Username invalid.'
redirect to '/'
end
# validate days is an integer
unless is_numeric?(params[:days]) || params[:days] == ''
flash[:alert] = 'Not a number.'
redirect to '/'
end
end
post '/follow' do
unfollow = Unfollow.new.tap do |u|
u.user = params[:user]
u.date = Date.today + params[:days].to_i
u.oauth_token = session[:oauth_token]
u.oauth_token_secret = session[:oauth_token_secret]
end
client = Twitter::Client.new(
:oauth_token => unfollow.oauth_token,
:oauth_token_secret => unfollow.oauth_token_secret
)
client.follow unfollow.user
if unfollow.save
flash[:notice] = "@#{unfollow.user} will be unfollowed on #{unfollow.date.strftime("%A, %b #{unfollow.date.day.ordinalize}")}!"
else
flash[:alert] = 'Not saved.'
end
redirect to '/'
end
get '/auth/:provider/callback' do
auth_hash = env['omniauth.auth'] # => OmniAuth::AuthHash
# store oauth_token and oauth_token_secret in the session
session[:oauth_token] = auth_hash[:credentials][:token]
session[:oauth_token_secret] = auth_hash[:credentials][:secret]
flash[:notice] = 'Authenticated successfully.'
redirect to '/'
end
get '/auth/failure' do
flash[:alert] = 'Authentication failed.'
redirect to '/'
end
get '/clear' do
session[:oauth_token] = nil
session[:oauth_token_secret] = nil
flash[:alert] = 'Tokens cleared.'
redirect to '/'
end
get '/application.css' do
scss :application, Compass.sass_engine_options
end
private
def is_numeric?(str)
!!str.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/)
end
end