Sha256: 266d3b4616de6d354227d6095f33939a60ad7ae178dda60c5d60146cdc5e64e0

Contents?: true

Size: 1000 Bytes

Versions: 1

Compression:

Stored size: 1000 Bytes

Contents

module Helium
  class DataPoint < Resource
    attr_reader :timestamp, :value, :port

    def initialize(client:, params:)
      super(client: client, params: params)

      @timestamp  = params.dig("attributes", "timestamp")
      @value      = params.dig("attributes", "value")
      @port       = params.dig("attributes", "port")
    end

    def timestamp
      DateTime.parse(@timestamp)
    end

    def max
      return nil unless @value.is_a?(Hash)
      @value["max"]
    end

    def min
      return nil unless @value.is_a?(Hash)
      @value["min"]
    end

    def avg
      return nil unless @value.is_a?(Hash)
      @value["avg"]
    end

    def aggregate?
      [max, min, avg].none? { |agg_value| agg_value.nil? }
    end

    def as_json
      j = super.merge({
        timestamp: timestamp,
        port: port
      })

      if aggregate?
        j[:max] = max
        j[:min] = min
        j[:avg] = avg
      else
        j[:value] = value
      end

      j
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
helium-ruby-0.4.0 lib/helium/data_point.rb