view app/controllers/tweets_controller.rb @ 164:59a4645fd24c

Correct way to check all numbers?
author nanaya <me@nanaya.pro>
date Fri, 03 Aug 2018 02:12:54 +0900
parents 3ff631612493
children 5af9b537db86
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]
    if id[/\D/].nil?
      params[:id] = 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