Class: Kentaa::Api::Resources::Resource

Inherits:
Base
  • Object
show all
Defined in:
lib/kentaa/api/resources/resource.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#config, #endpoint_path, #options, #resource_class

Instance Method Summary collapse

Methods inherited from Base

attribute_key, #loaded?

Constructor Details

#initialize(config, id: nil, data: nil, options: {}) ⇒ Resource

Returns a new instance of Resource.



11
12
13
14
15
16
17
18
19
20
# File 'lib/kentaa/api/resources/resource.rb', line 11

def initialize(config, id: nil, data: nil, options: {})
  super(config, options)

  if data
    @data = data || {}
    @id = @data.fetch(:id) if @data.key?(:id)
  elsif id
    @id = id
  end
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/kentaa/api/resources/resource.rb', line 9

def id
  @id
end

Instance Method Details

#create(attributes) ⇒ Object



29
30
31
# File 'lib/kentaa/api/resources/resource.rb', line 29

def create(attributes)
  save(attributes)
end

#created_atObject



50
51
52
# File 'lib/kentaa/api/resources/resource.rb', line 50

def created_at
  Time.parse(data[:created_at]) if data[:created_at]
end

#deleteObject



44
45
46
47
48
# File 'lib/kentaa/api/resources/resource.rb', line 44

def delete
  delete_resource

  nil
end

#loadObject



22
23
24
25
26
27
# File 'lib/kentaa/api/resources/resource.rb', line 22

def load
  super
  @id = data.fetch(:id) if data.key?(:id)

  self
end

#save(attributes) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/kentaa/api/resources/resource.rb', line 33

def save(attributes)
  if id
    @response = update_resource(attributes)
  else
    @response = create_resource(attributes)
    @id = data.fetch(:id) if data.key?(:id)
  end

  self
end

#updated_atObject



54
55
56
# File 'lib/kentaa/api/resources/resource.rb', line 54

def updated_at
  Time.parse(data[:updated_at]) if data[:updated_at]
end