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

#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



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

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