-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
117 lines (98 loc) · 3.42 KB
/
app.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
require 'rubygems'
require 'uri'
require 'sinatra'
require 'json'
require 'rest_client'
require 'data_mapper'
require 'active_support'
require 'twitter'
require 'nokogiri'
class User
include DataMapper::Resource
property :id, Serial
property :readmill_id, Integer
property :name, String
property :access_token, String
property :twitter_handle, String
end
class Message
include DataMapper::Resource
property :id, Serial
property :last_id, String, :default => 1
end
DataMapper.finalize
DataMapper::Logger.new($stdout, :debug)
configure do
DataMapper.setup(:default, (ENV["DATABASE_URL"] || {
:adapter => 'mysql',
:host => 'localhost',
:username => 'root' ,
:password => '',
:database => 'tweetmill'}))
DataMapper.auto_upgrade!
end
Twitter.configure do |config|
config.consumer_key = "F6fdEc2IEDr8ysrOXeXwA"
config.consumer_secret = "c6WNSEgqndgoOW8RW9oP7jtgEjpWwflnQJMvdzYso"
config.oauth_token = "541793455-CoteuIjFNcz6qSy7wwgswSrX1d2ooSDBJTDJfS1N"
config.oauth_token_secret = "Gm1Zb46QFQ3nV8qeRt7L5mtsKOAbxG81eWyyqj8mT4"
end
set :readmill_client_id, "3157dd6728aacd2cf93e3588893e9848"
set :readmill_client_secret, "d2ddd931b979fcc461e677497f22bafc"
set :readmill_redirect, "http://tweetmill.herokuapp.com/callback/readmill"
def fetch_and_parse(uri, token = nil, limit = nil)
puts "Fetching and parsing: #{uri} with token #{session[:token]}"
limit = "&count=#{limit}" if limit
url = "#{uri}?client_id=#{settings.readmill_client_id}#{limit}"
url = "#{url}&access_token=#{token}" if token
content = RestClient.get(url, :accept => :json).to_str
puts " Got Content: #{content}"
JSON.parse(content) rescue nil
end
get '/auth/readmill' do
redirect "http://readmill.com/oauth/authorize?response_type=code&client_id=#{settings.readmill_client_id}&redirect_uri=#{settings.readmill_redirect}&scope=non-expiring"
end
get '/callback/readmill' do
token_params = {
:grant_type => 'authorization_code',
:client_id => settings.readmill_client_id,
:client_secret => settings.readmill_client_secret,
:redirect_uri => settings.readmill_redirect,
:code => params[:code],
:scope => 'non-expiring'
}
resp = JSON.parse(RestClient.post("http://readmill.com/oauth/token.json", token_params).to_str) rescue nil
data = {
:user => fetch_and_parse("http://api.readmill.com/me.json", resp['access_token'])
}
user = User.first_or_create({ :readmill_id => data[:user]['id'] })
user.name = data[:user]['username']
user.access_token = resp['access_token']
@access_token = resp['access_token']
user.save!
erb :twitter
end
### -- TEMPLATES -- ###
get '/' do
erb :index
end
post '/done' do
user = User.first({:access_token => params[:access_token]})
if user
user.twitter_handle = params[:twitter_handle].downcase
user.save
Twitter.follow(user.twitter_handle)
begin
Twitter.update("@#{user.twitter_handle} Welcome to Tweetmill. Follow me and I will DM you otherwise @reply. Sometimes I take a few minutes to reply, sit tight.")
rescue Twitter::Error::Forbidden
Twitter.update("@#{user.twitter_handle} Welcome to Tweetmill. Follow me and I will DM you otherwise @reply. I might take some minutes to reply, keep calm.") rescue Twitter::Error::Forbidden
end
redirect "/thanks"
else
redirect "/?error=no such user"
end
end
get '/thanks' do
erb :thanks
end
get('/static/:filename') { send_file("./static/#{params[:filename]}") }