Module: ErpIntegration::Fulfil::Persistence

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

Instance Method Summary collapse

Instance Method Details

#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



34
35
36
37
38
39
# File 'lib/erp_integration/fulfil/persistence.rb', line 34

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



24
25
26
27
28
# File 'lib/erp_integration/fulfil/persistence.rb', line 24

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