Class: Cubits::Resource

Inherits:
Hashie::Mash
  • Object
show all
Defined in:
lib/cubits/resource.rb

Direct Known Subclasses

Invoice

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(params = {}) ⇒ Object

Creates a new resource



46
47
48
# File 'lib/cubits/resource.rb', line 46

def self.create(params = {})
  self.new Cubits.connection.post(path_to, params)
end

.find(id) ⇒ Object

Loads resource

Parameters:

  • id (String)

Returns:

  • nil if resource is not found



31
32
33
34
35
# File 'lib/cubits/resource.rb', line 31

def self.find(id)
  self.new Cubits.connection.get(path_to(id))
rescue NotFound
  nil
end

.path(p) ⇒ Object

Sets path for the resource



8
9
10
# File 'lib/cubits/resource.rb', line 8

def self.path(p)
  @path = p
end

.path_to(resource_or_id = nil) ⇒ Object

Returns API path to resource



14
15
16
17
18
19
20
21
22
23
# File 'lib/cubits/resource.rb', line 14

def self.path_to(resource_or_id = nil)
  fail ArgumentError, "Resource path is not set for #{self.class.name}" unless @path
  if resource_or_id.is_a?(Resource)
    "#{@path}/#{resource_or_id.id}"
  elsif resource_or_id
    "#{@path}/#{resource_or_id}"
  else
    @path
  end
end

Instance Method Details

#reloadObject

Reloads resource



39
40
41
42
# File 'lib/cubits/resource.rb', line 39

def reload
  fail "Resource #{self.class.name} does not have an id" unless self.respond_to?(:id)
  replace(self.class.find(id))
end