lib/ecoportal/api/common/batch_operation.rb in ecoportal-api-0.9.7 vs lib/ecoportal/api/common/batch_operation.rb in ecoportal-api-0.10.0
- old
+ new
@@ -26,74 +26,77 @@
end
log(:info) { "Processing batch responses" }
body_data(response.body).each.with_index do |subresponse, idx|
- callback = @operations[idx][:callback]
+ byebug
status = subresponse["status"]
- body = subresponse["response"]
method = @operations[idx][:method]
+ body = subresponse["response"]
+ callback = @operations[idx][:callback]
if status == 200 && method == "GET"
batch_response = BatchResponse.new(status, body, @wrapper.new(body))
log_batch_response(@operations[idx], batch_response)
- callback && callback.call(batch_response, batch_response.result)
+
+ callback&.call(batch_response, batch_response.result)
else
batch_response = BatchResponse.new(status, body)
log_batch_response(@operations[idx], batch_response)
- callback && callback.call(batch_response)
+
+ callback&.call(batch_response)
end
end
end
def get(doc, &block)
id = get_id(doc)
@operations << {
- path: @base_path + "/" + CGI::escape(id),
+ path: "#{@base_path}/#{CGI.escape(id)}",
method: "GET",
- callback: block_given? && block
+ callback: block
}
end
def update(doc, &block)
id = get_id(doc)
body = get_body(doc)
@operations << {
- path: @base_path + "/" + CGI::escape(id),
+ path: "#{@base_path}/#{CGI.escape(id)}",
method: "PATCH",
body: body,
- callback: block_given? && block
+ callback: block
}
end
def upsert(doc, &block)
id = get_id(doc)
body = get_body(doc)
@operations << {
- path: @base_path + "/" + CGI::escape(id),
+ path: "#{@base_path}/#{CGI.escape(id)}",
method: "POST",
body: body,
- callback: block_given? && block
+ callback: block
}
end
def delete(doc, &block)
id = get_id(doc)
@operations << {
- path: @base_path + "/" + CGI::escape(id),
+ path: "#{@base_path}/#{CGI.escape(id)}",
method: "DELETE",
- callback: block_given? && block
+ callback: block
}
end
def create(doc, &block)
body = get_body(doc)
@operations << {
path: @base_path,
method: "POST",
body: body,
- callback: block_given? && block
+ callback: block
}
end
private
@@ -102,17 +105,18 @@
def body_data(body)
body
end
def log_batch_response(operation, response)
- level = response.success?? :debug : :warn
log(:info) { "BATCH #{operation[:method]} #{operation[:path]}" }
log(:info) { "Status #{response.status}" }
+
+ level = response.success?? :debug : :warn
log(level) { "Response: #{JSON.pretty_generate(response.body)}" }
end
def log(level, &block)
- @logger.send(level, &block) if @logger
+ @logger&.send(level, &block)
end
end
end
end
end