Module: ErpIntegration::Fulfil::Persistence
- Included in:
- ApiResource
- Defined in:
- lib/erp_integration/fulfil/persistence.rb
Instance Method Summary collapse
-
#bulk_create(attributes_list) ⇒ Array(Array<Hash>, Array<String>?)
Creates several resources in Fulfil with a single HTTP request.
-
#create(attributes) ⇒ Array|Hash
Allows creating new resources in Fulfil.
-
#destroy(resource_id) ⇒ Boolean
Destroys the resource.
-
#update(resource_id, attributes) ⇒ Array|Hash
Updates the resource with the given attributes.
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`.
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, [(e)]] end |
#create(attributes) ⇒ Array|Hash
Allows creating new resources in Fulfil.
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, [(e)]] end |
#destroy(resource_id) ⇒ Boolean
Destroys the resource.
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 }, [(e)]] end |
#update(resource_id, attributes) ⇒ Array|Hash
Updates the resource with the given attributes.
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, [(e)]] end |