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

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

Direct Known Subclasses

Internal::Person

Constant Summary collapse

VALID_TAG_REGEX =
/^[A-Za-z0-9 &_'\/.-]+$/
VALID_EMAIL_REGEX =
/^[^@\s]+@[^@\s]+\.[^@\s]+$/
NON_PHONE_REGEX =
/[^+0-9]/

Instance Attribute Summary collapse

Attributes inherited from Common::BaseModel

#_key, #_parent

Instance Method Summary collapse

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_idString

internal id of the contractor entity of this person.

Returns:

  • (String)

    the current value of contractor_organization_id



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

def contractor_organization_id
  @contractor_organization_id
end

#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

#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.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/ecoportal/api/v1/person.rb', line 143

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



33
34
35
# File 'lib/ecoportal/api/v1/person.rb', line 33

def archive!
  self.archived = true
end

#as_jsonObject



103
104
105
# File 'lib/ecoportal/api/v1/person.rb', line 103

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

#as_update(ref = :last, ignore: []) ⇒ Object



107
108
109
# File 'lib/ecoportal/api/v1/person.rb', line 107

def as_update(ref = :last, ignore: [])
  super
end

#email=(value) ⇒ Object

Sets the email of a person.

Parameters:

  • value (String, nil)

    the email of this person.



65
66
67
68
69
# File 'lib/ecoportal/api/v1/person.rb', line 65

def email=(value)
  raise "Invalid email #{value.inspect}" if value && !value.match(VALID_EMAIL_REGEX)

  doc['email'] = value&.downcase
end

#filter_tagsArray<String>

Returns the filter tags of this person.

Returns:

  • (Array<String>)

    the filter tags of this person.



99
100
101
# File 'lib/ecoportal/api/v1/person.rb', line 99

def filter_tags
  doc['filter_tags'] ||= []
end

#filter_tags=(value) ⇒ Object

Note:

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.

Parameters:

  • value (Array<String>)

    array of tags.

Raises:

  • (Exception)

    if there was any invalid string tag.



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ecoportal/api/v1/person.rb', line 84

def filter_tags=(value)
  msg = "filter_tags= needs to be passed an Array, got #{value.class}"
  raise ArgumentError, msg unless value.is_a?(Array)

  end_tags = 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_tags)
end

#phone_number=(value) ⇒ Object

Note:

this property is read-only in the APIv0.

Sets the primary phone number of a person.

Parameters:

  • value (String, nil)

    the phone number of this person.



74
75
76
77
78
# File 'lib/ecoportal/api/v1/person.rb', line 74

def phone_number=(value)
  return

  doc['phone_number'] = parse_number(value)
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:



50
51
52
53
54
55
# File 'lib/ecoportal/api/v1/person.rb', line 50

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.



59
60
61
# File 'lib/ecoportal/api/v1/person.rb', line 59

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

#unarchive!Object



29
30
31
# File 'lib/ecoportal/api/v1/person.rb', line 29

def unarchive!
  self.archived = false
end