comparison app/controllers/tweets_controller.rb @ 165:5af9b537db86

Unbreak \everything/ - simplify routing, no more split hackery - fix user lookup and differentiate between id and name lookup
author nanaya <me@nanaya.pro>
date Fri, 03 Aug 2018 02:31:25 +0900
parents 59a4645fd24c
children 469df6354341
comparison
equal deleted inserted replaced
164:59a4645fd24c 165:5af9b537db86
1 class TweetsController < ApplicationController 1 class TweetsController < ApplicationController
2 before_action :validate_id, :only => :show
3
4 def index 2 def index
5 return try_redirect if params[:id] 3 return redirect if params[:id] || params[:name]
6 end 4 end
7 5
8 def show 6 def show
9 client = Tweet.new(params[:id]) 7 return redirect if params[:id][/D/].present?
8
9 client = Tweet.new(params[:id].to_i)
10 @user = client.user
11
12 return redirect if @user.screen_name != params[:name]
13
10 @tweets = client.timeline 14 @tweets = client.timeline
11 @user = client.user
12 rescue Twitter::Error::NotFound 15 rescue Twitter::Error::NotFound
13 head :not_found 16 head :not_found
14 rescue Twitter::Error::Unauthorized 17 rescue Twitter::Error::Unauthorized
15 head :forbidden 18 head :forbidden
16 end 19 end
17 20
18 private 21 def redirect
19 22 @user ||= Tweet.new(params[:id] || params[:name]).user
20 def validate_id 23 redirect_to tweet_path(@user.id, @user.screen_name)
21 id = params[:id].split("/")[0]
22 if id[/\D/].nil?
23 params[:id] = id
24 else
25 try_redirect
26 end
27 end
28
29 def try_redirect
30 user = Tweet.new(params[:id]).user
31 redirect_to tweet_path("#{user.id}/#{user.screen_name}")
32 rescue Twitter::Error::NotFound 24 rescue Twitter::Error::NotFound
33 head :not_found 25 head :not_found
34 rescue Twitter::Error::Unauthorized 26 rescue Twitter::Error::Unauthorized
35 head :forbidden 27 head :forbidden
36 end 28 end