Sha256: 6feda214d0ac9debc2f69594f72f464018e6a6e665d9b9a504961d3e58aa12a1

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

module RenuoCmsRails
  module API
    class Contents
      MAX_CACHE_TTL = 60 * 2 # 2 minutes

      # @param [RenuoCmsRails::Config] config
      def initialize(config)
        @config = config
      end

      def url
        "#{@config.api_host_with_protocol}/v1/#{@config.api_key}/content_blocks?_=#{cache_time}"
      end

      # :reek:UtilityFunction
      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

      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
      end

      private

      def handle_response(response)
        return {} unless response.is_a?(Net::HTTPOK)
        convert_json(response)
      end

      # :reek:UtilityFunction
      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
renuo-cms-rails-0.2.0 lib/renuo_cms_rails/api/contents.rb