2020-04-02 12:57:04 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Api::V1::GabTrendsController < Api::BaseController
|
|
|
|
respond_to :json
|
|
|
|
|
|
|
|
skip_before_action :set_cache_headers
|
|
|
|
|
|
|
|
def index
|
2020-06-08 22:10:51 -04:00
|
|
|
type = params[:type]
|
|
|
|
if type == 'feed'
|
2020-07-06 15:15:28 -05:00
|
|
|
body = Redis.current.get("gabtrends:feed")
|
2020-06-08 22:10:51 -04:00
|
|
|
|
2020-07-08 17:41:48 -05:00
|
|
|
if body.nil? || body.empty?
|
2020-07-07 16:01:51 -05:00
|
|
|
Request.new(:get, "https://trends.gab.com/trend-feed/json").perform do |res|
|
2020-07-08 18:35:42 -05:00
|
|
|
Rails.logger.debug "GabTrendsController: #{type} endpoint res code: #{res.code.to_s}"
|
2020-07-07 16:01:51 -05:00
|
|
|
if res.code == 200
|
|
|
|
body = res.body_with_limit
|
|
|
|
Redis.current.set("gabtrends:feed", body)
|
|
|
|
Redis.current.expire("gabtrends:feed", 1.hour.seconds)
|
2020-07-08 18:35:42 -05:00
|
|
|
render json: body
|
2020-07-07 16:01:51 -05:00
|
|
|
else
|
2020-07-08 18:35:42 -05:00
|
|
|
render json: nil
|
2020-07-07 16:01:51 -05:00
|
|
|
end
|
2020-06-08 22:10:51 -04:00
|
|
|
end
|
2020-07-08 18:35:42 -05:00
|
|
|
else
|
|
|
|
render json: body
|
2020-04-03 19:18:26 -04:00
|
|
|
end
|
2020-07-01 22:39:43 -04:00
|
|
|
elsif type == 'partner'
|
|
|
|
body = Redis.current.get("gabtrends:partner")
|
|
|
|
|
2020-07-08 17:41:48 -05:00
|
|
|
if body.nil? || body.empty?
|
2020-07-07 16:01:51 -05:00
|
|
|
Request.new(:get, "https://trends.gab.com/partner").perform do |res|
|
2020-07-08 18:35:42 -05:00
|
|
|
Rails.logger.debug "GabTrendsController: #{type} endpoint res code: #{res.code.to_s}"
|
2020-07-07 16:01:51 -05:00
|
|
|
if res.code == 200
|
|
|
|
body = res.body_with_limit
|
|
|
|
Redis.current.set("gabtrends:partner", body)
|
2020-07-08 17:41:48 -05:00
|
|
|
Redis.current.expire("gabtrends:partner", 1.minute.seconds)
|
2020-07-08 18:35:42 -05:00
|
|
|
render json: body
|
2020-07-07 16:01:51 -05:00
|
|
|
else
|
2020-07-08 18:35:42 -05:00
|
|
|
render json: nil
|
2020-07-07 16:01:51 -05:00
|
|
|
end
|
2020-07-01 22:39:43 -04:00
|
|
|
end
|
2020-07-08 18:35:42 -05:00
|
|
|
else
|
|
|
|
render json: body
|
2020-07-01 22:39:43 -04:00
|
|
|
end
|
2020-06-08 22:10:51 -04:00
|
|
|
else
|
|
|
|
raise GabSocial::NotPermittedError
|
|
|
|
end
|
2020-07-08 18:35:42 -05:00
|
|
|
|
|
|
|
rescue HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError, HTTP::Error
|
|
|
|
Rails.logger.debug "Error fetching gabtrends feed: #{type}"
|
|
|
|
render json: nil
|
2020-04-02 12:57:04 -04:00
|
|
|
end
|
2020-06-08 22:10:51 -04:00
|
|
|
|
2020-04-02 12:57:04 -04:00
|
|
|
end
|