class Ecoportal::API::V1::Person
@attr id [String] the internal unique id of this person (unique in all the system). @attr external_id [String] the alternative unique id of this person (unique in one organization). @attr name [String] the name of the person. @attr supervisor_id [String] internal or external id of the supervisor of this person. @attr_reader subordinates [Integer] the number of people this person is supervisor of. @attr details [PersonDetails, nil] the details of the person or `nil` if missing.
Constants
- VALID_EMAIL_REGEX
- VALID_TAG_REGEX
Public Instance Methods
Sets the PersonDetails
to the person, depending on the parameter received:
- `PersonSchema`: initializes the `PersonDetails` as per the schema specified (`schema_id` and `fields`). - `String`: it just sets the `schema_id` on the `PersonDetails` (as `fields` is not include, `details[key]=` will throw error).
(see details=
) @note
- this method alone only sets the internal structure of the details. - you will not be able to `reset!` after using this method.
@param schema_or_id [PersonSchema, String, nil, PersonDetails
, Hash] value to be set. @return [nil, PersonDetails] the resulting `PersonDetails` that set to the person.
# File lib/ecoportal/api/v1/person.rb, line 114 def add_details(schema_or_id) person_details_class.new.tap do |new_details| case schema_or_id when person_schema_class schema_or_id.initialize_details(new_details) when String new_details.schema_id = schema_or_id else raise "Invalid set on details: Requierd PersonSchema or String; got #{schema_or_id.class}" end self.details = new_details # Patch out static data from as_update original_doc["details"] = { "fields" => JSON.parse(doc["details"]["fields"].to_json) } end end
Ecoportal::API::Common::BaseModel#as_json
# File lib/ecoportal/api/v1/person.rb, line 76 def as_json super.merge "details" => details&.as_json end
Ecoportal::API::Common::BaseModel#as_update
# File lib/ecoportal/api/v1/person.rb, line 80 def as_update(ref = :last, ignore: []) super(ignore: ignore | ["subordinates"]) end
Sets the PersonDetails
to the person, depending on the paramter received:
- `nil`: blanks the details. - `PersonDetails`: sets a copy of the object param as details. - `Hash`: sets respectivelly the `schema_id` and the `fields` keys of the Hash to the person details.
@note unique point of access to update the PersonDetails
. @param value [nil, PersonDetails
, Hash] value to be set. @return [nil, PersonDetails] the resulting `PersonDetails` set to the person.
# File lib/ecoportal/api/v1/person.rb, line 91 def details=(value) case value when NilClass doc["details"] = nil when person_details_class doc["details"] = value.as_json when Hash doc["details"] = value.slice("schema_id", "fields") else raise "Invalid type set on details. Required nil, PersonDetails or Hash; got #{value.class}" end remove_instance_variable("@details") if defined?(@details) end
Sets the email of a person. @param value [String, nil] the email of this person.
# File lib/ecoportal/api/v1/person.rb, line 47 def email=(value) unless !value || value.match(VALID_EMAIL_REGEX) raise "Invalid email #{value.inspect}" end doc["email"] = value&.downcase end
Gets the supervisor (`Person`) of this person, with given his `supervisor_id`.
**Example Usage**: “`ruby API_KEY = 'some-private-api-key-version' HOST = “live.ecoportal.com” api = Ecoportal::API::Internal.new(API_KEY, host: HOST) person = api.people.get({“id”: “my-dummy-user”}) super = person.supervisor(api.client) pp “#{person.name}'s supervisor is #{super.name}.” “`
@param client [Common::Client] the api client to make the request.
# File lib/ecoportal/api/v1/person.rb, line 33 def supervisor(client) return @supervisor if defined?(@supervisor) return @supervisor = nil if supervisor_id.nil? @supervisor = client.people.get(supervisor_id).result end
Sets the supervisor of a person. @param person [Person, nil] the supervisor of this person.
# File lib/ecoportal/api/v1/person.rb, line 41 def supervisor=(person) self.supervisor_id = person&.id || person&.external_id end