Module: Alula::ApiOperations::Save::InstanceMethods

Defined in:
lib/alula/api_operations/save.rb

Instance Method Summary collapse

Instance Method Details

#createObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/alula/api_operations/save.rb', line 32

def create
  data = {
    attributes: as_patchable_json
  }
  #
  # Most creations _won't_ have an ID, but the Alula API utlizes a shared GUID strategy for a few resources
  # We need to conditionally include this ID in the data array even though the record 'doesnt exist'
  # 
  # - Dealer Branding shares the same primary ID as the Dealer record
  # - Dealer Contact info shares the same primary ID as the Dealer record
  data[:id] = id if id

  payload = {
    data: data
  }

  response = Alula::Client.request(:post, self.class.resource_url, payload, {})

  if response.ok?
    construct_from(response.data['data'])
    return true
  end

  handle_errors(response)
end

#saveObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/alula/api_operations/save.rb', line 9

def save
  payload = {
    data: {
      id: id,
      attributes: as_patchable_json
    }
  }
  payload[:data].delete(:id) if video_request?(resource_url)

  response = Alula::Client.request(:patch, resource_url, payload, {})

  if response.ok?
    construct_from(response.data['data'])
    return true
  end

  handle_errors(response)
end

#save!Object



28
29
30
# File 'lib/alula/api_operations/save.rb', line 28

def save!
  save || raise(ValidationError.new(self.errors))
end