Sha256: 585fa0b900cf814ecd67ca3e9f94e5518a008a9bf0822fed1c3a799c1f71aeb9

Contents?: true

Size: 1.34 KB

Versions: 22

Compression:

Stored size: 1.34 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
      attr_reader :fields, :filters
      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.
      def initialize(fields, filters)
        @fields = (fields || DEFAULT_FIELDS).map(&:to_s).uniq
        @filters = (filters || []).map(&:to_filter).uniq
      end

      def to_json(*_object)
        {
          fields: @fields,
          filters: @filters
        }.to_json
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
erp_integration-0.16.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.15.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.14.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.13.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.12.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.11.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.10.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.9.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.8.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.7.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.6.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.5.5 lib/erp_integration/fulfil/query.rb
erp_integration-0.5.4 lib/erp_integration/fulfil/query.rb
erp_integration-0.5.3 lib/erp_integration/fulfil/query.rb
erp_integration-0.5.2 lib/erp_integration/fulfil/query.rb
erp_integration-0.5.1 lib/erp_integration/fulfil/query.rb
erp_integration-0.5.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.4.0 lib/erp_integration/fulfil/query.rb
erp_integration-0.3.3 lib/erp_integration/fulfil/query.rb
erp_integration-0.3.2 lib/erp_integration/fulfil/query.rb