Class: RelateIq::ListItem

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ ListItem

field values always contains decoded values for example { ‘Status’ => ‘Application Submitted’ }



13
14
15
16
17
18
19
# File 'lib/relateiq/list_item.rb', line 13

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

Instance Attribute Details

#account_idObject

Returns the value of attribute account_id.



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

def 
  @account_id
end

#contact_idsObject

Returns the value of attribute contact_ids.



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

def contact_ids
  @contact_ids
end

#field_valuesObject

Returns the value of attribute field_values.



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

def field_values
  @field_values
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#list_idObject

Returns the value of attribute list_id.



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

def list_id
  @list_id
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.create(attrs) ⇒ Object



53
54
55
# File 'lib/relateiq/list_item.rb', line 53

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

.find_by_contact(list_id, contact_id) ⇒ Object



57
58
59
# File 'lib/relateiq/list_item.rb', line 57

def self.find_by_contact(list_id, contact_id)
  from_json(resource(list_id)["?contactIds=#{contact_id}"].get)
end

.from_json(json_string) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/relateiq/list_item.rb', line 44

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

.resource(list_id) ⇒ Object



39
40
41
42
# File 'lib/relateiq/list_item.rb', line 39

def self.resource(list_id)
  @resource ||= ServiceFactory.get_endpoint('lists')
  @resource["#{list_id}/listitems"]
end

Instance Method Details

#initialize_by_user(attrs) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/relateiq/list_item.rb', line 21

def initialize_by_user(attrs)
  @list_id = attrs.fetch(:list_id)
  @id = attrs.fetch(:id, nil)
  @name = attrs.fetch(:name, nil)
  @account_id = attrs.fetch(:account_id, nil)
  @contact_ids = attrs.fetch(:contact_ids, [])
  @field_values = attrs.fetch(:field_values, [])
end

#initialize_from_api(attrs) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/relateiq/list_item.rb', line 30

def initialize_from_api(attrs)
  @list_id = attrs.fetch(:listId)
  @id = attrs.fetch(:id, nil)
  @name = attrs.fetch(:name, nil)
  @account_id = attrs.fetch(:accountId, nil)
  @contact_ids = attrs.fetch(:contactIds, [])
  @field_values = decode_field_values(attrs.fetch(:fieldValues, nil))
end

#saveObject



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

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

#to_jsonObject



69
70
71
# File 'lib/relateiq/list_item.rb', line 69

def to_json
  to_hash.to_json
end