Module: Polyseerio::SDK::Method

Defined in:
lib/sdk/factory.rb,
lib/sdk/method/fact.rb,
lib/sdk/method/save.rb,
lib/sdk/method/check.rb,
lib/sdk/method/gauge.rb,
lib/sdk/method/index.rb,
lib/sdk/method/attach.rb,
lib/sdk/method/detach.rb,
lib/sdk/method/remove.rb,
lib/sdk/method/execute.rb,
lib/sdk/method/message.rb,
lib/sdk/method/to_json.rb,
lib/sdk/method/trigger.rb,
lib/sdk/method/add_fact.rb,
lib/sdk/method/add_gauge.rb

Overview

Instance methods.

Constant Summary collapse

MAP =
Polyseerio::Helper.dir_proc_map(File.dirname(__FILE__), self)

Class Method Summary collapse

Class Method Details

.add_factObject



5
6
7
8
9
10
11
# File 'lib/sdk/method/add_fact.rb', line 5

def self.add_fact
  proc do |instance, key, value|
    instance._facts ||= {}

    instance._facts[key] = value
  end
end

.add_gaugeObject



5
6
7
8
9
10
11
# File 'lib/sdk/method/add_gauge.rb', line 5

def self.add_gauge
  proc do |instance, key, value|
    instance._gauges ||= {}

    instance._gauges[key] = value
  end
end

.attachObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sdk/method/attach.rb', line 9

def self.attach
  proc do |instance|
    options = Polyseerio::Helper.defaults(
      {},
      delay: Constant::DEFAULT_HEARTBEAT_DELAY
    )

    uri = Helper.instance_to_uri instance

    options[:delay] = Polyseerio::Helper.ms_to_seconds options[:delay]

    Concurrent::Promise.new do
      heartbeat_thread = Thread.new(instance, options) do |inst, opts|
        begin
          loop do
            payload = {}

            Polyseerio.log 'debug', 'Sending heartbeat.'
            inst.request.post("#{uri}/heartbeat", payload).execute.value

            sleep(opts[:delay])
          end
        rescue => err
          Polyseerio.log 'error', 'Heartbeat failed.'
          Polyseerio.log 'error', err.message
        end
      end

      heartbeat_thread
    end
  end
end

.checkObject



5
6
7
8
# File 'lib/sdk/method/check.rb', line 5

def self.check
  proc do ||
  end
end

.detachObject



5
6
7
8
# File 'lib/sdk/method/detach.rb', line 5

def self.detach
  proc do ||
  end
end

.executeObject



5
6
7
8
# File 'lib/sdk/method/execute.rb', line 5

def self.execute
  proc do ||
  end
end

.factObject



5
6
7
8
# File 'lib/sdk/method/fact.rb', line 5

def self.fact
  proc do ||
  end
end

.factory(_request, _resource, methods = [], _options = {}) ⇒ Object

Generates method (instance) functions.



29
30
31
32
33
34
35
# File 'lib/sdk/factory.rb', line 29

def self.factory(_request, _resource, methods = [], _options = {})
  accumulator = Helper.accumulate_procs('method', MAP)

  methods = methods.each_with_object({}, &accumulator)

  methods
end

.gaugeObject



5
6
7
8
# File 'lib/sdk/method/gauge.rb', line 5

def self.gauge
  proc do ||
  end
end

.messageObject



5
6
7
8
# File 'lib/sdk/method/message.rb', line 5

def self.message
  proc do ||
  end
end

.removeObject



5
6
7
8
# File 'lib/sdk/method/remove.rb', line 5

def self.remove
  proc do ||
  end
end

.saveObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sdk/method/save.rb', line 7

def self.save
  proc do |instance|
    uri = Helper.instance_to_uri instance

    # TODO: need a way to make raw requests without middleware.
    # TODO: there is no reason to have to create another instance.
    if instance.new?
      instance.request.post(uri, instance.properties)
              .then do |result|
                instance.override_properties(result.properties)

                instance
              end
    else
      instance.request.put(uri, instance.properties)
              .then do |result|
                instance.override_properties(result.properties)

                instance
              end
    end
  end
end

.to_jsonObject



5
6
7
8
# File 'lib/sdk/method/to_json.rb', line 5

def self.to_json
  proc do ||
  end
end

.triggerObject



7
8
9
10
11
12
13
14
# File 'lib/sdk/method/trigger.rb', line 7

def self.trigger
  proc do |instance, payload = {}|
    # raise if the instance has not been saved yet...
    uri = Helper.instance_to_uri instance

    instance.request.post("#{uri}/trigger", payload)
  end
end