module Ecoportal module API class V1 # @attr_reader client [Common::Client] a `Common::Client` object that holds the configuration of the api connection. class People extend Common::BaseClass include Enumerable include Common::DocHelpers class_resolver :person_class, "Ecoportal::API::V1::Person" attr_reader :client # @param client [Common::Client] a `Common::Client` object that holds the configuration of the api connection. # @return [People] an instance object ready to make people api requests. def initialize(client) @client = client end # Iterates all the people of the organization. # @note # - it ignores the key `results_from:` of `params:`. # - `each` is called by `to_a` # @param params [Hash] # @option params [String] :per_page the number of people you get per request. # @option params [String] :q some text to search. Omit this parameter to target all the people. # @yield [person] does some stuff with the person. # @yieldparam person [Person] def each(params: {}, &block) return to_enum(:each) unless block results_from = nil loop do params.update(results_from: results_from) if results_from response = client.get("/people", params: params) raise "Request failed." unless response.success? response.body["results"].each do |person| yield person_class.new(person) end break unless (results_from = response.body["next_results_from"]) end self end # Gets all the people via api requests. # @note it ignores the key `:page` in `params:`. # @param params [Hash] # @option params [Integer] :per_page the number of people you get per request. # @option params [String] :q some text to search. # @return [Array] the array of people got via api. def get_all(params: {}) each(params: params).to_a end # Gets a person via api. # @note if the request has `success?` the returned `object.result` gives an object with that `Person`. # @param doc [String, Hash, Person] data containing an `id` (internal or external) of the target person. # @return [WrappedResponse] an object with the api response. def get(doc) response = client.get("/people/"+CGI::escape(get_id(doc))) Common::WrappedResponse.new(response, person_class) end # Requests an update of a person via api. # @param doc [Person, Hash] data that at least contains an `id` (internal or external) of the target person. # @return [Response] an object with the api response. def update(doc) body = get_body(doc) id = get_id(doc) client.patch("/people/"+CGI::escape(id), data: body) end # Requests to create a person via api. # @param doc [Person, Hash] data that at least contains an `id` (internal or external) of the target person. # @return [Response] an object with the api response. def create(doc) body = get_body(doc) client.post("/people", data: body) end # Requests to update an existing person or if it does not exist, to create it, via api. # @param doc [Person, Hash] data that at least contains an `id` (internal or external) of the target person. # @return [Response] an object with the api response. def upsert(doc) body = get_body(doc) id = get_id(doc) client.post("/people/"+CGI::escape(id), data: body) end # Requests to completelly remove from an organization an existing person via api. # @param doc [Person, Hash] data that at least contains an `id` (internal or external) of the target person. # @return [Response] an object with the api response. def delete(doc) id = get_id(doc) client.delete("/people/"+CGI::escape(id)) end # Creates a `BatchOperation` and yields it to the given bock. # @yield [batch_op] adds multiple api requests for the current batch. # @yieldparam batch_op [BatchOperation] def batch operation = Common::BatchOperation.new("/people", person_class, logger: client.logger) yield operation # The batch operation is responsible for logging the output client.without_response_logging do client.post("/people/batch", data: operation.as_json).tap do |response| operation.process_response(response) end end end # Creates a new `Person` object. # @return [Person] new empty person object of the current version. def new person_class.new end end end end end require 'ecoportal/api/v1/schema_field_value' require 'ecoportal/api/v1/person_details' require 'ecoportal/api/v1/person'