Sha256: 61633914ee05cc43a6ab7a8767e23793c6a18acb96c2a8bb1d9f8a8fa87dba58

Contents?: true

Size: 1.87 KB

Versions: 35

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

module ErpIntegration
  module Fulfil
    # The `Query` class transforms where clauses and included fields into a
    # queryable object that Fulfil understands.
    #
    # @example
    #   $ Query.new([:id, 'product.id'], [['id', '=', '100']]).to_json
    #   # => {"fields":["id","product.id"],"filters":[["id","=","100"]]}
    #
    # The instance of the `Query` class is meant to be passed to `Faraday` or
    # a third-party vendor client (e.g. `Fulfil::Client`) that uses `Faraday`.
    #
    # `Faraday` will call the `.to_json` method before sending the data to Fulfil.
    # So, there is no need to explicitly call `.to_json` on the `Query` instance.
    #
    # @example
    #   Faraday.post("/api-endpoint", Query.new([:id, 'product.id'], [WhereClause.new(key: :id, value: 100)]))
    class Query
      delegate :to_json, to: :to_h

      attr_reader :fields, :filters, :offset, :limit

      DEFAULT_FIELDS = %w[id].freeze

      # @param fields [Array<String|Symbol>] A list of fields for Fulfil.
      # @param filters [Array<WhereClause>] A list of where clauses for Fulfil.
      # @param alternative_filters [Array<OrClause>] A list of or clauses for Fulfil.
      # @param offset [Integer] Offset for pagination.
      # @param limit [Integer] Limit for pagination.
      def initialize(fields:, filters:, alternative_filters: [], offset: nil, limit: nil)
        @fields = (fields || DEFAULT_FIELDS).map(&:to_s).uniq
        @filters = (filters || []).map(&:to_filter).uniq
        @filters += alternative_filters.map(&:to_filter).uniq if alternative_filters&.any?
        @offset = offset
        @limit = limit
      end

      def to_h
        {}.tap do |hash|
          hash[:fields] = @fields
          hash[:filters] = @filters
          hash[:limit] = @limit if @limit
          hash[:offset] = @offset if @offset
        end
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
erp_integration-0.57.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.56.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.55.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.54.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.53.1 lib/erp_integration/fulfil/query.rb
erp_integration-0.53.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.52.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.50.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.49.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.48.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.43.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.42.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.41.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.40.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.39.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.38.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.37.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.36.1 lib/erp_integration/fulfil/query.rb
erp_integration-0.36.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.35.0 lib/erp_integration/fulfil/query.rb