view app/controllers/tweets_controller.rb @ 66:1161ad2a4390

Take care of rt removal from gem api Double the default count since filtering is done at gem level, not api.
author nanaya <me@myconan.net>
date Tue, 01 Dec 2015 01:34:12 +0900
parents 8f68ca606099
children 5dd24a65547b
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 :exclude_replies => true, :count => 40
    @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.name}")
  rescue Twitter::Error::NotFound
    head :not_found
  end
end