require 'ecoportal/api/common/doc_helpers' module Ecoportal module API class V1 class People include Enumerable include Common::DocHelpers attr_reader :client def initialize(client) @client = client end def each(params: {}, &block) return to_enum(:each) unless block page = 1 loop do response = @client.get("/people", params: params.merge(page: page)) raise "Request failed." unless response.success? response.body["results"].each do |person| yield person_class.new(person) end break if page >= response.body["total_pages"] page += 1 end self end def get(doc) response = @client.get("/people/"+CGI::escape(get_id(doc))) Common::WrappedResponse.new(response, person_class) end def get_all(params: {}) each(params: params).to_a end def update(doc) body = get_body(doc) id = get_id(doc) @client.patch("/people/"+CGI::escape(id), data: body) end def create(doc) body = get_body(doc) @client.post("/people", data: body) end def upsert(doc) body = get_body(doc) id = get_id(doc) @client.post("/people/"+CGI::escape(id), data: body) end def batch operation = Common::BatchOperation.new("/people", person_class) yield operation @client.post("/people/batch", data: operation.as_json).tap do |response| operation.process_response(response) end end def delete(doc) id = get_id(doc) @client.delete("/people/"+CGI::escape(id)) end def new person_class.new end private def person_class V1::Person end def get_body(doc) if doc.respond_to?(:as_update) doc.as_update elsif doc.respond_to?(:as_json) doc.as_json else doc end end def get_id(doc) id = nil id ||= doc.id if doc.respond_to?(:id) id ||= doc.external_id if doc.respond_to?(:external_id) id ||= doc["id"] if doc.is_a?(Hash) id ||= doc["external_id"] if doc.is_a?(Hash) id ||= doc if doc.is_a?(String) id or raise "No ID has been given!" id end end end end end require 'ecoportal/api/v1/person' require 'ecoportal/api/v1/person_details' require 'ecoportal/api/v1/schema_field_value'