Sha256: 8df5668f9799c14ae3b5b15218a4f1287ce45c4f6124ef8b76901f899440abb1

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module Floe
  class Workflow
    class Path
      class << self
        def path?(payload)
          payload.start_with?("$")
        end

        def value(payload, context, input = {})
          new(payload).value(context, input)
        end
      end

      attr_reader :payload

      def initialize(payload)
        @payload = payload

        raise Floe::InvalidWorkflowError, "Path [#{payload}] must be a string" if payload.nil? || !payload.kind_of?(String)
        raise Floe::InvalidWorkflowError, "Path [#{payload}] must start with \"$\"" if payload[0] != "$"
      end

      def value(context, input = {})
        obj, path =
          if payload.start_with?("$$")
            [context, payload[1..]]
          else
            [input, payload]
          end

        # If path is $ then just return the entire input
        return obj if path == "$"

        results = JsonPath.on(obj, path)
        case results.count
        when 0
          raise Floe::PathError, "Path [#{payload}] references an invalid value"
        when 1
          results.first
        else
          results
        end
      end

      def to_s
        payload
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
floe-0.15.1 lib/floe/workflow/path.rb
floe-0.15.0 lib/floe/workflow/path.rb
floe-0.14.0 lib/floe/workflow/path.rb
floe-0.13.1 lib/floe/workflow/path.rb
floe-0.13.0 lib/floe/workflow/path.rb