Class: RIQ::ListItem

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

Overview

A List Item is a row in a List.

Instance Attribute Summary collapse

Attributes inherited from RIQObject

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RIQObject

#delete, #save

Constructor Details

#initialize(id = nil, lid: nil) ⇒ ListItem

Returns a new instance of ListItem.

Examples:

create a list item

# vanilla
RIQ::ListItem.new
# with a list id
RIQ::ListItem(lid: 'abc123') # OR RIQ.list('abc123').list_item


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/riq/list_item.rb', line 21

def initialize(id = nil, lid: nil)
  if id.is_a? Hash
    # init with data
    super(id)
  elsif id.nil?
    # vanilla object
    super(nil)
    # maybe init with lid
    @list_id = lid unless lid.nil?
  elsif lid.nil?
    # has id, but not lid, that's an error
    raise RIQError, 'ObjectID and List ID are required'
  else
    # grabbing a specific listitem, fetch it
    super("#{lid}/listitems/#{id}")
  end
end

Instance Attribute Details

#account_idObject

Returns the value of attribute account_id.



9
10
11
# File 'lib/riq/list_item.rb', line 9

def 
  @account_id
end

#contact_idsObject

Returns the value of attribute contact_ids.



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

def contact_ids
  @contact_ids
end

#created_dateObject (readonly)

Returns the value of attribute created_date.



14
15
16
# File 'lib/riq/list_item.rb', line 14

def created_date
  @created_date
end

#field_valuesObject

Returns the value of attribute field_values.



8
9
10
# File 'lib/riq/list_item.rb', line 8

def field_values
  @field_values
end

#list_idObject

Returns the value of attribute list_id.



11
12
13
# File 'lib/riq/list_item.rb', line 11

def list_id
  @list_id
end

#modified_dateObject (readonly)

Returns the value of attribute modified_date.



13
14
15
# File 'lib/riq/list_item.rb', line 13

def modified_date
  @modified_date
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.node(lid = nil, oid = nil) ⇒ Object

Note:

this is the only object for which you have to include two params

Parameters:

  • lid (String) (defaults to: nil)

    ListId that the lit item belongs to

  • oid (String) (defaults to: nil)

    ObjectId for the object



47
48
49
50
51
52
53
54
# File 'lib/riq/list_item.rb', line 47

def self.node(lid = nil, oid = nil)
  # weird workaround for fetching node on init
  if lid.nil? && !oid.nil?
    "lists/#{oid}"
  else  
    "lists/#{lid || @list_id}/listitems/#{oid}"
  end
end

Instance Method Details

#dataHash

Returns all relevant stored data.

Returns:

  • (Hash)

    all relevant stored data



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/riq/list_item.rb', line 57

def data
  {
    name: @name,
    account_id: @account_id,
    contact_ids: @contact_ids.flatten,
    id: @id,
    list_id: @list_id,
    field_values: @field_values,
    modified_date: @modified_date
  }
end

#field_value(key) ⇒ String, Array #field_value(key, value) ⇒ Object

Overloads:

  • #field_value(key) ⇒ String, Array

    Returns Value of key.

    Parameters:

    • key (String, Integer)

    Returns:

    • (String, Array)

      Value of key

  • #field_value(key, value) ⇒ Object

    Sets key to value

    Parameters:

    • key (String, Integer)

      Key to set

    • value (String, Integer, Array)

      Sets key to value



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/riq/list_item.rb', line 91

def field_value(key, value = nil)
  # TODO: double check that this works with arrays of stuff
  # or, have a format function that casts ints to string on save
  if value.nil?
    @field_values.fetch(key.to_sym, nil)
  else
    unless value.is_a? Array
      value = value.to_s
    end
    @field_values[key.to_sym] = value
    {key.to_sym => value}
  end
end

#nodeString

Returns endpoint.

Returns:

  • (String)

    endpoint



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

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

#payloadString

Returns the JSON representation of #data.

Returns:

  • (String)

    the JSON representation of #data



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/riq/list_item.rb', line 70

def payload
  pld = {}
  data.each do |k, v|
    if k == :field_values
      pld['fieldValues'] = @field_values.to_raw
    elsif k['_']
      pld[k.to_cam] = v
    else
      pld[k] = v
    end
  end
  pld.to_json
end