Class: RelateIq::Contact

Inherits:
Object
  • Object
show all
Defined in:
lib/relateiq/contact.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Contact

Returns a new instance of Contact.



60
61
62
63
64
65
66
# File 'lib/relateiq/contact.rb', line 60

def initialize(attrs = {})
  if attrs.key? :properties
    initialize_from_api(attrs)
  else
    initialize_by_user(attrs)
  end
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



3
4
5
# File 'lib/relateiq/contact.rb', line 3

def address
  @address
end

#cityObject

Returns the value of attribute city.



3
4
5
# File 'lib/relateiq/contact.rb', line 3

def city
  @city
end

#companyObject

Returns the value of attribute company.



3
4
5
# File 'lib/relateiq/contact.rb', line 3

def company
  @company
end

#emailObject

Returns the value of attribute email.



3
4
5
# File 'lib/relateiq/contact.rb', line 3

def email
  @email
end

#first_nameObject

Returns the value of attribute first_name.



3
4
5
# File 'lib/relateiq/contact.rb', line 3

def first_name
  @first_name
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/relateiq/contact.rb', line 3

def id
  @id
end

#last_nameObject

Returns the value of attribute last_name.



3
4
5
# File 'lib/relateiq/contact.rb', line 3

def last_name
  @last_name
end

#linkedinObject

Returns the value of attribute linkedin.



3
4
5
# File 'lib/relateiq/contact.rb', line 3

def linkedin
  @linkedin
end

#phoneObject

Returns the value of attribute phone.



3
4
5
# File 'lib/relateiq/contact.rb', line 3

def phone
  @phone
end

#stateObject

Returns the value of attribute state.



3
4
5
# File 'lib/relateiq/contact.rb', line 3

def state
  @state
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/relateiq/contact.rb', line 3

def title
  @title
end

#twitterObject

Returns the value of attribute twitter.



3
4
5
# File 'lib/relateiq/contact.rb', line 3

def twitter
  @twitter
end

#zipcodeObject

Returns the value of attribute zipcode.



3
4
5
# File 'lib/relateiq/contact.rb', line 3

def zipcode
  @zipcode
end

Class Method Details

.create(attrs) ⇒ Object



29
30
31
# File 'lib/relateiq/contact.rb', line 29

def self.create(attrs)
  Contact.new(attrs).save
end

.find(id) ⇒ Object



21
22
23
# File 'lib/relateiq/contact.rb', line 21

def self.find(id)
  from_json(resource["#{id}"].get)
end

.find_by_email(email) ⇒ Object



25
26
27
# File 'lib/relateiq/contact.rb', line 25

def self.find_by_email(email)
  from_json(resource["?properties.email=#{email}"].get)
end

.from_json(json_string) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/relateiq/contact.rb', line 51

def self.from_json(json_string)
  contact_hash = JSON.parse(json_string, symbolize_names: true)
  if contact_hash.key? :objects
    contact_hash[:objects].map { |li| Contact.new(li) }
  else
    Contact.new(contact_hash)
  end
end

.resourceObject



17
18
19
# File 'lib/relateiq/contact.rb', line 17

def self.resource
  @resource ||= ServiceFactory.get_endpoint('contacts')
end

Instance Method Details

#full_addressObject



38
39
40
41
# File 'lib/relateiq/contact.rb', line 38

def full_address
  return nil unless address || city || state || zipcode
  "#{address}, #{city}, #{state} #{zipcode}"
end

#full_nameObject



33
34
35
36
# File 'lib/relateiq/contact.rb', line 33

def full_name
  return nil unless first_name || last_name
  "#{first_name} #{last_name}"
end

#saveObject



43
44
45
46
47
48
49
# File 'lib/relateiq/contact.rb', line 43

def save
  if id
    Contact.from_json(Contact.resource["#{id}"].put to_json)
  else
    Contact.from_json(Contact.resource.post to_json)
  end
end

#to_jsonObject



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/relateiq/contact.rb', line 68

def to_json
  riq_hash = { properties: { email: [{ value: @email }] } }
  riq_hash[:id] = id if id
  inject_property_value_hash(riq_hash[:properties], 'name', full_name)
  inject_property_value_hash(riq_hash[:properties], 'phone', phone)
  inject_property_value_hash(riq_hash[:properties], 'address', address)
  inject_property_value_hash(riq_hash[:properties], 'liurl', linkedin)
  inject_property_value_hash(riq_hash[:properties], 'twhan', twitter)
  inject_property_value_hash(riq_hash[:properties], 'company', company)
  inject_property_value_hash(riq_hash[:properties], 'title', title)
  riq_hash.to_json
end