view app/controllers/tweets_controller.rb @ 86:5bfc986200db

The caching becomes a bit confusing because of parameters
author nanaya <me@myconan.net>
date Mon, 27 Jun 2016 05:14:10 +0900
parents fe5ea9b86091
children b01b5265e6d1
line wrap: on
line source

class TweetsController < ApplicationController
  before_action :validate_id, :only => :show

  def index
  end

  def show
    client = Tweet.new(params[:id])
    @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]
    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.screen_name}")
  rescue Twitter::Error::NotFound
    head :not_found
  end
end