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
- #create(doc) ⇒ Object
- #delete(doc) ⇒ Object
- #get(doc) ⇒ Object
-
#initialize(base_path, wrapper) ⇒ BatchOperation
constructor
A new instance of BatchOperation.
- #process_response(response) ⇒ Object
- #update(doc) ⇒ Object
- #upsert(doc) ⇒ Object
Methods included from DocHelpers
Constructor Details
#initialize(base_path, wrapper) ⇒ BatchOperation
Returns a new instance of BatchOperation.
8 9 10 11 12 |
# File 'lib/ecoportal/api/common/batch_operation.rb', line 8 def initialize(base_path, wrapper) @base_path = base_path @wrapper = wrapper @operations = [] end |
Instance Method Details
#as_json ⇒ Object
14 15 16 17 18 19 20 |
# 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) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/ecoportal/api/common/batch_operation.rb', line 84 def create(doc) body = get_body(doc) @operations << { path: @base_path, method: "POST", body: body, callback: block_given? && Proc.new } end |
#delete(doc) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/ecoportal/api/common/batch_operation.rb', line 75 def delete(doc) id = get_id(doc) @operations << { path: @base_path + "/" + CGI::escape(id), method: "DELETE", callback: block_given? && Proc.new } end |
#get(doc) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/ecoportal/api/common/batch_operation.rb', line 44 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) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ecoportal/api/common/batch_operation.rb', line 22 def process_response(response) if response.success? response.body.each.with_index do |subresponse, idx| next unless 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)) callback.call batch_response, batch_response.result else batch_response = BatchResponse.new(status, body) callback.call batch_response end end else raise "Total failure in batch operation" end end |
#update(doc) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ecoportal/api/common/batch_operation.rb', line 53 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) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ecoportal/api/common/batch_operation.rb', line 64 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 |