Class: LaunchdarklyApiHelperClass

Inherits:
Object
  • Object
show all
Defined in:
lib/launchdarkly_api_helper/launchdarkly_api_helper_class.rb

Overview

All methods related to launch darkly api are defined here

Instance Method Summary collapse

Constructor Details

#initialize(access_token, project_name, log_file) ⇒ LaunchdarklyApiHelperClass

Returns a new instance of LaunchdarklyApiHelperClass.



12
13
14
15
16
# File 'lib/launchdarkly_api_helper/launchdarkly_api_helper_class.rb', line 12

def initialize(access_token, project_name, log_file)
  @access_token = access_token
  @launch_darkly_flags = 'https://app.launchdarkly.com/api/v2/flags/project_name'.gsub 'project_name', project_name
  @logger = Logger.new(log_file)
end

Instance Method Details

#add_values_to_clause(env, flag, clause_name, clause_value) ⇒ Object



139
140
141
142
143
144
# File 'lib/launchdarkly_api_helper/launchdarkly_api_helper_class.rb', line 139

def add_values_to_clause(env, flag, clause_name, clause_value)
  rule_at_index, clause_at_index = rules_clauses_index(env, flag, clause_name)
  request_url = "#{@launch_darkly_flags}/#{flag}"
  request_body = { 'op' => 'add', 'path' => "/environments/#{env}/rules/#{rule_at_index}/clauses/#{clause_at_index}/values/0", 'value' => clause_value }
  ld_request(:patch, request_url, [request_body])['environments'][env]['rules'][rule_at_index]['clauses'][clause_at_index]['values']
end

#convert_to_json(hash) ⇒ Object



22
23
24
# File 'lib/launchdarkly_api_helper/launchdarkly_api_helper_class.rb', line 22

def convert_to_json(hash)
  JSON.dump(hash)
end

#create_flag(key, name, description, tags) ⇒ Object



57
58
59
60
61
62
# File 'lib/launchdarkly_api_helper/launchdarkly_api_helper_class.rb', line 57

def create_flag(key, name, description, tags)
  request_url = @launch_darkly_flags
  request_body = {}
  request_body.merge!(key: key, name: name, description: description, tags: tags)
  ld_request(:post, request_url, request_body)
end

#delete_flag(flag) ⇒ Object



154
155
156
157
# File 'lib/launchdarkly_api_helper/launchdarkly_api_helper_class.rb', line 154

def delete_flag(flag)
  request_url = "#{@launch_darkly_flags}/#{flag}"
  ld_request(:delete, request_url)
end

#feature_flag_variation_index(status_response, details_response) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/launchdarkly_api_helper/launchdarkly_api_helper_class.rb', line 71

def feature_flag_variation_index(status_response, details_response)
  variations = details_response['variations']
  value_at_index = -1
  variations.length.times do |index|
    next unless variations[index]['value'].eql? status_response

    value_at_index = index
    break
  end
  value_at_index
end

#feature_flag_variation_name(details_response, index_response) ⇒ Object



87
88
89
# File 'lib/launchdarkly_api_helper/launchdarkly_api_helper_class.rb', line 87

def feature_flag_variation_name(details_response, index_response)
  details_response['variations'][index_response]['name']
end

#feature_flag_variation_value(details_response, index_response) ⇒ Object



83
84
85
# File 'lib/launchdarkly_api_helper/launchdarkly_api_helper_class.rb', line 83

def feature_flag_variation_value(details_response, index_response)
  details_response['variations'][index_response]['value']
end

#fetch_flag_details(env, flag) ⇒ Object



47
48
49
50
# File 'lib/launchdarkly_api_helper/launchdarkly_api_helper_class.rb', line 47

def fetch_flag_details(env, flag)
  request_url = "#{@launch_darkly_flags}/#{flag}?env=#{env}"
  ld_request(:get, request_url)
end

#fetch_flag_toggle_status(env, flag) ⇒ Object



52
53
54
55
# File 'lib/launchdarkly_api_helper/launchdarkly_api_helper_class.rb', line 52

def fetch_flag_toggle_status(env, flag)
  flag_details_response = fetch_flag_details(env, flag)
  flag_details_response['environments'][env]['on']
end

#flag_variation_served(env, flag) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/launchdarkly_api_helper/launchdarkly_api_helper_class.rb', line 91

def flag_variation_served(env, flag)
  details_response = fetch_flag_details(env, flag)
  toggle_status_response = fetch_flag_toggle_status(env, flag)
  variation_index_response = feature_flag_variation_index(toggle_status_response, details_response) # ['environments'][env]['fallthrough']['variation']
  variation_value_response = feature_flag_variation_value(details_response, variation_index_response) # ['variations'][variation_index_response]['value']
  variation_name_response = feature_flag_variation_name(details_response, variation_index_response)
  [toggle_status_response, variation_index_response, variation_value_response, variation_name_response]
end

#get_values_from_clauses(env, flag, clause_name) ⇒ Object



134
135
136
137
# File 'lib/launchdarkly_api_helper/launchdarkly_api_helper_class.rb', line 134

def get_values_from_clauses(env, flag, clause_name)
  rule_at_index, clause_at_index = rules_clauses_index(env, flag, clause_name)
  @feature_flag_rules_list[rule_at_index]['clauses'][clause_at_index]['values']
end

#ld_request(http_method, request_url, request_body = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/launchdarkly_api_helper/launchdarkly_api_helper_class.rb', line 26

def ld_request(http_method, request_url, request_body = nil)
  url = URI(request_url)
  https = Net::HTTP.new(url.host, url.port)
  https.use_ssl = true
  request = REQUEST_CLASSES[http_method]['method'].new(url)
  request['Authorization'] = @access_token
  request['Content-Type'] = 'application/json'
  request['LD-API-Version'] = 'beta'
  case http_method
  when :get, :patch, :post
    request.body = convert_to_json(request_body) unless http_method == :get
    https.request(request)
  when :delete
    return https.request(request)
  else
    raise StandardError, "Undefined HTTP method #{http_method} found"
  end
  response = https.request(request)
  @response = parse_json(response.read_body)
end

#parse_json(json) ⇒ Object



18
19
20
# File 'lib/launchdarkly_api_helper/launchdarkly_api_helper_class.rb', line 18

def parse_json(json)
  JSON.parse(json)
end

#remove_values_from_clause(env, flag, clause_name, clause_value) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/launchdarkly_api_helper/launchdarkly_api_helper_class.rb', line 146

def remove_values_from_clause(env, flag, clause_name, clause_value)
  rule_at_index, clause_at_index = rules_clauses_index(env, flag, clause_name)
  value_at_index = search_value_index(rule_at_index, clause_at_index, clause_value)
  request_url = "#{@launch_darkly_flags}/#{flag}"
  request_body = [{ 'op' => 'remove', 'path' => "/environments/#{env}/rules/#{rule_at_index}/clauses/#{clause_at_index}/values/#{value_at_index}" }]
  ld_request(:patch, request_url, request_body)['environments'][env]['rules'][rule_at_index]['clauses'][clause_at_index]['values']
end

#rules_clauses_index(env, flag, clause_name) ⇒ Object



128
129
130
131
132
# File 'lib/launchdarkly_api_helper/launchdarkly_api_helper_class.rb', line 128

def rules_clauses_index(env, flag, clause_name)
  feature_flag_response = fetch_flag_details(env, flag)
  @feature_flag_rules_list = feature_flag_response['environments'][env]['rules']
  search_rule_index_clause_index(clause_name)
end

#search_rule_index_clause_index(clause_name) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/launchdarkly_api_helper/launchdarkly_api_helper_class.rb', line 100

def search_rule_index_clause_index(clause_name)
  rule_at_index = -1
  clause_at_index = -1
  @feature_flag_rules_list.length.times do |rule_index|
    @feature_flag_clauses_list = @feature_flag_rules_list[rule_index]['clauses']
    @feature_flag_clauses_list.length.times do |clause_index|
      next unless @feature_flag_clauses_list[clause_index]['attribute'].eql? clause_name

      rule_at_index = rule_index
      clause_at_index = clause_index
      break
    end
  end
  [rule_at_index, clause_at_index]
end

#search_value_index(rule_at_index, clause_at_index, clause_value) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/launchdarkly_api_helper/launchdarkly_api_helper_class.rb', line 116

def search_value_index(rule_at_index, clause_at_index, clause_value)
  value_at_index = -1
  @feature_flag_values_list = @feature_flag_rules_list[rule_at_index]['clauses'][clause_at_index]['values']
  @feature_flag_values_list.length.times do |value_index|
    next unless @feature_flag_values_list[value_index].eql? clause_value

    value_at_index = value_index
    break
  end
  value_at_index
end

#toggle_specific_environment(env, flag, flag_value) ⇒ Object



64
65
66
67
68
69
# File 'lib/launchdarkly_api_helper/launchdarkly_api_helper_class.rb', line 64

def toggle_specific_environment(env, flag, flag_value)
  request_url = "#{@launch_darkly_flags}/#{flag}"
  request_body = { 'op' => 'replace', 'path' => "/environments/#{env}/on", 'value' => flag_value }
  response_body = ld_request(:patch, request_url, [request_body])
  response_body['environments'][env]['on']
end