# HG changeset patch # User nanaya # Date 1435853860 -32400 # Node ID 8f68ca6060999ffe6fa37407f30dcabb8584df52 # Parent e01fb1aa55a30a9740fa29f4e64a8f2a64f10b0c Add twitter id to the url so it'll be consistent. Not sure if there will be use case of "following this id". diff -r e01fb1aa55a3 -r 8f68ca606099 app/controllers/tweets_controller.rb --- a/app/controllers/tweets_controller.rb Sat Jun 27 14:47:28 2015 +0900 +++ b/app/controllers/tweets_controller.rb Fri Jul 03 01:17:40 2015 +0900 @@ -1,4 +1,6 @@ class TweetsController < ApplicationController + before_action :validate_id, :only => :show + def index end @@ -11,4 +13,23 @@ rescue Twitter::Error::Unauthorized head :forbidden end + + private + + def validate_id + id = params[:id].split("/")[0] + int_id = id.to_i + if int_id.to_s == id + params[:id] = int_id + else + try_redirect + end + end + + def try_redirect + user = Tweet.new(params[:id]).user + redirect_to tweet_path("#{user.id}/#{user.name}") + rescue Twitter::Error::NotFound + head :not_found + end end diff -r e01fb1aa55a3 -r 8f68ca606099 config/routes.rb --- a/config/routes.rb Sat Jun 27 14:47:28 2015 +0900 +++ b/config/routes.rb Fri Jul 03 01:17:40 2015 +0900 @@ -5,7 +5,7 @@ # You can have the root of your site routed with "root" root "tweets#index" - get "*id" => "tweets#show", :defaults => { :format => :atom } + get "*id" => "tweets#show", :defaults => { :format => :atom }, :as => "tweet" # Example of regular route: # get 'products/:id' => 'catalog#view' diff -r e01fb1aa55a3 -r 8f68ca606099 test/controllers/tweets_controller_test.rb --- a/test/controllers/tweets_controller_test.rb Sat Jun 27 14:47:28 2015 +0900 +++ b/test/controllers/tweets_controller_test.rb Fri Jul 03 01:17:40 2015 +0900 @@ -2,15 +2,20 @@ class TweetsControllerTest < ActionController::TestCase test "should get show" do - get :show, :id => "edogawa_test", :format => :atom + get :show, :id => "2791517370/edogawa_test", :format => :atom assert_response :success end test "does not explode on empty timeline" do - get :show, :id => "nanaya_t_empty", :format => :atom + get :show, :id => "3168319146/nanaya_t_empty", :format => :atom assert_response :success end + test "redirect on @handle access" do + get :show, :id => "edogawa_test", :format => :atom + assert_redirected_to "/2791517370/edogawa_test" + end + test "should get index" do get :index assert_response :success