class RenuoCmsRails::API::Contents
Constants
- MAX_CACHE_TTL
Public Class Methods
new(config)
click to toggle source
@param [RenuoCmsRails::Config] config
# File lib/renuo_cms_rails/api/contents.rb, line 7 def initialize(config) @config = config end
Public Instance Methods
cache_time()
click to toggle source
:reek: UtilityFunction
# File lib/renuo_cms_rails/api/contents.rb, line 16 def cache_time # note: using integer division for rounding, so that we get a new key every 2 minutes, e.g. # part 1: 249 / 120 = 2 (not 2.075) # part 2: 2 * 120 = 240 # all: (249 / 120) * 120 = 240 (Time.now.to_i / MAX_CACHE_TTL) * MAX_CACHE_TTL end
fetch()
click to toggle source
# File lib/renuo_cms_rails/api/contents.rb, line 24 def fetch uri = URI(url) Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| request = Net::HTTP::Get.new uri handle_response(http.request(request)) end rescue StandardError {} end
url()
click to toggle source
# File lib/renuo_cms_rails/api/contents.rb, line 11 def url "#{@config.api_host_with_protocol}/v1/#{@config.api_key}/content_blocks?_=#{cache_time}" end
Private Instance Methods
convert_json(response)
click to toggle source
:reek: UtilityFunction
# File lib/renuo_cms_rails/api/contents.rb, line 42 def convert_json(response) content_blocks_json = JSON.parse(response.body)['content_blocks'] return {} unless content_blocks_json content_blocks_json.map { |block_json| [block_json['content_path'], block_json['content']] }.to_h rescue JSON::ParserError {} end
handle_response(response)
click to toggle source
# File lib/renuo_cms_rails/api/contents.rb, line 36 def handle_response(response) return {} unless response.is_a?(Net::HTTPOK) convert_json(response) end