Module: LHS::Item::Save

Extended by:
ActiveSupport::Concern
Included in:
LHS::Item
Defined in:
lib/lhs/concerns/item/save.rb

Instance Method Summary collapse

Instance Method Details

#save(options = nil) ⇒ Object



9
10
11
12
13
14
# File 'lib/lhs/concerns/item/save.rb', line 9

def save(options = nil)
  save!(options)
rescue LHC::Error => e
  self.errors = LHS::Errors.new(e.response)
  false
end

#save!(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lhs/concerns/item/save.rb', line 16

def save!(options = {})
  options ||= {}
  record = _data.class
  data = _data._raw.dup
  if href.present?
    url = href
  else
    endpoint = record.find_endpoint(data)
    url = endpoint.compile(data)
    endpoint.remove_interpolated_params!(data)
  end

  options = options.merge(method: :post, url: url, body: data.to_json)
  options[:headers] ||= {}
  options[:headers].merge!('Content-Type' => 'application/json')

  data = record.request(options)
  _data.merge_raw!(data)
  true
end