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