class Bodhi::ResourceBatch
Attributes
type[RW]
Public Class Methods
new(type, resources=[])
click to toggle source
Calls superclass method
Bodhi::Batch::new
# File lib/bodhi-slam/batches/resource.rb, line 10 def initialize(type, resources=[]) super(resources) @type = type end
Public Instance Methods
save!(context)
click to toggle source
Saves all records in the batch to the cloud and populates the created
and failed
arrays with the results
# File lib/bodhi-slam/batches/resource.rb, line 17 def save!(context) if context.invalid? raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s end records.each{ |record| record.validate! } response = context.connection.post do |request| request.url "/#{context.namespace}/resources/#{type}" request.headers['Content-Type'] = 'application/json' request.headers[context.credentials_header] = context.credentials request.body = records.map(&:attributes).to_json end if response.status != 200 raise Bodhi::ApiErrors.new(body: response.body, status: response.status), "status: #{response.status}, body: #{response.body}" end results = response.body.zip(records) results.each do |response, record| if response["location"] record.sys_id = response["location"].match(/(?<id>[a-zA-Z0-9]{24})/)[:id] @created.push record else @failed.push record end end end