comparison app/controllers/tweets_controller.rb @ 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 8983c426e256
children 1161ad2a4390
comparison
equal deleted inserted replaced
54:e01fb1aa55a3 55:8f68ca606099
1 class TweetsController < ApplicationController 1 class TweetsController < ApplicationController
2 before_action :validate_id, :only => :show
3
2 def index 4 def index
3 end 5 end
4 6
5 def show 7 def show
6 client = Tweet.new(params[:id]) 8 client = Tweet.new(params[:id])
9 rescue Twitter::Error::NotFound 11 rescue Twitter::Error::NotFound
10 head :not_found 12 head :not_found
11 rescue Twitter::Error::Unauthorized 13 rescue Twitter::Error::Unauthorized
12 head :forbidden 14 head :forbidden
13 end 15 end
16
17 private
18
19 def validate_id
20 id = params[:id].split("/")[0]
21 int_id = id.to_i
22 if int_id.to_s == id
23 params[:id] = int_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.name}")
32 rescue Twitter::Error::NotFound
33 head :not_found
34 end
14 end 35 end