Class: FtShared

Inherits:
Object
  • Object
show all
Defined in:
lib/ft_gem/shared/ft_shared.rb

Instance Method Summary collapse

Instance Method Details

#all_togglesObject



13
14
15
16
17
18
19
20
# File 'lib/ft_gem/shared/ft_shared.rb', line 13

def all_toggles
  Rails.cache.fetch('all-feature-toggles', expires_in: 1.minute) do
    JSON.parse(HTTParty.get("#{ENV['SITE_URL']}/service/ft/api/v1/feature-toggles", headers: authentication_header).to_json)['data']
  end
rescue StandardError => e
  Rails.logger.error "FtHelper::all_toggles error #{e.message}"
  {}
end

#authentication_headerObject



22
23
24
# File 'lib/ft_gem/shared/ft_shared.rb', line 22

def authentication_header
  { Authorization: "Bearer #{MumsnetJWT.tokenify}" }
end

#fetch(name:) ⇒ Object



2
3
4
5
6
7
8
9
10
11
# File 'lib/ft_gem/shared/ft_shared.rb', line 2

def fetch(name:)
  http_response = ''
  Rails.cache.fetch("feature-toggle-#{name}", expires_in: 1.minute) do
    http_response = HTTParty.get("#{ENV['SITE_URL']}/service/ft/api/v1/feature-toggles/status", query: { name: name }, headers: authentication_header)
    JSON.parse(http_response.to_json)['data']
  end
rescue StandardError => e
  Rails.logger.error "FtHelper::fetch error for #{name}\nerror: #{e.message}\nhttp_response: #{http_response}"
  nil
end

#whitelisted?(url:, whitelist_urls:) ⇒ Boolean

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ft_gem/shared/ft_shared.rb', line 26

def whitelisted?(url:, whitelist_urls:)
  return true if whitelist_urls.empty? || url.nil?
  whitelisted = false
  whitelist_urls.each do |whitelist_url|
    if whitelist_url[whitelist_url.length - 1] == '*'
      whitelist_url.chomp!('*')
      whitelisted = true if url.starts_with?(whitelist_url)
    elsif whitelist_url == url
      whitelisted = true
    end
  end
  whitelisted
end