changeset 55:8f68ca606099

Add twitter id to the url so it'll be consistent. Not sure if there will be use case of "following this id".
author nanaya <me@myconan.net>
date Fri, 03 Jul 2015 01:17:40 +0900
parents e01fb1aa55a3
children 598b7bcb1663
files app/controllers/tweets_controller.rb config/routes.rb test/controllers/tweets_controller_test.rb
diffstat 3 files changed, 29 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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'
--- 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