Class: Vend::RemoteResource

Inherits:
Resource show all
Includes:
DeclarativeSetters, Paths
Defined in:
lib/vend/remote_resource.rb

Instance Attribute Summary collapse

Attributes inherited from Resource

#store

Instance Method Summary collapse

Methods included from Paths

#close_path, #destroy_path, included, #open_path, #resource_path, #show_path, #update_path

Methods included from DeclarativeSetters

included

Methods inherited from Resource

#initialize, #initialize_virtus, #set_attributes, #set_attributes_virtus

Constructor Details

This class inherits a constructor from Vend::Resource

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



8
9
10
# File 'lib/vend/remote_resource.rb', line 8

def error
  @error
end

Instance Method Details

#loadObject



36
37
38
39
40
41
42
# File 'lib/vend/remote_resource.rb', line 36

def load
  self.attributes = store.get(
    path: resource_path,
    status: 200
  ).data
  self
end

#saveObject



10
11
12
13
14
15
16
17
# File 'lib/vend/remote_resource.rb', line 10

def save
  begin
    save!
  rescue ValidationFailed
    (@error ||= []) << $!.message
    false
  end
end

#save!Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vend/remote_resource.rb', line 19

def save!
  save_path = id ? update_path : self.class.create_path
  method = id ? :post : :post
  response = store.send(method, path: save_path,
    data: sendable_attributes.to_json,
    status: 200
  ).data

  if response['status'] && response['status'] == 'error'
    raise ValidationFailed.
      new [response['error'], response['details']].join(': ')
  end

  self.attributes = response[self.class.path]
  true
end