Class: ErpIntegration::Fulfil::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/erp_integration/fulfil/query.rb

Overview

The ‘Query` class transforms where clauses and included fields into a queryable object that Fulfil understands.

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.

Examples:

$ Query.new([:id, 'product.id'], [['id', '=', '100']]).to_json
# => {"fields":["id","product.id"],"filters":[["id","=","100"]]}
Faraday.post("/api-endpoint", Query.new([:id, 'product.id'], [WhereClause.new(key: :id, value: 100)]))

Constant Summary collapse

DEFAULT_FIELDS =
%w[id].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields:, filters:, alternative_filters: [], offset: nil, limit: nil) ⇒ Query

Returns a new instance of Query.

Parameters:

  • fields (Array<String|Symbol>)

    A list of fields for Fulfil.

  • filters (Array<WhereClause>)

    A list of where clauses for Fulfil.

  • alternative_filters (Array<OrClause>) (defaults to: [])

    A list of or clauses for Fulfil.

  • offset (Integer) (defaults to: nil)

    Offset for pagination.

  • limit (Integer) (defaults to: nil)

    Limit for pagination.



32
33
34
35
36
37
38
# File 'lib/erp_integration/fulfil/query.rb', line 32

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

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



23
24
25
# File 'lib/erp_integration/fulfil/query.rb', line 23

def fields
  @fields
end

#filtersObject (readonly)

Returns the value of attribute filters.



23
24
25
# File 'lib/erp_integration/fulfil/query.rb', line 23

def filters
  @filters
end

#limitObject (readonly)

Returns the value of attribute limit.



23
24
25
# File 'lib/erp_integration/fulfil/query.rb', line 23

def limit
  @limit
end

#offsetObject (readonly)

Returns the value of attribute offset.



23
24
25
# File 'lib/erp_integration/fulfil/query.rb', line 23

def offset
  @offset
end

Instance Method Details

#to_hObject



40
41
42
43
44
45
46
47
# File 'lib/erp_integration/fulfil/query.rb', line 40

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