/
app.rb
130 lines (102 loc) · 3.3 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
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-
$:.unshift File.dirname(__FILE__)
Bundler.require(:default, :web)
require 'database'
require 'omniauth'
require 'time'
require 'pp'
require 'erb'
CONSUMER_KEY, CONSUMER_SECRET = File.open("consumer.cfg").read.split("\n")
class App < Sinatra::Base
configure do
include ERB::Util
use Rack::Session::Cookie, :secret => "change me",
:expire_after => 3600 * 24 * 2
set :logging, true
set :dump_errors, true
set :show_exceptions, true
end
use OmniAuth::Builder do
provider :twitter, CONSUMER_KEY, CONSUMER_SECRET
end
helpers do
def current_user
@current_user ||= User.find_by_screen_name(session[:screen_name])
end
def login?
return unless session[:screen_name] or current_user
return true
end
def paginate_options(params)
opts = {:page => 1, :per_page => 50}
opts.each{|k, v| opts[k] = params[k].to_i if params[k] and params[k].to_i > 0}
opts
end
end
get '/auth/twitter/callback' do
auth = request.env["omniauth.auth"]
access_token = auth["extra"]["access_token"]
curr_user = auth["extra"]["raw_info"]
mongo_user = User.find_or_create_by_user_id(curr_user.id)
mongo_user.screen_name = curr_user.screen_name
mongo_user.name = curr_user.name
mongo_user.profile_image_url = curr_user.profile_image_url
mongo_user.access_token = access_token.token
mongo_user.access_secret= access_token.secret
mongo_user.save
session[:screen_name] = curr_user.screen_name
redirect "/"
end
get '/' do
redirect '/home' if login?
erb :index
end
before %r{/home|/ajax|/api} do
redirect '/' unless login?
end
get '/home' do
opts = paginate_options(params)
@articles = current_user.retrieve_articles(opts)
@title = current_user.screen_name
@has_next_page = (@articles.size == opts[:per_page])
@next_page_url = "/home?page=#{opts[:page] + 1}" if @has_next_page
erb :user_home
end
get '/home/article/:article_id' do
@article = current_user.retrieve_article(params[:article_id])
erb :article_detail
end
get '/home/:year/:mon/:day' do
opts = paginate_options(params)
articles_in_date = Articles_in_date.first({
:user_id => current_user.id,
:year => params[:year].to_i,
:mon => params[:mon].to_i,
:day => params[:day].to_i}).articles.reverse
@articles = articles_in_date[((opts[:page] - 1) * 50)..((opts[:page] * 50) - 1)]
@has_next_page = (@articles.size == opts[:per_page])
@next_page_url = "/home/#{params[:year]}/#{params[:mon]}/#{params[:day]}?page=#{opts[:page] + 1}" if @has_next_page
erb :user_home
end
get '/ajax/article/:article_id' do
@article = current_user.retrieve_article(params[:article_id])
erb :article_detail, :layout => false
end
get '/api/article/pickup/:article_id' do
@article = current_user.retrieve_article(params[:article_id])
unless @article.pickup
@article.pickup = true
else
@article.pickup = false
end
@article.save
return @article.to_json
end
get '/api/articles/recent' do
"このAPIはセキュリティ上の問題が報告されているため一時的に利用できません"
end
get '/logout' do
session.delete(:screen_name)
redirect '/'
end
end