class Ecoportal::API::Common::BatchOperation

Public Class Methods

new(base_path, wrapper, logger: nil) click to toggle source
# File lib/ecoportal/api/common/batch_operation.rb, line 7
def initialize(base_path, wrapper, logger: nil)
  @base_path  = base_path
  @wrapper    = wrapper
  @operations = []
  @logger     = logger
end

Public Instance Methods

as_json() click to toggle source
# File lib/ecoportal/api/common/batch_operation.rb, line 14
def as_json
  {
    actions: @operations.map do |operation|
      operation.slice(:path, :method, :body)
    end
  }
end
create(doc) click to toggle source
# File lib/ecoportal/api/common/batch_operation.rb, line 88
def create(doc)
  body = get_body(doc)
  @operations << {
    path:     @base_path,
    method:   "POST",
    body:     body,
    callback: block_given? && Proc.new
  }
end
delete(doc) click to toggle source
# File lib/ecoportal/api/common/batch_operation.rb, line 79
def delete(doc)
  id = get_id(doc)
  @operations << {
    path:     @base_path + "/" + CGI::escape(id),
    method:   "DELETE",
    callback: block_given? && Proc.new
  }
end
get(doc) click to toggle source
# File lib/ecoportal/api/common/batch_operation.rb, line 48
def get(doc)
  id = get_id(doc)
  @operations << {
    path:     @base_path + "/" + CGI::escape(id),
    method:   "GET",
    callback: block_given? && Proc.new
  }
end
process_response(response) click to toggle source
# File lib/ecoportal/api/common/batch_operation.rb, line 22
def process_response(response)
  unless response.success?
    log(:error) { "Total failure in batch operation." }
    raise "Total failure in batch operation"
  end

  log(:info) { "Processing batch responses" }

  body_data(response.body).each.with_index do |subresponse, idx|
    callback = @operations[idx][:callback]
    status   = subresponse["status"]
    body     = subresponse["response"]
    method   = @operations[idx][:method]

    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)
    else
      batch_response = BatchResponse.new(status, body)
      log_batch_response(@operations[idx], batch_response)
      callback && callback.call(batch_response)
    end
  end
end
update(doc) click to toggle source
# File lib/ecoportal/api/common/batch_operation.rb, line 57
def update(doc)
  id   = get_id(doc)
  body = get_body(doc)
  @operations << {
    path:     @base_path + "/" + CGI::escape(id),
    method:   "PATCH",
    body:     body,
    callback: block_given? && Proc.new
  }
end
upsert(doc) click to toggle source
# File lib/ecoportal/api/common/batch_operation.rb, line 68
def upsert(doc)
  id   = get_id(doc)
  body = get_body(doc)
  @operations << {
    path:     @base_path + "/" + CGI::escape(id),
    method:   "POST",
    body:     body,
    callback: block_given? && Proc.new
  }
end

Private Instance Methods

body_data(body) click to toggle source

Hook for other api versions to obtain the raw data of a response @note this was introduced to allow `v2` to reuse this class

# File lib/ecoportal/api/common/batch_operation.rb, line 102
def body_data(body)
  body
end
log(level, &block) click to toggle source
# File lib/ecoportal/api/common/batch_operation.rb, line 113
def log(level, &block)
  @logger.send(level, &block) if @logger
end
log_batch_response(operation, response) click to toggle source
# File lib/ecoportal/api/common/batch_operation.rb, line 106
def log_batch_response(operation, response)
  level = response.success?? :debug : :warn
  log(:info) { "BATCH #{operation[:method]} #{operation[:path]}" }
  log(:info) { "Status #{response.status}" }
  log(level) { "Response: #{JSON.pretty_generate(response.body)}" }
end