Class: BrainzLab::Cortex::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/brainzlab/cortex/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



11
12
13
14
# File 'lib/brainzlab/cortex/client.rb', line 11

def initialize(config)
  @config = config
  @base_url = config.cortex_url || 'https://cortex.brainzlab.ai'
end

Instance Method Details

#create_flag(attrs) ⇒ Object

Create (push) a flag definition. Returns the created flag hash or nil.



79
80
81
82
83
84
85
86
87
88
# File 'lib/brainzlab/cortex/client.rb', line 79

def create_flag(attrs)
  response = request(:post, '/api/v1/flags', body: { flag: attrs })

  return nil unless created_or_ok?(response)

  JSON.parse(response.body, symbolize_names: true)[:flag]
rescue StandardError => e
  log_error('create_flag', e)
  nil
end

#evaluate(flag_name, context: {}) ⇒ Object

Evaluate a single flag



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/brainzlab/cortex/client.rb', line 17

def evaluate(flag_name, context: {})
  response = request(
    :post,
    '/api/v1/evaluate',
    body: {
      flag: flag_name,
      context: context
    }
  )

  return nil unless response.is_a?(Net::HTTPSuccess)

  data = JSON.parse(response.body, symbolize_names: true)
  data[:result]
rescue StandardError => e
  log_error('evaluate', e)
  nil
end

#evaluate_all(context: {}) ⇒ Object

Evaluate multiple flags at once



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/brainzlab/cortex/client.rb', line 37

def evaluate_all(context: {})
  response = request(
    :post,
    '/api/v1/evaluate/batch',
    body: { context: context }
  )

  return {} unless response.is_a?(Net::HTTPSuccess)

  data = JSON.parse(response.body, symbolize_names: true)
  data[:flags] || {}
rescue StandardError => e
  log_error('evaluate_all', e)
  {}
end

#get_flag(flag_name) ⇒ Object

Get flag details



67
68
69
70
71
72
73
74
75
76
# File 'lib/brainzlab/cortex/client.rb', line 67

def get_flag(flag_name)
  response = request(:get, "/api/v1/flags/#{CGI.escape(flag_name.to_s)}")

  return nil unless response.is_a?(Net::HTTPSuccess)

  JSON.parse(response.body, symbolize_names: true)
rescue StandardError => e
  log_error('get_flag', e)
  nil
end

#listObject

List all flags



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/brainzlab/cortex/client.rb', line 54

def list
  response = request(:get, '/api/v1/flags')

  return [] unless response.is_a?(Net::HTTPSuccess)

  data = JSON.parse(response.body, symbolize_names: true)
  data[:flags] || []
rescue StandardError => e
  log_error('list', e)
  []
end

#provision(project_id:, app_name:) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/brainzlab/cortex/client.rb', line 122

def provision(project_id:, app_name:)
  response = request(
    :post,
    '/api/v1/projects/provision',
    body: { project_id: project_id, app_name: app_name },
    use_service_key: true
  )

  response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPCreated)
rescue StandardError => e
  log_error('provision', e)
  false
end

#set_percentage(flag_name, percentage:, environment:) ⇒ Object

Set a percentage flag’s rollout in an environment. Returns the new state or nil.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/brainzlab/cortex/client.rb', line 107

def set_percentage(flag_name, percentage:, environment:)
  response = request(
    :post,
    "/api/v1/flags/#{CGI.escape(flag_name)}/set_percentage",
    body: { percentage: percentage, environment: environment }
  )

  return nil unless created_or_ok?(response)

  JSON.parse(response.body, symbolize_names: true)
rescue StandardError => e
  log_error('set_percentage', e)
  nil
end

#toggle(flag_name, enabled:, environment:) ⇒ Object

Set a flag’s enabled state in an environment. Returns the new state or nil.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/brainzlab/cortex/client.rb', line 91

def toggle(flag_name, enabled:, environment:)
  response = request(
    :post,
    "/api/v1/flags/#{CGI.escape(flag_name)}/toggle",
    body: { enabled: enabled, environment: environment }
  )

  return nil unless created_or_ok?(response)

  JSON.parse(response.body, symbolize_names: true)
rescue StandardError => e
  log_error('toggle', e)
  nil
end