Class: Ecoportal::API::V1::Person
- Inherits:
-
Common::BaseModel
- Object
- Common::BaseModel
- Ecoportal::API::V1::Person
- Defined in:
- lib/ecoportal/api/v1/person.rb
Direct Known Subclasses
Constant Summary collapse
- VALID_TAG_REGEX =
/^[A-Za-z0-9 &_'\/.-]+$/- VALID_EMAIL_REGEX =
/^[^@\s]+@[^@\s]+\.[^@\s]+$/
Instance Attribute Summary collapse
-
#contractor_organization_id ⇒ String
internal id of the contractor entity of this person.
-
#details ⇒ PersonDetails?
the details of the person or
nilif missing. -
#external_id ⇒ String
the alternative unique id of this person (unique in one organization).
-
#id ⇒ String
the internal unique id of this person (unique in all the system).
-
#name ⇒ String
the name of the person.
-
#supervisor_id ⇒ String
internal or external id of the supervisor of this person.
Attributes inherited from Common::BaseModel
Instance Method Summary collapse
-
#add_details(schema_or_id) ⇒ nil, PersonDetails
Sets the PersonDetails to the person, depending on the parameter received: -
PersonSchema: initializes thePersonDetailsas per the schema specified (schema_idandfields). - #archive! ⇒ Object
- #as_json ⇒ Object
- #as_update(ref = :last, ignore: []) ⇒ Object
-
#email=(value) ⇒ Object
Sets the email of a person.
-
#filter_tags ⇒ Array<String>
The filter tags of this person.
-
#filter_tags=(value) ⇒ Object
Validates the string tags of the array, and sets the
filter_tagsproperty of the account. -
#supervisor(client) ⇒ Object
Gets the supervisor (
Person) of this person, with given hissupervisor_id. -
#supervisor=(person) ⇒ Object
Sets the supervisor of a person.
- #unarchive! ⇒ Object
Methods inherited from Common::BaseModel
#consolidate!, #dirty?, #doc, embeds_one, #initial_doc, #initialize, #original_doc, passthrough, #print_pretty, #replace_doc!, #replace_original_doc!, #reset!, #to_json
Methods included from Common::BaseClass
#class_resolver, #redef_without_warning, #resolve_class
Constructor Details
This class inherits a constructor from Ecoportal::API::Common::BaseModel
Instance Attribute Details
#contractor_organization_id ⇒ String
internal id of the contractor entity of this person.
10 11 12 |
# File 'lib/ecoportal/api/v1/person.rb', line 10 def contractor_organization_id @contractor_organization_id end |
#details ⇒ PersonDetails?
the details of the person or nil if missing.
10 11 12 |
# File 'lib/ecoportal/api/v1/person.rb', line 10 def details @details end |
#external_id ⇒ String
the alternative unique id of this person (unique in one organization).
10 11 12 |
# File 'lib/ecoportal/api/v1/person.rb', line 10 def external_id @external_id end |
#id ⇒ String
the internal unique id of this person (unique in all the system).
10 11 12 |
# File 'lib/ecoportal/api/v1/person.rb', line 10 def id @id end |
#name ⇒ String
the name of the person.
10 11 12 |
# File 'lib/ecoportal/api/v1/person.rb', line 10 def name @name end |
#supervisor_id ⇒ String
internal or external id of the supervisor of this person.
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
- 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 thePersonDetailsas per the schema specified (schema_idandfields).String: it just sets theschema_idon thePersonDetails(asfieldsis not include,details[key]=will throw error). (see #details=)
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/ecoportal/api/v1/person.rb', line 131 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 |
#archive! ⇒ Object
30 31 32 |
# File 'lib/ecoportal/api/v1/person.rb', line 30 def archive! self.archived = true end |
#as_json ⇒ Object
91 92 93 |
# File 'lib/ecoportal/api/v1/person.rb', line 91 def as_json super.merge 'details' => details&.as_json end |
#as_update(ref = :last, ignore: []) ⇒ Object
95 96 97 |
# File 'lib/ecoportal/api/v1/person.rb', line 95 def as_update(ref = :last, ignore: []) super end |
#email=(value) ⇒ Object
Sets the email of a person.
62 63 64 65 66 |
# File 'lib/ecoportal/api/v1/person.rb', line 62 def email=(value) raise "Invalid email #{value.inspect}" if value && !value.match(VALID_EMAIL_REGEX) doc['email'] = value&.downcase end |
#filter_tags ⇒ Array<String>
Returns the filter tags of this person.
87 88 89 |
# File 'lib/ecoportal/api/v1/person.rb', line 87 def doc['filter_tags'] ||= [] end |
#filter_tags=(value) ⇒ Object
all is set in upper case and preserves the original order.
Validates the string tags of the array, and sets the filter_tags property of the account.
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ecoportal/api/v1/person.rb', line 72 def (value) msg = "filter_tags= needs to be passed an Array, got #{value.class}" raise ArgumentError, msg unless value.is_a?(Array) = value.compact.map do |tag| msg = "Invalid filter tag #{tag.inspect}" raise ArgumentError, msg unless tag.match(VALID_TAG_REGEX) tag.upcase end set_uniq_array_keep_order('filter_tags', ) 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}."
47 48 49 50 51 52 |
# File 'lib/ecoportal/api/v1/person.rb', line 47 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.
56 57 58 |
# File 'lib/ecoportal/api/v1/person.rb', line 56 def supervisor=(person) self.supervisor_id = person&.id || person&.external_id end |
#unarchive! ⇒ Object
26 27 28 |
# File 'lib/ecoportal/api/v1/person.rb', line 26 def unarchive! self.archived = false end |