2020-07-02 02:33:10 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-11-09 18:08:31 +00:00
|
|
|
class Api::V1::ShopController < EmptyController
|
2020-07-02 02:33:10 +01:00
|
|
|
def index
|
2020-11-09 18:08:31 +00:00
|
|
|
if Rails.env != 'development'
|
|
|
|
render json: nil
|
|
|
|
end
|
|
|
|
|
2020-07-02 02:33:10 +01:00
|
|
|
type = params[:type]
|
|
|
|
if type == 'featured_products'
|
|
|
|
body = Redis.current.get("gabstore:featuredproducts")
|
|
|
|
|
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://shop.dissenter.com/product/group/json").perform do |res|
|
2020-07-09 00:35:42 +01:00
|
|
|
Rails.logger.debug "ShopController dissenter products 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("gabstore:featuredproducts", body)
|
|
|
|
Redis.current.expire("gabstore:featuredproducts", 15.minutes.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 02:33:10 +01:00
|
|
|
end
|
2020-07-09 00:35:42 +01:00
|
|
|
else
|
|
|
|
render json: body
|
2020-07-02 02:33:10 +01:00
|
|
|
end
|
|
|
|
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 dissenter shop: #{type}"
|
|
|
|
render json: nil
|
2020-07-02 02:33:10 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|