Class: Ecoportal::API::V1::Person

Inherits:
Common::BaseModel show all
Defined in:
lib/ecoportal/api/v1/person.rb

Direct Known Subclasses

Internal::Person

Instance Attribute Summary collapse

Attributes inherited from Common::BaseModel

#_key, #_parent

Instance Method Summary collapse

Methods inherited from Common::BaseModel

#as_update, #consolidate!, #dirty?, #doc, embeds_one, #initialize, #original_doc, passthrough, #print, #reset!, #to_json

Methods included from Common::BaseClass

#class_resolver, #resolve_class

Constructor Details

This class inherits a constructor from Ecoportal::API::Common::BaseModel

Instance Attribute Details

#detailsPersonDetails?

the details of the person or nil if missing.

Returns:



10
11
12
# File 'lib/ecoportal/api/v1/person.rb', line 10

def details
  @details
end

#external_idString

the alternative unique id of this person (unique in one organization).

Returns:

  • (String)

    the current value of external_id



10
11
12
# File 'lib/ecoportal/api/v1/person.rb', line 10

def external_id
  @external_id
end

#idString

the internal unique id of this person (unique in all the system).

Returns:

  • (String)

    the current value of id



10
11
12
# File 'lib/ecoportal/api/v1/person.rb', line 10

def id
  @id
end

#nameString

the name of the person.

Returns:

  • (String)

    the current value of name



10
11
12
# File 'lib/ecoportal/api/v1/person.rb', line 10

def name
  @name
end

#subordinatesInteger (readonly)

the number of people this person is supervisor of.

Returns:

  • (Integer)

    the current value of subordinates



10
11
12
# File 'lib/ecoportal/api/v1/person.rb', line 10

def subordinates
  @subordinates
end

#supervisor_idString

internal or external id of the supervisor of this person.

Returns:

  • (String)

    the current value of supervisor_id



10
11
12
# File 'lib/ecoportal/api/v1/person.rb', line 10

def supervisor_id
  @supervisor_id
end

Instance Method Details

#add_details(schema_or_id) ⇒ nil, PersonDetails

Note:
  • this method alone only sets the internal structure of the details.
  • you will not be able to reset! after using this method.

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=)

Parameters:

Returns:

  • (nil, PersonDetails)

    the resulting PersonDetails that set to the person.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ecoportal/api/v1/person.rb', line 76

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

#as_jsonObject



42
43
44
# File 'lib/ecoportal/api/v1/person.rb', line 42

def as_json
  super.merge "details" => details&.as_json
end

#supervisor(client) ⇒ Object

Gets the supervisor (Person) of this person, with given his supervisor_id.

Example Usage:

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}."

Parameters:



30
31
32
33
34
# File 'lib/ecoportal/api/v1/person.rb', line 30

def supervisor(client)
  return @supervisor if defined?(@supervisor)
  return @supervisor = nil if supervisor_id.nil?
  @supervisor = client.people.get(supervisor_id).result
end

#supervisor=(person) ⇒ Object

Sets the supervisor of a person.

Parameters:

  • person (Person, nil)

    the supervisor of this person.



38
39
40
# File 'lib/ecoportal/api/v1/person.rb', line 38

def supervisor=(person)
  self.supervisor_id = person&.id || person&.external_id
end