Module: ErpIntegration::Fulfil::Persistence

Included in:
ApiResource
Defined in:
lib/erp_integration/fulfil/persistence.rb

Instance Method Summary collapse

Instance Method Details

#bulk_create(attributes_list) ⇒ Array(Array<Hash>, Array<String>?)

Creates several resources in Fulfil with a single HTTP request.

Fulfil’s bulk endpoint is atomic: if any record fails validation, the whole batch is rejected with a single error message. There is no per-record error breakdown. Callers needing per-record errors should validate inputs client-side or fall back to per-record ‘#create`.

Parameters:

  • attributes_list (Array<Hash>)

    One hash per resource to create.

Returns:

  • (Array(Array<Hash>, Array<String>?))

    A tuple of the input attributes (with ‘:id` merged in on success) and an array of error messages (nil on success).



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/erp_integration/fulfil/persistence.rb', line 30

def bulk_create(attributes_list)
  normalized = normalize_attributes(attributes_list)
  ids = client.post("model/#{model_name}", normalized)

  unless ids.size == normalized.size
    raise ErpIntegration::Error, "Fulfil returned #{ids.size} ids for #{normalized.size} records"
  end

  ids.each_with_index { |id, i| normalized[i][:id] = id }
  [normalized, nil]
rescue ErpIntegration::HttpError::BadRequest => e
  [normalized, [extract_error_message(e)]]
end

#create(attributes) ⇒ Array|Hash

Allows creating new resources in Fulfil.

Parameters:

  • attributes (Hash)

    A list of attributes for the new resource.

Returns:

  • (Array|Hash)

    The response from the API



10
11
12
13
14
15
16
17
# File 'lib/erp_integration/fulfil/persistence.rb', line 10

def create(attributes)
  client
    .post("model/#{model_name}", normalize_attributes(attributes))
    .map { |new_record_id| attributes.merge!(id: new_record_id) }
    .first
rescue ErpIntegration::HttpError::BadRequest => e
  [attributes, [extract_error_message(e)]]
end

#destroy(resource_id) ⇒ Boolean

Destroys the resource.

Parameters:

  • resource_id (Integer)

    The ID of the resource.

Returns:

  • (Boolean)

    Returns true if the resource was deleted



59
60
61
62
63
64
# File 'lib/erp_integration/fulfil/persistence.rb', line 59

def destroy(resource_id)
  client.delete("model/#{model_name}/#{resource_id}")
  { id: resource_id }
rescue ErpIntegration::HttpError::BadRequest => e
  [{ id: resource_id }, [extract_error_message(e)]]
end

#update(resource_id, attributes) ⇒ Array|Hash

Updates the resource with the given attributes.

Parameters:

  • resource_id (Integer)

    The ID of the resource.

  • attributes (Hash)

    A list of attributes to update for the resource.

Returns:

  • (Array|Hash)

    The response from the API



49
50
51
52
53
# File 'lib/erp_integration/fulfil/persistence.rb', line 49

def update(resource_id, attributes)
  client.put("model/#{model_name}/#{resource_id}", attributes)
rescue ErpIntegration::HttpError::BadRequest => e
  [attributes, [extract_error_message(e)]]
end