Class: UppityRobot::Client

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

Overview

UppityRobot::Client is a CLI focused wrapper for the UptimeRobot::Client

Constant Summary collapse

RECORD_TYPES =
{
  getAlertContacts: "alert_contacts",
  getMonitors: "monitors"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task, params) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
# File 'lib/uppityrobot/client.rb', line 13

def initialize(task, params)
  @client = client
  @task = verify_task(task)
  @params = params
  @response = nil
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



6
7
8
# File 'lib/uppityrobot/client.rb', line 6

def response
  @response
end

Instance Method Details

#executeObject



20
21
22
# File 'lib/uppityrobot/client.rb', line 20

def execute
  @response = client.send(@task, @params)
end

#fetchObject

use when a single record result is expected & required



59
60
61
62
63
64
65
# File 'lib/uppityrobot/client.rb', line 59

def fetch
  verify_fetch_params
  # TODO: support alert_contacts search (requires filter)
  response = execute
  verify_fetch_total response
  response[RECORD_TYPES[@task]].first
end

#filter(filter = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/uppityrobot/client.rb', line 24

def filter(filter = {})
  type = RECORD_TYPES[@task]
  Enumerator.new do |yielder|
    paginate.each do |response, _offset, _total|
      response[type].find_all do |record|
        satisfies_conditions = true
        filter.each do |key, regex|
          satisfies_conditions = false unless record.key?(key) && record[key].to_s =~ /#{regex}/
        end
        yielder << record if satisfies_conditions
      end
    end
  end.lazy
end

#paginateObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/uppityrobot/client.rb', line 39

def paginate
  Enumerator.new do |yielder|
    @params.delete :offset # we want to start from the beginning
    response = execute # initial connection
    offset = 0
    limit = response["pagination"]["limit"]
    total = response["pagination"]["total"]

    loop do
      raise StopIteration if offset >= total

      @params = @params.merge({offset: offset})
      response = execute
      yielder << [response, offset, total]
      offset += limit
    end
  end.lazy
end