Class: RenuoCmsRails::API::Contents

Inherits:
Object
  • Object
show all
Defined in:
lib/renuo_cms_rails/api/contents.rb

Constant Summary collapse

MAX_CACHE_TTL =

2 minutes

60 * 2

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Contents

Returns a new instance of Contents.

Parameters:



7
8
9
# File 'lib/renuo_cms_rails/api/contents.rb', line 7

def initialize(config)
  @config = config
end

Instance Method Details

#cache_timeObject

:reek:UtilityFunction



16
17
18
19
20
21
22
# 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

#fetchObject



24
25
26
27
28
29
30
31
32
# 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

#urlObject



11
12
13
# 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