view app/controllers/tweets_controller.rb @ 157:6e6051cd9cca

Or maybe not
author nanaya <me@nanaya.pro>
date Sat, 28 Jul 2018 03:42:23 +0900
parents 32c8c150cd80
children 3ff631612493
line wrap: on
line source

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

  def index
    return try_redirect if params[:id]
  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
  rescue Twitter::Error::Unauthorized
    head :forbidden
  end
end