Class: BrainzLab::Beacon::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



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

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

Instance Method Details

#check_history(monitor_id, limit: 100) ⇒ Object

Get check history



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

def check_history(monitor_id, limit: 100)
  response = request(
    :get,
    "/api/v1/monitors/#{monitor_id}/checks",
    params: { limit: limit }
  )

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

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

#create_monitor(name:, url:, type: 'http', interval: 60, **options) ⇒ Object

Create a new monitor



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

def create_monitor(name:, url:, type: 'http', interval: 60, **options)
  response = request(
    :post,
    '/api/v1/monitors',
    body: {
      name: name,
      url: url,
      monitor_type: type,
      interval: interval,
      **options
    }
  )

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

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

#delete_monitor(id) ⇒ Object

Delete a monitor



78
79
80
81
82
83
84
# File 'lib/brainzlab/beacon/client.rb', line 78

def delete_monitor(id)
  response = request(:delete, "/api/v1/monitors/#{id}")
  response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPNoContent)
rescue StandardError => e
  log_error('delete_monitor', e)
  false
end

#get_monitor(id) ⇒ Object

Get monitor status



39
40
41
42
43
44
45
46
47
48
# File 'lib/brainzlab/beacon/client.rb', line 39

def get_monitor(id)
  response = request(:get, "/api/v1/monitors/#{id}")

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

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

#list_incidents(status: nil) ⇒ Object

List active incidents



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/brainzlab/beacon/client.rb', line 134

def list_incidents(status: nil)
  params = {}
  params[:status] = status if status

  response = request(:get, '/api/v1/incidents', params: params)

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

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

#list_monitorsObject

List all monitors



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/brainzlab/beacon/client.rb', line 51

def list_monitors
  response = request(:get, '/api/v1/monitors')

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

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

#pause_monitor(id) ⇒ Object

Pause a monitor



87
88
89
90
91
92
93
# File 'lib/brainzlab/beacon/client.rb', line 87

def pause_monitor(id)
  response = request(:post, "/api/v1/monitors/#{id}/pause")
  response.is_a?(Net::HTTPSuccess)
rescue StandardError => e
  log_error('pause_monitor', e)
  false
end

#provision(project_id:, app_name:) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/brainzlab/beacon/client.rb', line 149

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

#resume_monitor(id) ⇒ Object

Resume a monitor



96
97
98
99
100
101
102
# File 'lib/brainzlab/beacon/client.rb', line 96

def resume_monitor(id)
  response = request(:post, "/api/v1/monitors/#{id}/resume")
  response.is_a?(Net::HTTPSuccess)
rescue StandardError => e
  log_error('resume_monitor', e)
  false
end

#status_summaryObject

Get current status summary



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

def status_summary
  response = request(:get, '/api/v1/status')

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

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

#update_monitor(id, **attributes) ⇒ Object

Update a monitor



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/brainzlab/beacon/client.rb', line 64

def update_monitor(id, **attributes)
  response = request(
    :put,
    "/api/v1/monitors/#{id}",
    body: attributes
  )

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