Sha256: 6c4c681901c7975e4f7621675f76ed1bf704866c00ab5349baa5e7b3148302c1

Contents?: true

Size: 1.63 KB

Versions: 21

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module Boxcars
  # used by Boxcars to return structured result and additional context
  class Observation
    attr_reader :note, :status, :added_context

    # @param note [String] The note to use for the result
    # @param status [Symbol] :ok or :error
    # @param added_context [Hash] Any additional context to add to the result
    def initialize(note:, status: :ok, **added_context)
      @note = note
      @status = status
      @added_context = added_context
    end

    # @return [Hash] The result as a hash
    def to_h
      {
        note: note,
        status: status
      }.merge(added_context).compact
    end

    # @return [String] The result as a json string
    def to_json(*args)
      JSON.generate(to_h, *args)
    end

    # @return [String] An explanation of the result
    def to_s
      note.to_s
    end

    # @return [String] An explanation of the result
    def to_text
      to_s
    end

    # create a new Observaton from a text string with a status of :ok
    # @param note [String] The text to use for the observation
    # @param added_context [Hash] Any additional context to add to the result
    # @return [Boxcars::Observation] The observation
    def self.ok(note, **kwargs)
      new(note: note, status: :ok, **kwargs)
    end

    # create a new Observaton from a text string with a status of :error
    # @param note [String] The text to use for the observation
    # @param added_context [Hash] Any additional context to add to the result
    # @return [Boxcars::Observation] The observation
    def self.err(note, **kwargs)
      new(note: note, status: :error, **kwargs)
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
boxcars-0.6.6 lib/boxcars/observation.rb
boxcars-0.6.5 lib/boxcars/observation.rb
boxcars-0.6.4 lib/boxcars/observation.rb
boxcars-0.6.3 lib/boxcars/observation.rb
boxcars-0.6.2 lib/boxcars/observation.rb
boxcars-0.6.1 lib/boxcars/observation.rb
boxcars-0.5.1 lib/boxcars/observation.rb
boxcars-0.4.10 lib/boxcars/observation.rb
boxcars-0.4.9 lib/boxcars/observation.rb
boxcars-0.4.8 lib/boxcars/observation.rb
boxcars-0.4.7 lib/boxcars/observation.rb
boxcars-0.4.6 lib/boxcars/observation.rb
boxcars-0.4.5 lib/boxcars/observation.rb
boxcars-0.4.4 lib/boxcars/observation.rb
boxcars-0.4.3 lib/boxcars/observation.rb
boxcars-0.4.2 lib/boxcars/observation.rb
boxcars-0.4.1 lib/boxcars/observation.rb
boxcars-0.4.0 lib/boxcars/observation.rb
boxcars-0.3.5 lib/boxcars/observation.rb
boxcars-0.3.4 lib/boxcars/observation.rb