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



20
21
22
23
24
25
26
27
# File 'lib/ft_gem/shared/ft_shared.rb', line 20

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

#fetch(name:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/ft_gem/shared/ft_shared.rb', line 9

def fetch(name:)
  http_response = ''
  Rails.cache.fetch("feature-toggle-#{name}", expires_in: 1.minute) do
    http_response = HTTParty.get("#{svc_url}/service/ft/api/v1/feature-toggles/status", query: { name: name }, headers: header_hash)
    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

#header_hashObject



29
30
31
# File 'lib/ft_gem/shared/ft_shared.rb', line 29

def header_hash
  { Authorization: "Bearer #{MumsnetJWT.tokenify}", Host: "#{ENV['SITE_HOSTNAME']}" }
end

#svc_urlObject



3
4
5
6
7
# File 'lib/ft_gem/shared/ft_shared.rb', line 3

def svc_url
    url = ENV['MICROSVC_URL']
    url = ENV['SITE_URL'] unless url
    url
end

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

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ft_gem/shared/ft_shared.rb', line 33

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