Sha256: 17a0491d869b42d979694cb53c0c69278075d4958491d96c796268f5430f8954

Contents?: true

Size: 1.44 KB

Versions: 6

Compression:

Stored size: 1.44 KB

Contents

require_relative 'connection'

# Model for the Senec technical data.
#
# Example use:
#
# connection = Senec::Cloud::Connection.new(username: '...', password: '...')
#
# # Get the data of a specific system:
# TechnicalData[connection].find('123456')
#
# # Get the data of the default system:
# TechnicalData[connection].first
#
module Senec
  module Cloud
    class TechnicalData
      class Finder
        def initialize(connection)
          @connection = connection
        end
        attr_reader :connection

        def find(system_id)
          TechnicalData.new(connection:, system_id:).tap(&:load_data)
        end

        def first
          find(connection.default_system_id)
        end
      end

      def self.[](connection)
        Finder.new(connection)
      end

      def initialize(connection: nil, system_id: nil, data: nil)
        raise ArgumentError unless connection.nil? ^ data.nil?

        @connection = connection
        @system_id = system_id

        # Useful for testing only
        @data = data
      end

      def load_data
        raise 'Data already present!' if @data

        @system_id ||= connection.default_system_id
        @data = fetch_data
      end

      attr_reader :system_id, :data

      private

      def get(path, params: nil)
        @connection.get(path, params:)
      end

      def fetch_data
        return unless system_id

        get("/v1/senec/systems/#{system_id}/technical-data")
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
senec-0.18.0 lib/senec/cloud/technical_data.rb
senec-0.17.2 lib/senec/cloud/technical_data.rb
senec-0.17.1 lib/senec/cloud/technical_data.rb
senec-0.17.0 lib/senec/cloud/technical_data.rb
senec-0.16.0 lib/senec/cloud/technical_data.rb
senec-0.15.0 lib/senec/cloud/technical_data.rb