Class: RIQ::RIQObject Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/riq/riq_obj.rb

Overview

This class is abstract.

This class should not be used directly. Instead, use a child such as Contact or List.

Direct Known Subclasses

Account, Contact, Event, List, ListItem, User

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil) ⇒ RIQObject

Returns Self.

Parameters:

  • id (String, Hash) (defaults to: nil)

    ObjectId or well-formatted hash of data (usually provided by another object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/riq/riq_obj.rb', line 13

def initialize(id = nil)
  @client = RIQ.client
  @id = id

  unless @id.nil?
    # data hash
    if @id.is_a? Hash
      # this looks dumb, could name variables better
      data = @id
    else
      data = @client.get(node)
    end
    init(data.symbolize)
  else
    init
  end
  self
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#modified_dateObject (readonly)

Returns the value of attribute modified_date.



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

def modified_date
  @modified_date
end

Class Method Details

.node(id = nil) ⇒ String

Returns endpoint.

Parameters:

  • id (String) (defaults to: nil)

    ObjectId

Returns:

  • (String)

    endpoint

Raises:



39
40
41
# File 'lib/riq/riq_obj.rb', line 39

def self.node(id = nil)
  raise RIQError, 'This should be overwritten'
end

Instance Method Details

#dataHash

Returns all relevant stored data.

Returns:

  • (Hash)

    all relevant stored data

Raises:



44
45
46
# File 'lib/riq/riq_obj.rb', line 44

def data
  raise RIQError, 'This should be overwritten'
end

#deleteObject

Note:

This is IRREVERSIBLE

Deletes the object



75
76
77
# File 'lib/riq/riq_obj.rb', line 75

def delete
  @client.delete(node)
end

#nodeString

Returns endpoint.

Returns:

  • (String)

    endpoint

Raises:



33
34
35
# File 'lib/riq/riq_obj.rb', line 33

def node
  raise RIQError, 'This should be overwritten'
end

#payloadString

Returns the JSON representation of #data.

Returns:

  • (String)

    the JSON representation of #data



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/riq/riq_obj.rb', line 49

def payload
  pld = {}
  data.each do |k, v|
    if k['_']
      pld[k.to_cam] = v
    else
      pld[k] = v
    end
  end
  pld.to_json
end

#save(options = nil) ⇒ Object

Creates or updates the object



62
63
64
65
66
67
68
69
70
71
# File 'lib/riq/riq_obj.rb', line 62

def save(options = nil)
  pre_save
  if @id.nil?
    # create
    init(@client.post(node, payload, options: options).symbolize)
  else
    # update
    init(@client.put(node, payload, options: options).symbolize)
  end
end