Class: RIQ::Contact

Inherits:
RIQObject show all
Defined in:
lib/riq/contact.rb

Overview

Contacts represent people in an Organization’s address book.

Instance Attribute Summary collapse

Attributes inherited from RIQObject

#id, #modified_date

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RIQObject

#delete, #initialize, #payload, #save

Constructor Details

This class inherits a constructor from RIQ::RIQObject

Instance Attribute Details

#propertiesObject

Returns the value of attribute properties.



7
8
9
# File 'lib/riq/contact.rb', line 7

def properties
  @properties
end

Class Method Details

.node(id = nil) ⇒ String

Returns endpoint.

Parameters:

  • id (String) (defaults to: nil)

    ObjectId

Returns:

  • (String)

    endpoint



15
16
17
# File 'lib/riq/contact.rb', line 15

def self.node(id = nil)
  "contacts/#{id}"
end

Instance Method Details

#add(prop, val) ⇒ Array

Adds property with correct scaffolding

Parameters:

  • prop (Symbol)

    Type Property to add, such as :name or :email

  • val (String, Array)

    Value(s) to add

Returns:

  • (Array)

    Values for that property after the add

Raises:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/riq/contact.rb', line 74

def add(prop, val)
  # validate(prop)
  prop = prop.to_sym
  @properties[prop] = [] unless @properties.include? prop

  if val.is_a? Array
    val.each do |i|
      add(prop, i)
    end
    return
  end

  raise RIQError, 'Values must be strings' unless val.is_a?(String)

  # don't add duplicate 
  if @properties[prop].select{|p| p[:value] == val}.empty?
    @properties[prop] << {value: val, metadata: {}}
  end
  get_prop(prop)
end

#addressString

Returns the preferred value for address.

Returns:

  • (String)

    the preferred value for address



61
62
63
# File 'lib/riq/contact.rb', line 61

def address
  get_prop(:address)
end

#dataHash

Returns all relevant stored data.

Returns:

  • (Hash)

    all relevant stored data



20
21
22
23
24
25
26
# File 'lib/riq/contact.rb', line 20

def data
  {
    id: @id,
    properties: @properties
    # modified_date: @modified_date
  }
end

#emailString

Returns the preferred value for email.

Returns:

  • (String)

    the preferred value for email



51
52
53
# File 'lib/riq/contact.rb', line 51

def email
  get_prop(:email)
end

#info(prop, val) ⇒ Hash

Returns metadata and other info about a given property.

Parameters:

  • prop (Symbol)

    The property to fetch. One of [:name, :phone, :email, :address]

  • val (String)

    The value to get info on, such as ‘[email protected]

Returns:

  • (Hash)

    metadata and other info about a given property



125
126
127
128
129
# File 'lib/riq/contact.rb', line 125

def info(prop, val)
  # validate(prop)

  @properties[prop].select{|p| p[:value] == val}.first
end

#nameArray

Returns all of the values for name.

Returns:

  • (Array)

    all of the values for name



30
31
32
# File 'lib/riq/contact.rb', line 30

def name
  get_prop(:name)
end

#nodeString

Returns endpoint.

Returns:

  • (String)

    endpoint



10
11
12
# File 'lib/riq/contact.rb', line 10

def node
  self.class.node(@id)
end

#phoneString

Returns the preferred value for phone.

Returns:

  • (String)

    the preferred value for phone



41
42
43
# File 'lib/riq/contact.rb', line 41

def phone
  get_prop(:phone)
end

#primary_addressString

Returns the preferred value for primary_address.

Returns:

  • (String)

    the preferred value for primary_address



66
67
68
# File 'lib/riq/contact.rb', line 66

def primary_address
  get_primary_prop(:address)
end

#primary_emailString

Returns the preferred value for primary_email.

Returns:

  • (String)

    the preferred value for primary_email



56
57
58
# File 'lib/riq/contact.rb', line 56

def primary_email
  get_primary_prop(:email)
end

#primary_nameString

Returns the preferred value for primary_name.

Returns:

  • (String)

    the preferred value for primary_name



36
37
38
# File 'lib/riq/contact.rb', line 36

def primary_name
  get_primary_prop(:name)
end

#primary_phoneString

Returns the preferred value for primary_phone.

Returns:

  • (String)

    the preferred value for primary_phone



46
47
48
# File 'lib/riq/contact.rb', line 46

def primary_phone
  get_primary_prop(:phone)
end

#remove(prop, val) ⇒ Array

Removes property from hash

Parameters:

  • prop (Symbol)

    Type Property to remove, such as :name or :email

  • val (String, Array)

    Value(s) to remove

Returns:

  • (Array)

    Values for that property after the remove



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/riq/contact.rb', line 99

def remove(prop, val)
  # validate(prop)
  prop = prop.to_sym

  if val.is_a? Array
    val.each do |i|
      remove(prop, i)
    end
  end

  if @properties.include? prop
    @properties[prop] = @properties[prop].reject{|p| p[:value] == val}
  end
  get_prop(prop)
end

#upsertObject

Edits an existing object based on matching email(s) or saves a new object

See Also:



117
118
119
120
# File 'lib/riq/contact.rb', line 117

def upsert
  # can only be email right now
  save({_upsert: 'email'})
end