Class: Ecoportal::API::Common::BatchOperation
- Inherits:
-
Object
- Object
- Ecoportal::API::Common::BatchOperation
- Includes:
- DocHelpers
- Defined in:
- lib/ecoportal/api/common/batch_operation.rb
Instance Method Summary collapse
- #as_json ⇒ Object
- #count ⇒ Object
- #create(doc, &block) ⇒ Object
- #delete(doc, &block) ⇒ Object
- #get(doc, &block) ⇒ Object
-
#initialize(base_path, wrapper, logger: nil, deep_logging: false) ⇒ BatchOperation
constructor
A new instance of BatchOperation.
- #process_response(response) ⇒ Object
- #update(doc, &block) ⇒ Object
- #upsert(doc, &block) ⇒ Object
Methods included from DocHelpers
Constructor Details
#initialize(base_path, wrapper, logger: nil, deep_logging: false) ⇒ BatchOperation
Returns a new instance of BatchOperation.
7 8 9 10 11 12 13 |
# File 'lib/ecoportal/api/common/batch_operation.rb', line 7 def initialize(base_path, wrapper, logger: nil, deep_logging: false) @base_path = base_path @wrapper = wrapper @operations = [] @logger = logger @deep_logging = deep_logging end |
Instance Method Details
#as_json ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/ecoportal/api/common/batch_operation.rb', line 19 def as_json { actions: @operations.map do |operation| operation.slice(:path, :method, :body) end } end |
#count ⇒ Object
15 16 17 |
# File 'lib/ecoportal/api/common/batch_operation.rb', line 15 def count @operations.count end |
#create(doc, &block) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/ecoportal/api/common/batch_operation.rb', line 96 def create(doc, &block) body = get_body(doc) @operations << { path: @base_path, method: "POST", body: body, callback: block } end |
#delete(doc, &block) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/ecoportal/api/common/batch_operation.rb', line 87 def delete(doc, &block) id = get_id(doc) @operations << { path: "#{@base_path}/#{CGI.escape(id)}", method: "DELETE", callback: block } end |
#get(doc, &block) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/ecoportal/api/common/batch_operation.rb', line 56 def get(doc, &block) id = get_id(doc) @operations << { path: "#{@base_path}/#{CGI.escape(id)}", method: "GET", callback: block } end |
#process_response(response) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ecoportal/api/common/batch_operation.rb', line 27 def process_response(response) unless response.success? msg = "Error: total failure in batch operation." log(:debug) { msg } raise msg end log(:debug) { "Processing batch responses" } if deep_logging? body_data(response.body).each.with_index do |subresponse, idx| status = subresponse["status"] 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&.call(batch_response, batch_response.result) else batch_response = BatchResponse.new(status, body) log_batch_response(@operations[idx], batch_response) callback&.call(batch_response) end end end |
#update(doc, &block) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ecoportal/api/common/batch_operation.rb', line 65 def update(doc, &block) id = get_id(doc) body = get_body(doc) @operations << { path: "#{@base_path}/#{CGI.escape(id)}", method: "PATCH", body: body, callback: block } end |
#upsert(doc, &block) ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ecoportal/api/common/batch_operation.rb', line 76 def upsert(doc, &block) id = get_id(doc) body = get_body(doc) @operations << { path: "#{@base_path}/#{CGI.escape(id)}", method: "POST", body: body, callback: block } end |