diff 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
line wrap: on
line diff
--- a/app/controllers/tweets_controller.rb	Fri Aug 03 02:12:54 2018 +0900
+++ b/app/controllers/tweets_controller.rb	Fri Aug 03 02:31:25 2018 +0900
@@ -1,34 +1,26 @@
 class TweetsController < ApplicationController
-  before_action :validate_id, :only => :show
-
   def index
-    return try_redirect if params[:id]
+    return redirect if params[:id] || params[:name]
   end
 
   def show
-    client = Tweet.new(params[:id])
+    return redirect if params[:id][/D/].present?
+
+    client = Tweet.new(params[:id].to_i)
+    @user = client.user
+
+    return redirect if @user.screen_name != params[:name]
+
     @tweets = client.timeline
-    @user = client.user
   rescue Twitter::Error::NotFound
     head :not_found
   rescue Twitter::Error::Unauthorized
     head :forbidden
   end
 
-  private
-
-  def validate_id
-    id = params[:id].split("/")[0]
-    if id[/\D/].nil?
-      params[:id] = id
-    else
-      try_redirect
-    end
-  end
-
-  def try_redirect
-    user = Tweet.new(params[:id]).user
-    redirect_to tweet_path("#{user.id}/#{user.screen_name}")
+  def redirect
+    @user ||= Tweet.new(params[:id] || params[:name]).user
+    redirect_to tweet_path(@user.id, @user.screen_name)
   rescue Twitter::Error::NotFound
     head :not_found
   rescue Twitter::Error::Unauthorized